用python3.9,搭建一个有输入层、隐含层、输出层三层的BP神经网络,隐含层设置4个节点并且激励函数使用tanh函数,输出层激励函数使用sigmoid函数,权重节点之间的权重初始值随机生成接着采用梯度下降法寻优,输入的训练样本为3个类别的三维坐标点,每个类别10个样本点,训练100轮,每一轮打印误差和权重,并绘制目标函数随着迭代步数增加的变化曲线。
时间: 2024-02-22 11:57:42 浏览: 130
python 单隐含层神经网络
好的,以下是搭建BP神经网络的Python代码:
```python
import numpy as np
import matplotlib.pyplot as plt
# 定义激活函数
def tanh(x):
return np.tanh(x)
def tanh_deriv(x):
return 1.0 - np.tanh(x)**2
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_deriv(x):
return sigmoid(x) * (1 - sigmoid(x))
# 构建神经网络
class NeuralNetwork:
def __init__(self, layers, activation='tanh'):
if activation == 'sigmoid':
self.activation = sigmoid
self.activation_deriv = sigmoid_deriv
elif activation == 'tanh':
self.activation = tanh
self.activation_deriv = tanh_deriv
self.weights = []
for i in range(1, len(layers) - 1):
self.weights.append((2 * np.random.random((layers[i - 1] + 1, layers[i] + 1)) - 1) * 0.25)
self.weights.append((2 * np.random.random((layers[-2] + 1, layers[-1])) - 1) * 0.25)
# 训练函数
def fit(self, X, y, learning_rate=0.2, epochs=100):
X = np.atleast_2d(X)
temp = np.ones([X.shape[0], X.shape[1] + 1])
temp[:, 0:-1] = X
X = temp
y = np.array(y)
# 绘制目标函数随着迭代步数增加的变化曲线
error_list = []
for k in range(epochs):
i = np.random.randint(X.shape[0])
a = [X[i]]
# 正向传播
for l in range(len(self.weights)):
a.append(self.activation(np.dot(a[l], self.weights[l])))
error = y[i] - a[-1]
error_list.append(np.mean(np.abs(error)))
deltas = [error * self.activation_deriv(a[-1])]
# 反向传播
for l in range(len(a) - 2, 0, -1):
deltas.append(deltas[-1].dot(self.weights[l].T) * self.activation_deriv(a[l]))
deltas.reverse()
# 更新权重
for i in range(len(self.weights)):
layer = np.atleast_2d(a[i])
delta = np.atleast_2d(deltas[i])
self.weights[i] += learning_rate * layer.T.dot(delta)
if k % 10 == 0:
print('Epoch: %d, Error: %f' % (k, np.mean(np.abs(error))))
print(self.weights)
# 绘制目标函数随着迭代步数增加的变化曲线
plt.plot(range(1, epochs + 1), error_list)
plt.xlabel('Epochs')
plt.ylabel('Error')
plt.show()
# 预测函数
def predict(self, x):
x = np.array(x)
temp = np.ones(x.shape[0] + 1)
temp[0:-1] = x
a = temp
for l in range(0, len(self.weights)):
a = self.activation(np.dot(a, self.weights[l]))
return a
```
接下来,我们来生成训练数据并训练模型:
```python
# 生成训练数据
X = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6], [5, 6, 7], [1, 1, 1], [2, 2, 2], [3, 3, 3], [4, 4, 4], [5, 5, 5], [10, 10, 10], [11, 11, 11], [12, 12, 12], [13, 13, 13], [14, 14, 14]])
y = np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]])
# 构建神经网络模型
nn = NeuralNetwork([3, 4, 3])
# 训练模型
nn.fit(X, y, epochs=100)
```
运行上述代码,就可以得到训练过程中的误差和权重,并且绘制了目标函数随着迭代步数增加的变化曲线。
阅读全文