numpy实现多层感知机
时间: 2023-12-14 14:31:43 浏览: 87
以下是使用numpy实现多层感知机的示例代码:
```python
import numpy as np
# 定义sigmoid激活函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 定义softmax激活函数
def softmax(x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x, axis=1, keepdims=True)
# 定义交叉熵损失函数
def cross_entropy_loss(y_pred, y_true):
m = y_pred.shape[0]
log_likelihood = -np.log(y_pred[range(m), y_true])
loss = np.sum(log_likelihood) / m
return loss
# 定义多层感知机类
class MLP:
def __init__(self, input_size, hidden_sizes, output_size):
self.input_size = input_size
self.hidden_sizes = hidden_sizes
self.output_size = output_size
# 初始化权重和偏置
self.weights = []
self.biases = []
sizes = [input_size] + hidden_sizes + [output_size]
for i in range(len(sizes) - 1):
self.weights.append(np.random.randn(sizes[i], sizes[i+1]))
self.biases.append(np.zeros((1, sizes[i+1])))
# 前向传播
def forward(self, x):
self.zs = []
self.activations = [x]
for i in range(len(self.weights)):
z = np.dot(self.activations[-1], self.weights[i]) + self.biases[i]
self.zs.append(z)
a = sigmoid(z)
self.activations.append(a)
y_pred = softmax(self.activations[-1])
return y_pred
# 反向传播
def backward(self, x, y_true, y_pred, learning_rate):
m = y_pred.shape[0]
delta = y_pred
delta[range(m), y_true] -= 1
for i in range(len(self.weights)-1, -1, -1):
delta = np.dot(delta, self.weights[i].T) * sigmoid(self.zs[i]) * (1 - sigmoid(self.zs[i]))
self.weights[i] -= learning_rate * np.dot(self.activations[i].T, delta)
self.biases[i] -= learning_rate * np.sum(delta, axis=0, keepdims=True)
# 训练模型
def train(self, x_train, y_train, epochs, learning_rate):
for epoch in range(epochs):
y_pred = self.forward(x_train)
loss = cross_entropy_loss(y_pred, y_train)
self.backward(x_train, y_train, y_pred, learning_rate)
if epoch % 100 == 0:
print("Epoch %d, loss %.4f" % (epoch, loss))
# 预测
def predict(self, x):
y_pred = self.forward(x)
return np.argmax(y_pred, axis=1)
```
使用示例:
```python
# 生成随机数据
np.random.seed(0)
X = np.random.randn(1000, 10)
y = np.random.randint(0, 3, (1000,))
# 创建模型
model = MLP(input_size=10, hidden_sizes=[5, 3], output_size=3)
# 训练模型
model.train(X, y, epochs=1000, learning_rate=0.1)
# 预测
y_pred = model.predict(X)
print("Accuracy:", np.mean(y_pred == y))
```
阅读全文