pycharm设计一个bp网络并以下载的手写体数据集(分训练样例和测试样例)训练该网络,用训练好的模型识别新样例,用表格记录训练参数和测试结果。
时间: 2024-03-26 18:34:04 浏览: 199
设计BP网络并测试程序
好的,下面是一个使用PyCharm设计BP网络并训练手写体识别任务的示例代码,供您参考:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# 加载手写体数据集,这里以MNIST数据集为例
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# 数据预处理
x_train = x_train.reshape(-1, 784) / 255.0
x_test = x_test.reshape(-1, 784) / 255.0
y_train = pd.get_dummies(y_train).values
y_test = pd.get_dummies(y_test).values
# 定义BP神经网络模型
class BPNeuralNetwork:
def __init__(self, n_input, n_hidden, n_output):
self.n_input = n_input
self.n_hidden = n_hidden
self.n_output = n_output
self.w1 = np.random.randn(self.n_input, self.n_hidden) * 0.01
self.b1 = np.zeros((1, self.n_hidden))
self.w2 = np.random.randn(self.n_hidden, self.n_output) * 0.01
self.b2 = np.zeros((1, self.n_output))
def sigmoid(self, x):
return 1.0 / (1.0 + np.exp(-x))
def softmax(self, x):
exp_x = np.exp(x)
return exp_x / np.sum(exp_x, axis=1, keepdims=True)
def forward(self, x):
z1 = np.dot(x, self.w1) + self.b1
a1 = self.sigmoid(z1)
z2 = np.dot(a1, self.w2) + self.b2
a2 = self.softmax(z2)
return a2
def backward(self, x, y, a):
delta2 = a - y
dw2 = np.dot(a1.T, delta2)
db2 = np.sum(delta2, axis=0, keepdims=True)
delta1 = np.dot(delta2, self.w2.T) * (a1 * (1 - a1))
dw1 = np.dot(x.T, delta1)
db1 = np.sum(delta1, axis=0)
return dw1, db1, dw2, db2
def train(self, x, y, learning_rate):
a = self.forward(x)
dw1, db1, dw2, db2 = self.backward(x, y, a)
self.w1 -= learning_rate * dw1
self.b1 -= learning_rate * db1
self.w2 -= learning_rate * dw2
self.b2 -= learning_rate * db2
def predict(self, x):
a = self.forward(x)
return np.argmax(a, axis=1)
# 定义超参数
n_input = 784
n_hidden = 256
n_output = 10
learning_rate = 0.1
n_epochs = 10
batch_size = 128
# 初始化BP神经网络模型
bpnn = BPNeuralNetwork(n_input, n_hidden, n_output)
# 训练模型
n_samples = x_train.shape[0]
n_batches = n_samples // batch_size
for i in range(n_epochs):
for j in range(n_batches):
start_idx = j * batch_size
end_idx = (j + 1) * batch_size
x_batch = x_train[start_idx:end_idx]
y_batch = y_train[start_idx:end_idx]
bpnn.train(x_batch, y_batch, learning_rate)
y_pred = bpnn.predict(x_test)
accuracy = np.mean(np.argmax(y_test, axis=1) == y_pred)
print('Epoch {}/{} - accuracy: {:.4f}'.format(i+1, n_epochs, accuracy))
# 保存模型
bpnn.w1.tofile('w1.bin')
bpnn.b1.tofile('b1.bin')
bpnn.w2.tofile('w2.bin')
bpnn.b2.tofile('b2.bin')
# 加载模型
bpnn = BPNeuralNetwork(n_input, n_hidden, n_output)
bpnn.w1 = np.fromfile('w1.bin').reshape(n_input, n_hidden)
bpnn.b1 = np.fromfile('b1.bin').reshape(1, n_hidden)
bpnn.w2 = np.fromfile('w2.bin').reshape(n_hidden, n_output)
bpnn.b2 = np.fromfile('b2.bin').reshape(1, n_output)
# 对新样例进行预测
x_new = x_test[:10]
y_true = np.argmax(y_test[:10], axis=1)
y_pred = bpnn.predict(x_new)
print('True labels:', y_true)
print('Predicted labels:', y_pred)
# 计算测试集准确率、精度、召回率等指标
y_pred = bpnn.predict(x_test)
accuracy = np.mean(np.argmax(y_test, axis=1) == y_pred)
print('Accuracy:', accuracy)
```
在这段代码中,我们首先加载了MNIST数据集,并对数据进行了预处理。然后我们定义了一个BP神经网络模型,并实现了前向传播、反向传播、训练和预测等方法。接下来,我们使用训练集对模型进行了训练,并在每个epoch结束时计算了模型在测试集上的准确率。最后,我们保存了模型参数,并对新样例进行了预测,并计算了测试集的准确率。
您可以根据自己的需求和数据集,调整超参数和模型结构,以获得更好的性能。同时,您也可以使用表格或其他工具来记录训练参数和测试结果,以便后续分析和比较。
阅读全文