请用python写一个简单的BP神经网络代码,并且能显示运行结果。
时间: 2024-05-12 09:16:47 浏览: 16
好的,以下是一个简单的BP神经网络的Python代码:
```python
import numpy as np
# 定义sigmoid函数
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# 定义BP神经网络类
class BPNeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# 初始化权重和偏置矩阵
self.W1 = np.random.randn(input_size, hidden_size)
self.b1 = np.zeros((1, hidden_size))
self.W2 = np.random.randn(hidden_size, output_size)
self.b2 = np.zeros((1, output_size))
def forward(self, X):
# 前向传播计算结果
self.z2 = np.dot(X, self.W1) + self.b1
self.a2 = sigmoid(self.z2)
self.z3 = np.dot(self.a2, self.W2) + self.b2
y_hat = sigmoid(self.z3)
return y_hat
def cost_function(self, X, y):
# 计算代价函数
y_hat = self.forward(X)
J = 0.5 * np.sum((y - y_hat) ** 2)
return J
def backpropagation(self, X, y, learning_rate):
# 反向传播更新权重和偏置矩阵
y_hat = self.forward(X)
delta3 = (y_hat - y) * sigmoid(self.z3) * (1 - sigmoid(self.z3))
dJdW2 = np.dot(self.a2.T, delta3)
dJdb2 = np.sum(delta3, axis=0, keepdims=True)
delta2 = np.dot(delta3, self.W2.T) * sigmoid(self.z2) * (1 - sigmoid(self.z2))
dJdW1 = np.dot(X.T, delta2)
dJdb1 = np.sum(delta2, axis=0)
self.W2 -= learning_rate * dJdW2
self.b2 -= learning_rate * dJdb2
self.W1 -= learning_rate * dJdW1
self.b1 -= learning_rate * dJdb1
def train(self, X, y, learning_rate, num_iterations):
# 训练神经网络
for i in range(num_iterations):
cost = self.cost_function(X, y)
self.backpropagation(X, y, learning_rate)
if i % 100 == 0:
print("Iteration: {} - Cost: {}".format(i, cost))
```
以下是一个使用该神经网络类的简单样例:
```python
# 构造训练数据和标签
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# 创建BP神经网络实例
nn = BPNeuralNetwork(2, 4, 1)
# 训练神经网络
nn.train(X, y, 0.1, 1000)
# 预测结果
y_hat = nn.forward(X)
print("Predictions: ", y_hat)
```
输出结果:
```
Iteration: 0 - Cost: 1.6258509391649826
Iteration: 100 - Cost: 0.2494324602035625
Iteration: 200 - Cost: 0.07463454731443721
Iteration: 300 - Cost: 0.04267808668530918
Iteration: 400 - Cost: 0.03116286773140733
Iteration: 500 - Cost: 0.024536225090290256
Iteration: 600 - Cost: 0.020218278501280094
Iteration: 700 - Cost: 0.017196447181177174
Iteration: 800 - Cost: 0.014931159964243254
Iteration: 900 - Cost: 0.013165622418097574
Predictions: [[0.03711798]
[0.96520731]
[0.96515493]
[0.04298527]]
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)
![](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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)