给出python实现3层的BP神经网络的完整代码 要求: •1.在第一象限,随机生成两类数据集合【尽量要求线性不可分】 2. 构造3层BP神经网络对两类数据集合进行分类训练 3. 进行分类测试 4.原则上要求使用源码编写,不要使用资源包
时间: 2024-12-12 16:32:46 浏览: 20
在Python中,我们可以使用numpy和matplotlib库来创建一个简单的三层BP(Backpropagation)神经网络。这里是一个简化的示例,用于在二维空间中模拟线性不可分的数据集,并使用梯度下降法进行训练。请注意,这个例子仅作教学目的,实际应用中可能需要更复杂的数据预处理和网络结构。
```python
import numpy as np
import matplotlib.pyplot as plt
# 生成二维线性不可分的数据集
np.random.seed(0)
x1 = np.linspace(-5, 5, 100)
y1 = x1 + 2
x2 = np.linspace(-5, -2, 100)
y2 = -x2 + 3
data = np.vstack((np.column_stack([x1, y1]), np.column_stack([x2, y2])))
X = data[:, 0:2]
Y = np.hstack([np.zeros(len(x1)), np.ones(len(x2))])
# 划分训练集和测试集
split_ratio = 0.8
train_size = int(split_ratio * len(X))
X_train, Y_train = X[:train_size], Y[:train_size]
X_test, Y_test = X[train_size:], Y[train_size:]
# 定义BP神经网络模型
class NeuralNetwork:
def __init__(self, layers):
self.layers = layers
self.weights = [np.random.randn(y, x) for x, y in zip(layers[:-1], layers[1:])]
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
def forward(self, X):
activations = [X]
for W in self.weights:
z = np.dot(W, activations[-1])
activations.append(self.sigmoid(z))
return activations
def backward(self, X, Y, output, learning_rate=0.1):
m = X.shape[1]
d_weights = []
d_biases = []
# 计算误差反向传播
dz = output - Y
d_weights.append(np.dot(dz.T, activations[-2]))
dbias = np.sum(dz, axis=1, keepdims=True)
for i in range(len(self.weights)-1, -1, -1):
dz = np.multiply(dz, activations[i] * (1 - activations[i])) * self.weights[i+1].T
d_weights.insert(0, dz)
d_biases.insert(0, dbias)
# 更新权重和偏置
for i in range(len(self.weights)):
self.weights[i] -= learning_rate * d_weights[i] / m
self.weights[i] += learning_rate * d_weights[i] / m
def train(self, X, Y, epochs=1000):
for epoch in range(epochs):
output = self.forward(X)
loss = np.mean(np.square(output[-1] - Y))
if (epoch+1) % 100 == 0:
print(f"Epoch {epoch+1}: Loss = {loss}")
self.backward(X, Y, output[-1])
nn = NeuralNetwork([2, 5, 1]) # 三层网络:输入层、隐藏层和输出层
nn.train(X_train, Y_train)
# 分类测试
predictions = nn.forward(X_test)[-1] > 0.5
accuracy = np.mean(predictions == Y_test)
print(f"Accuracy on test set: {accuracy}")
# 可视化决策边界
plt.scatter(X_train[Y_train == 0, 0], X_train[Y_train == 0, 1], color='red', label="Class 0")
plt.scatter(X_train[Y_train == 1, 0], X_train[Y_train == 1, 1], color='blue', label="Class 1")
decision_boundary = -(nn.weights[-1][0, 0] / nn.weights[-1][0, 1])
plt.plot([-6, 6], [decision_boundary, decision_boundary], 'k-', linewidth=2)
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.show()
```
阅读全文