Python 实现 BP 神经网络 - Iris 数据集分类
import numpy as np import matplotlib.pyplot as plt from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import LabelBinarizer
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(self.input_size, self.hidden_size) * 0.01
self.b1 = np.zeros((1, self.hidden_size))
self.W2 = np.random.randn(self.hidden_size, self.output_size) * 0.01
self.b2 = np.zeros((1, self.output_size))
# 定义 sigmoid 函数
def sigmoid(self, z):
return 1 / (1 + np.exp(-z))
# 前向传播
def forward(self, X):
self.Z1 = np.dot(X, self.W1) + self.b1
self.A1 = self.sigmoid(self.Z1)
self.Z2 = np.dot(self.A1, self.W2) + self.b2
self.A2 = self.sigmoid(self.Z2)
return self.A2
# 反向传播
def backward(self, X, y, learning_rate):
m = X.shape[0]
dZ2 = self.A2 - y
dW2 = np.dot(self.A1.T, dZ2) / m
db2 = np.sum(dZ2, axis=0, keepdims=True) / m
dZ1 = np.dot(dZ2, self.W2.T) * (self.A1 * (1 - self.A1))
dW1 = np.dot(X.T, dZ1) / m
db1 = np.sum(dZ1, axis=0, keepdims=True) / m
self.W1 -= learning_rate * dW1
self.b1 -= learning_rate * db1
self.W2 -= learning_rate * dW2
self.b2 -= learning_rate * db2
# 训练神经网络
def train(self, X, y, learning_rate, epochs):
for i in range(epochs):
self.forward(X)
self.backward(X, y, learning_rate)
# 预测结果
def predict(self, X):
return np.round(self.forward(X))
# 绘制损失函数曲线
def plot_loss(self, loss_history):
plt.plot(loss_history)
plt.title('Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()
加载数据集并预处理
iris = load_iris() X = iris.data y = iris.target.reshape((-1, 1)) encoder = LabelBinarizer() y = encoder.fit_transform(y)
划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
创建神经网络模型
model = BPNeuralNetwork(input_size=X.shape[1], hidden_size=10, output_size=y.shape[1])
训练模型并绘制损失函数曲线
loss_history = [] for i in range(2000): model.train(X_train, y_train, learning_rate=0.1, epochs=1) loss = np.mean(np.square(y_train - model.forward(X_train))) loss_history.append(loss) model.plot_loss(loss_history)
在测试集上进行预测
y_pred = model.predict(X_test)
计算准确率
accuracy = 100 * np.sum(y_pred == y_test) / y_test.shape[0] print('Accuracy: %.2f%%' % accuracy) print('202178030431 王秋野')
原文地址: https://www.cveoy.top/t/topic/nFdQ 著作权归作者所有。请勿转载和采集!