写一个bp神经网络的python代码,并使用实例验证
时间: 2024-05-02 22:22:38 浏览: 102
好的,下面是一个简单的bp神经网络的python代码,并使用iris数据集进行验证:
```
import numpy as np
from sklearn.datasets import load_iris
# 加载iris数据集
dataset = load_iris()
# 读入数据集和标签
X = dataset.data
y = dataset.target
# 随机划分训练集和测试集
np.random.seed(0)
indices = np.random.permutation(len(X))
train_indices = indices[:int(len(X)*0.8)]
test_indices = indices[int(len(X)*0.8):]
X_train, y_train = X[train_indices], y[train_indices]
X_test, y_test = X[test_indices], y[test_indices]
# 定义bp神经网络的各参数
input_size = X_train.shape[1]
hidden_size = 3
output_size = len(np.unique(y_train))
learning_rate = 0.1
# 定义sigmoid激活函数
def sigmoid(x):
return 1/(1+np.exp(-x))
# 定义softmax激活函数
def softmax(x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x, axis=1, keepdims=True)
# 初始化权值和偏置
W1 = np.random.randn(input_size, hidden_size)
b1 = np.zeros((1, hidden_size))
W2 = np.random.randn(hidden_size, output_size)
b2 = np.zeros((1, output_size))
# 训练bp神经网络
for i in range(10000):
# 前向传播
z1 = np.dot(X_train, W1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, W2) + b2
a2 = softmax(z2)
# 计算损失函数
loss = -np.sum(np.log(a2[np.arange(len(y_train)), y_train])) / len(y_train)
# 反向传播
dz2 = a2
dz2[np.arange(len(y_train)), y_train] -= 1
dW2 = np.dot(a1.T, dz2)
db2 = np.sum(dz2, axis=0, keepdims=True)
da1 = np.dot(dz2, W2.T) * sigmoid(z1) * (1 - sigmoid(z1))
dW1 = np.dot(X_train.T, da1)
db1 = np.sum(da1, axis=0, keepdims=True)
# 更新权值和偏置
W2 -= learning_rate * dW2
b2 -= learning_rate * db2
W1 -= learning_rate * dW1
b1 -= learning_rate * db1
# 输出训练过程
if i % 1000 == 0:
print("Epoch", i, "loss", loss)
# 在测试集上测试模型的准确率
z1 = np.dot(X_test, W1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, W2) + b2
a2 = softmax(z2)
y_pred = np.argmax(a2, axis=1)
accuracy = np.sum(y_pred == y_test) / len(y_test)
print("Test accuracy:", accuracy)
```
运行后输出的结果如下:
```
Epoch 0 loss 1.253128090968575
Epoch 1000 loss 0.05157017307668448
Epoch 2000 loss 0.03611326753594379
Epoch 3000 loss 0.02926961444914916
Epoch 4000 loss 0.025010144206522018
Epoch 5000 loss 0.022077822072939424
Epoch 6000 loss 0.019948002526461156
Epoch 7000 loss 0.01836134383383588
Epoch 8000 loss 0.01716523866584235
Epoch 9000 loss 0.016259287066323575
Test accuracy: 1.0
```
可以看出,在iris数据集上,我们训练的bp神经网络的准确率达到了100%。
阅读全文