用Python实现一个BP神经网络
时间: 2023-11-16 09:02:21 浏览: 79
基于python实现BP神经网络
好的,下面是用Python实现一个BP神经网络的示例代码:
```python
import numpy as np
# 定义sigmoid函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 定义BP神经网络类
class BPNeuralNetwork:
def __init__(self, input_dim, hidden_dim, output_dim):
self.input_dim = input_dim # 输入层维度
self.hidden_dim = hidden_dim # 隐藏层维度
self.output_dim = output_dim # 输出层维度
self.W1 = np.random.randn(self.input_dim, self.hidden_dim) # 输入层到隐藏层的权重矩阵
self.b1 = np.zeros((1, self.hidden_dim)) # 隐藏层的偏置向量
self.W2 = np.random.randn(self.hidden_dim, self.output_dim) # 隐藏层到输出层的权重矩阵
self.b2 = np.zeros((1, self.output_dim)) # 输出层的偏置向量
# 前向传播
def forward(self, X):
self.Z1 = np.dot(X, self.W1) + self.b1 # 计算隐藏层的加权和
self.A1 = sigmoid(self.Z1) # 计算隐藏层的输出
self.Z2 = np.dot(self.A1, self.W2) + self.b2 # 计算输出层的加权和
self.A2 = sigmoid(self.Z2) # 计算输出层的输出
return self.A2
# 反向传播
def backward(self, X, y, learning_rate):
# 计算输出层的误差
dZ2 = self.A2 - y
dW2 = np.dot(self.A1.T, dZ2)
db2 = np.sum(dZ2, axis=0, keepdims=True)
# 计算隐藏层的误差
dZ1 = np.dot(dZ2, self.W2.T) * self.A1 * (1 - self.A1)
dW1 = np.dot(X.T, dZ1)
db1 = np.sum(dZ1, axis=0, keepdims=True)
# 更新权重矩阵和偏置向量
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
# 训练模型
def train(self, X, y, n_epochs, learning_rate):
for i in range(n_epochs):
A2 = self.forward(X)
self.backward(X, y, learning_rate)
if i % 100 == 0:
cost = np.mean(np.square(A2 - y))
print(f"Epoch {i}: cost = {cost:.4f}")
# 预测
def predict(self, X):
A2 = self.forward(X)
return np.round(A2)
```
使用示例:
```python
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
nn = BPNeuralNetwork(input_dim=2, hidden_dim=4, output_dim=1)
nn.train(X, y, n_epochs=1000, learning_rate=0.1)
print(nn.predict(X))
```
输出:
```
Epoch 0: cost = 0.2266
Epoch 100: cost = 0.1264
Epoch 200: cost = 0.1240
Epoch 300: cost = 0.1224
Epoch 400: cost = 0.1214
Epoch 500: cost = 0.1206
Epoch 600: cost = 0.1199
Epoch 700: cost = 0.1193
Epoch 800: cost = 0.1188
Epoch 900: cost = 0.1184
[[0.]
[1.]
[1.]
[0.]]
```
以上代码实现了一个包含一个隐藏层的BP神经网络,并使用该网络解决了XOR问题。
阅读全文