pythonbp神经网络代码
时间: 2023-10-19 11:09:20 浏览: 108
鲸鱼算法优化bp神经网络,完整代码,换成自己的数据即可使用
5星 · 资源好评率100%
好的,以下是一个简单的 Python BP 神经网络代码示例:
```python
import numpy as np
class NeuralNetwork:
def __init__(self, layers):
self.weights = []
for i in range(1, len(layers)):
w = np.random.randn(layers[i-1], layers[i])
self.weights.append(w)
def sigmoid(self, x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(self, x):
return x * (1 - x)
def feed_forward(self, X):
a = X
for w in self.weights:
z = np.dot(a, w)
a = self.sigmoid(z)
return a
def backpropagation(self, X, y, learning_rate):
a = X
activations = [a]
zs = []
for w in self.weights:
z = np.dot(a, w)
zs.append(z)
a = self.sigmoid(z)
activations.append(a)
delta = (activations[-1] - y) * self.sigmoid_derivative(activations[-1])
for i in range(len(self.weights)-1, -1, -1):
dw = np.dot(activations[i].T, delta)
delta = np.dot(delta, self.weights[i].T) * self.sigmoid_derivative(activations[i])
self.weights[i] -= learning_rate * dw
def train(self, X, y, learning_rate=0.1, epochs=1000):
for i in range(epochs):
self.backpropagation(X, y, learning_rate)
def predict(self, X):
return self.feed_forward(X)
# 示例用法
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
nn = NeuralNetwork([2, 4, 1])
nn.train(X, y)
print(nn.predict(X))
```
该代码实现了一个简单的 BP 神经网络,包括前向传播、反向传播和训练等功能。其中,`NeuralNetwork` 类的构造函数接受一个列表 `layers`,表示神经网络的层数和每层的神经元个数;`sigmoid` 和 `sigmoid_derivative` 方法分别实现了 sigmoid 函数和其导数;`feed_forward` 方法实现了前向传播;`backpropagation` 方法实现了反向传播;`train` 方法用于训练神经网络;`predict` 方法用于预测输出。
阅读全文