bp神经网络python
时间: 2023-08-26 08:07:09 浏览: 45
使用Python实现BP神经网络可以分为以下几个步骤:
1. 导入必要的库和模块:
```python
import numpy as np
import matplotlib.pyplot as plt
```
2. 定义一个BP神经网络类:
```python
class BPNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights1 = np.random.randn(self.input_size, self.hidden_size)
self.weights2 = np.random.randn(self.hidden_size, self.output_size)
self.bias1 = np.zeros((1, self.hidden_size))
self.bias2 = np.zeros((1, self.output_size))
```
其中,input_size、hidden_size和output_size分别表示输入层、隐藏层和输出层的神经元个数,weights1和weights2分别表示输入层到隐藏层和隐藏层到输出层的权重,bias1和bias2分别表示隐藏层和输出层的偏置。
3. 定义一个sigmoid函数和其导数:
```python
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
```
sigmoid函数是BP神经网络中常用的激活函数,sigmoid_derivative函数是其导数。
4. 定义前向传播函数:
```python
def forward(self, X):
self.z2 = np.dot(X, self.weights1) + self.bias1
self.a2 = sigmoid(self.z2)
self.z3 = np.dot(self.a2, self.weights2) + self.bias2
y_hat = sigmoid(self.z3)
return y_hat
```
其中,X表示输入数据,z2、a2和z3分别表示隐藏层的加权输入、隐藏层的输出和输出层的加权输入,y_hat表示输出层的输出。
5. 定义反向传播函数:
```python
def backward(self, X, y, y_hat, learning_rate):
delta3 = (y_hat - y) * sigmoid_derivative(y_hat)
d_weights2 = np.dot(self.a2.T, delta3)
d_bias2 = np.sum(delta3, axis=0, keepdims=True)
delta2 = np.dot(delta3, self.weights2.T) * sigmoid_derivative(self.a2)
d_weights1 = np.dot(X.T, delta2)
d_bias1 = np.sum(delta2, axis=0)
self.weights1 -= learning_rate * d_weights1
self.bias1 -= learning_rate * d_bias1
self.weights2 -= learning_rate * d_weights2
self.bias2 -= learning_rate * d_bias2
```
其中,X、y和y_hat分别表示输入数据、实际输出和预测输出,learning_rate表示学习率,delta3表示输出层的误差,d_weights2和d_bias2分别表示隐藏层到输出层权重和偏置的梯度,delta2表示隐藏层的误差,d_weights1和d_bias1分别表示输入层到隐藏层权重和偏置的梯度。
6. 训练模型:
```python
def train(self, X, y, epochs, learning_rate):
losses = []
for i in range(epochs):
y_hat = self.forward(X)
loss = np.mean(0.5 * (y - y_hat) ** 2)
losses.append(loss)
self.backward(X, y, y_hat, learning_rate)
if i % 1000 == 0:
print('Epoch:', i, 'Loss:', loss)
return losses
```
其中,X、y分别表示输入数据和实际输出,epochs表示训练轮数,learning_rate表示学习率,losses表示每轮训练的损失值。
7. 使用模型进行预测:
```python
def predict(self, X):
y_hat = self.forward(X)
return np.round(y_hat)
```
其中,X表示输入数据,y_hat表示预测输出。
完整代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
class BPNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
self.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
self.weights1 = np.random.randn(self.input_size, self.hidden_size)
self.weights2 = np.random.randn(self.hidden_size, self.output_size)
self.bias1 = np.zeros((1, self.hidden_size))
self.bias2 = np.zeros((1, self.output_size))
def forward(self, X):
self.z2 = np.dot(X, self.weights1) + self.bias1
self.a2 = sigmoid(self.z2)
self.z3 = np.dot(self.a2, self.weights2) + self.bias2
y_hat = sigmoid(self.z3)
return y_hat
def backward(self, X, y, y_hat, learning_rate):
delta3 = (y_hat - y) * sigmoid_derivative(y_hat)
d_weights2 = np.dot(self.a2.T, delta3)
d_bias2 = np.sum(delta3, axis=0, keepdims=True)
delta2 = np.dot(delta3, self.weights2.T) * sigmoid_derivative(self.a2)
d_weights1 = np.dot(X.T, delta2)
d_bias1 = np.sum(delta2, axis=0)
self.weights1 -= learning_rate * d_weights1
self.bias1 -= learning_rate * d_bias1
self.weights2 -= learning_rate * d_weights2
self.bias2 -= learning_rate * d_bias2
def train(self, X, y, epochs, learning_rate):
losses = []
for i in range(epochs):
y_hat = self.forward(X)
loss = np.mean(0.5 * (y - y_hat) ** 2)
losses.append(loss)
self.backward(X, y, y_hat, learning_rate)
if i % 1000 == 0:
print('Epoch:', i, 'Loss:', loss)
return losses
def predict(self, X):
y_hat = self.forward(X)
return np.round(y_hat)
if __name__ == '__main__':
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
nn = BPNeuralNetwork(2, 3, 1)
losses = nn.train(X, y, epochs=10000, learning_rate=0.1)
plt.plot(losses)
plt.show()
print('Predictions:', nn.predict(X))
```
在上述代码中,我们使用BP神经网络来解决异或逻辑运算问题,其中X表示输入数据,y表示实际输出。在训练模型时,我们设置了10000次迭代,学习率为0.1。在预测时,我们打印了模型的预测输出。运行代码后,可以看到模型的损失值在逐渐下降,最终输出的预测结果也符合异或逻辑运算的规律。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)