bp算法python 隐藏层有两层的代码示例
时间: 2023-06-17 22:05:28 浏览: 87
基于双隐层BP的函数模拟
下面是一个使用Python实现的具有两个隐藏层的BP神经网络示例代码:
```python
import numpy as np
class NeuralNetwork:
def __init__(self, input_size, hidden_size1, hidden_size2, output_size):
# 定义网络的输入层、两个隐藏层和输出层的大小
self.input_size = input_size
self.hidden_size1 = hidden_size1
self.hidden_size2 = hidden_size2
self.output_size = output_size
# 定义网络的权重和偏置
self.W1 = np.random.randn(self.input_size, self.hidden_size1)
self.b1 = np.zeros((1, self.hidden_size1))
self.W2 = np.random.randn(self.hidden_size1, self.hidden_size2)
self.b2 = np.zeros((1, self.hidden_size2))
self.W3 = np.random.randn(self.hidden_size2, self.output_size)
self.b3 = np.zeros((1, self.output_size))
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def forward(self, X):
# 前向传播计算输出值
self.z1 = np.dot(X, self.W1) + self.b1
self.a1 = self.sigmoid(self.z1)
self.z2 = np.dot(self.a1, self.W2) + self.b2
self.a2 = self.sigmoid(self.z2)
self.z3 = np.dot(self.a2, self.W3) + self.b3
y_pred = self.sigmoid(self.z3)
return y_pred
def backward(self, X, y, y_pred, learning_rate):
# 反向传播计算梯度
delta4 = (y_pred - y) * self.sigmoid_derivative(y_pred)
dW3 = np.dot(self.a2.T, delta4)
db3 = np.sum(delta4, axis=0, keepdims=True)
delta3 = np.dot(delta4, self.W3.T) * self.sigmoid_derivative(self.a2)
dW2 = np.dot(self.a1.T, delta3)
db2 = np.sum(delta3, axis=0)
delta2 = np.dot(delta3, self.W2.T) * self.sigmoid_derivative(self.a1)
dW1 = np.dot(X.T, delta2)
db1 = np.sum(delta2, axis=0)
# 更新权重和偏置
self.W3 -= learning_rate * dW3
self.b3 -= learning_rate * db3
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, learning_rate, epochs):
for i in range(epochs):
# 前向传播计算输出值
y_pred = self.forward(X)
# 反向传播计算梯度并更新权重和偏置
self.backward(X, y, y_pred, learning_rate)
# 每1000个epoch输出一次损失函数的值
if i % 1000 == 0:
loss = np.mean(np.square(y_pred - y))
print("Epoch", i, "loss:", loss)
```
这个神经网络具有一个输入层、两个隐藏层和一个输出层。在初始化网络时,我们指定了每个层的大小。在训练时,我们使用反向传播算法计算梯度并更新网络的权重和偏置。在每个epoch结束时,我们计算损失函数的值并输出。
阅读全文