构建BP神经网络代码
时间: 2023-11-22 08:47:43 浏览: 56
以下是构建BP神经网络的Python代码,其中包括了数据预处理、模型构建、训练和预测等步骤:
```python
import numpy as np
# 数据预处理
def preprocess_data(X, y):
X = np.array(X)
y = np.array(y)
X = X / np.max(X) # 归一化
y = y.reshape(-1, 1)
return X, y
# 激活函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 损失函数
def loss(y_pred, y_true):
return np.mean((y_pred - y_true) ** 2)
# 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(input_dim, hidden_dim) # 输入层到隐藏层的权重
self.b1 = np.zeros((1, hidden_dim)) # 隐藏层的偏置
self.W2 = np.random.randn(hidden_dim, output_dim) # 隐藏层到输出层的权重
self.b2 = np.zeros((1, 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_true, y_pred, learning_rate):
delta2 = (y_pred - y_true) * self.a2 * (1 - self.a2)
dW2 = np.dot(self.a1.T, delta2)
db2 = np.sum(delta2, axis=0, keepdims=True)
delta1 = np.dot(delta2, self.W2.T) * self.a1 * (1 - self.a1)
dW1 = np.dot(X.T, delta1)
db1 = np.sum(delta1, axis=0)
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
# 训练模型
def train(self, X, y, epochs, learning_rate):
X, y = preprocess_data(X, y)
for i in range(epochs):
y_pred = self.forward(X)
l = loss(y_pred, y)
self.backward(X, y, y_pred, learning_rate)
if i % 100 == 0:
print("Epoch %d: loss = %f" % (i, l))
# 预测
def predict(self, X):
X = np.array(X)
X = X / np.max(X) # 归一化
y_pred = self.forward(X)
return y_pred
```
阅读全文