bp神经网络python
时间: 2023-10-21 09:06:31 浏览: 29
在 Python 中实现 BP(反向传播)神经网络,可以使用一些第三方库,例如 NumPy、Keras、TensorFlow 等。
这里以 NumPy 为例,给出一个简单的 BP 神经网络实现的代码:
```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.input_size = input_size
self.hidden_size = hidden_size
self.output_size = output_size
# 初始化权重和偏置
self.W1 = np.random.randn(hidden_size, input_size)
self.b1 = np.zeros((hidden_size, 1))
self.W2 = np.random.randn(output_size, hidden_size)
self.b2 = np.zeros((output_size, 1))
# 前向传播
def forward(self, X):
self.Z1 = np.dot(self.W1, X) + self.b1
self.A1 = sigmoid(self.Z1)
self.Z2 = np.dot(self.W2, self.A1) + self.b2
self.A2 = sigmoid(self.Z2)
return self.A2
# 反向传播
def backward(self, X, y, learning_rate):
m = X.shape[1]
dZ2 = self.A2 - y
dW2 = (1 / m) * np.dot(dZ2, self.A1.T)
db2 = (1 / m) * np.sum(dZ2, axis=1, keepdims=True)
dZ1 = np.dot(self.W2.T, dZ2) * sigmoid(self.Z1) * (1 - sigmoid(self.Z1))
dW1 = (1 / m) * np.dot(dZ1, X.T)
db1 = (1 / m) * np.sum(dZ1, axis=1, keepdims=True)
# 更新权重和偏置
self.W2 = self.W2 - learning_rate * dW2
self.b2 = self.b2 - learning_rate * db2
self.W1 = self.W1 - learning_rate * dW1
self.b1 = self.b1 - learning_rate * db1
# 训练
def train(self, X, y, num_epochs, learning_rate):
for i in range(num_epochs):
# 前向传播
y_hat = self.forward(X)
# 计算损失
loss = -(1 / y.shape[1]) * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
# 反向传播
self.backward(X, y, learning_rate)
# 输出损失
print(f"Epoch {i+1}/{num_epochs}: loss = {loss:.4f}")
# 预测
def predict(self, X):
y_hat = self.forward(X)
return np.round(y_hat)
```
使用示例:
```python
# 构造数据
X = np.array([[0, 0, 1], [0, 1, 1], [1, 0, 1], [1, 1, 1]]).T
y = np.array([[0, 1, 1, 0]])
# 创建 BP 神经网络
nn = BPNeuralNetwork(input_size=X.shape[0], hidden_size=4, output_size=1)
# 训练
nn.train(X, y, num_epochs=10000, learning_rate=0.1)
# 预测
X_test = np.array([[1, 0, 0], [1, 1, 0], [0, 0, 0], [0, 1, 0]]).T
y_pred = nn.predict(X_test)
print(y_pred)
```
参考资料:
- [Build Your Own Neural Network in Python](https://towardsdatascience.com/build-your-own-neural-network-in-python-8d13e5916345)
- [Neural Networks and Deep Learning](http://neuralnetworksanddeeplearning.com/)
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)