加强机关网站内容建设,如何创建一个平台,购物网站介绍,推荐好的网站或网页基于BP神经网络对MNIST数据集检测识别 1#xff0e;作者介绍2#xff0e;BP神经网络介绍2.1 BP神经网络 3#xff0e;BP神经网络对MNIST数据集检测实验3.1 读取数据集3.2 前向传播3.3 损失函数3.4 构建神经网络3.5 训练3.6 模型推理 4#xff0e;完整代码 1#xff0e;作者… 基于BP神经网络对MNIST数据集检测识别 1作者介绍2BP神经网络介绍2.1 BP神经网络 3BP神经网络对MNIST数据集检测实验3.1 读取数据集3.2 前向传播3.3 损失函数3.4 构建神经网络3.5 训练3.6 模型推理 4完整代码 1作者介绍
王凯男西安工程大学电子信息学院2022级研究生 研究方向机器视觉与人工智能 电子邮件1794240761qq.com
张思怡女西安工程大学电子信息学院2022级研究生张宏伟人工智能课题组 研究方向机器视觉与人工智能 电子邮件981664791qq.com
2BP神经网络介绍
2.1 BP神经网络
搭建一个两层两个权重矩阵一个隐藏层的神经网络其中输入节点和输出节点的个数是确定的分别为 784 和 10。而隐藏层节点的个数还未确定并没有明确要求隐藏层的节点个数所以在这里取50个。现在神经网络的结构已经确定了再看一下里面是怎么样的这里画出了对一个数据的运算过程 数学公式
3BP神经网络对MNIST数据集检测实验
3.1 读取数据集
安装numpy :pip install numpy 安装matplotlib pip install matplotlib mnist是一个包含各种手写数字图片的数据集其中有60000个训练数据和10000个测试时局即60000个 train_img 和与之对应的 train_label10000个 test_img 和 与之对应的test_label。 其中的 train_img 和 test_img 就是这种图片的形式train_img 是为了训练神经网络算法的训练数据test_img 是为了测试神经网络算法的测试数据每一张图片为2828将图片转换为2828784个像素点每个像素点的值为0到255像素点值的大小代表灰度从而构成一个1784的矩阵作为神经网络的输入而神经网络的输出形式为110的矩阵个eg[0.010.010.010.040.80.010.10.010.010.01]矩阵里的数字代表神经网络预测值的概率比如0.8代表第五个数的预测值概率。 其中 train_label 和 test_label 是 对应训练数据和测试数据的标签可以理解为一个1*10的矩阵用one-hot-vectors只有正确解表示为1表示one_hot_label为True的情况下标签作为one-hot数组返回one-hot数组 例[0000100000]即矩阵里的数字1代表第五个数为True也就是这个标签代表数字5。 数据集的读取 load_mnist(normalizeTrue, flattenTrue, one_hot_labelFalse):中 normalize : 是否将图像的像素值正规化为0.0~1.0将像素值正规化有利于提高精度。flatten : 是否将图像展开为一维数组。 one_hot_label:是否采用one-hot表示。 完整代码及数据集下载https://gitee.com/wang-kai-ya/bp.git
3.2 前向传播
前向传播时我们可以构造一个函数输入数据输出预测。
def predict(self, x):w1, w2 self.dict[w1], self.dict[w2]b1, b2 self.dict[b1], self.dict[b2]a1 np.dot(x, w1) b1z1 sigmoid(a1)a2 np.dot(z1, w2) b2y softmax(a2)3.3 损失函数
求出神经网络对一组数据的预测值是一个1*10的矩阵。
其中Yk表示的是第k个节点的预测值Tk表示标签中第k个节点的one-hot值举前面的eg手写数字5的图片预测值和5的标签 Yk[0.010.010.010.040.80.010.10.010.010.01] Tk[0, 0, 0, 0, 1, 0, 0, 0, 0, 0] 值得一提的是在交叉熵误差函数中Tk的值只有一个1其余为0所以对于这个数据的交叉熵误差就为 E -1log0.8。 在这里选用交叉熵误差作为损失函数代码实现如下
def loss(self, y, t):t t.argmax(axis1)num y.shape[0]s y[np.arange(num), t]return -np.sum(np.log(s)) / num3.4 构建神经网络
前面我们定义了预测值predict, 损失函数loss, 识别精度accuracy, 梯度grad下面构建一个神经网络的类把这些方法添加到神经网络的类中
for i in range(epoch):batch_mask np.random.choice(train_size, batch_size) # 从0到60000 随机选100个数x_batch x_train[batch_mask]y_batch net.predict(x_batch)t_batch t_train[batch_mask]grad net.gradient(x_batch, t_batch)for key in (w1, b1, w2, b2):net.dict[key] - lr * grad[key]loss net.loss(y_batch, t_batch)train_loss_list.append(loss)# 每批数据记录一次精度和当前的损失值if i % iter_per_epoch 0:train_acc net.accuracy(x_train, t_train)test_acc net.accuracy(x_test, t_test)train_acc_list.append(train_acc)test_acc_list.append(test_acc)print(第 str(i/600) 次迭代train_acc, test_acc, loss :| str(train_acc) , str(test_acc) , str(loss))3.5 训练
import numpy as np
import matplotlib.pyplot as plt
from TwoLayerNet import TwoLayerNet
from mnist import load_mnist(x_train, t_train), (x_test, t_test) load_mnist(normalizeTrue, one_hot_labelTrue)
net TwoLayerNet(input_size784, hidden_size50, output_size10, weight_init_std0.01)epoch 20400
batch_size 100
lr 0.1train_size x_train.shape[0] # 60000
iter_per_epoch max(train_size / batch_size, 1) # 600train_loss_list []
train_acc_list []
test_acc_list []保存权重
np.save(w1.npy, net.dict[w1])
np.save(b1.npy, net.dict[b1])
np.save(w2.npy, net.dict[w2])
np.save(b2.npy, net.dict[b2])结果可视化
3.6 模型推理
import numpy as np
from mnist import load_mnist
from functions import sigmoid, softmax
import cv2
######################################数据的预处理
(x_train, t_train), (x_test, t_test) load_mnist(normalizeTrue, one_hot_labelTrue)
batch_mask np.random.choice(100,1) # 从0到60000 随机选100个数
#print(batch_mask)
x_batch x_train[batch_mask]#####################################转成图片
arr x_batch.reshape(28,28)
cv2.imshow(wk,arr)
key cv2.waitKey(10000)
#np.savetxt(batch_mask.txt,arr)
#print(x_batch)
#train_size x_batch.shape[0]
#print(train_size)
########################################进入模型预测
w1 np.load(w1.npy)
b1 np.load(b1.npy)
w2 np.load(w2.npy)
b2 np.load(b2.npy)a1 np.dot(x_batch,w1) b1
z1 sigmoid(a1)
a2 np.dot(z1,w2) b2
y softmax(a2)
p np.argmax(y, axis1)print(p)运行python reasoning.py 可以看到模型拥有较高的准确率。
4完整代码
训练
import numpy as np
import matplotlib.pyplot as plt
from TwoLayerNet import TwoLayerNet
from mnist import load_mnist(x_train, t_train), (x_test, t_test) load_mnist(normalizeTrue, one_hot_labelTrue)
net TwoLayerNet(input_size784, hidden_size50, output_size10, weight_init_std0.01)epoch 20400
batch_size 100
lr 0.1train_size x_train.shape[0] # 60000
iter_per_epoch max(train_size / batch_size, 1) # 600train_loss_list []
train_acc_list []
test_acc_list []for i in range(epoch):batch_mask np.random.choice(train_size, batch_size) # 从0到60000 随机选100个数x_batch x_train[batch_mask]y_batch net.predict(x_batch)t_batch t_train[batch_mask]grad net.gradient(x_batch, t_batch)for key in (w1, b1, w2, b2):net.dict[key] - lr * grad[key]loss net.loss(y_batch, t_batch)train_loss_list.append(loss)# 每批数据记录一次精度和当前的损失值if i % iter_per_epoch 0:train_acc net.accuracy(x_train, t_train)test_acc net.accuracy(x_test, t_test)train_acc_list.append(train_acc)test_acc_list.append(test_acc)print(第 str(i/600) 次迭代train_acc, test_acc, loss :| str(train_acc) , str(test_acc) , str(loss))np.save(w1.npy, net.dict[w1])
np.save(b1.npy, net.dict[b1])
np.save(w2.npy, net.dict[w2])
np.save(b2.npy, net.dict[b2])markers {train: o, test: s}
x np.arange(len(train_acc_list))
plt.plot(x, train_acc_list, labeltrain acc)
plt.plot(x, test_acc_list, labeltest acc, linestyle--)
plt.xlabel(epochs)
plt.ylabel(accuracy)
plt.ylim(0, 1.0)
plt.legend(loclower right)
plt.show()测试
import numpy as np
from mnist import load_mnist
from functions import sigmoid, softmax
import cv2
######################################数据的预处理
(x_train, t_train), (x_test, t_test) load_mnist(normalizeTrue, one_hot_labelTrue)
batch_mask np.random.choice(100,1) # 从0到60000 随机选100个数
#print(batch_mask)
x_batch x_train[batch_mask]#####################################转成图片
arr x_batch.reshape(28,28)
cv2.imshow(wk,arr)
key cv2.waitKey(10000)
#np.savetxt(batch_mask.txt,arr)
#print(x_batch)
#train_size x_batch.shape[0]
#print(train_size)
########################################进入模型预测
w1 np.load(w1.npy)
b1 np.load(b1.npy)
w2 np.load(w2.npy)
b2 np.load(b2.npy)a1 np.dot(x_batch,w1) b1
z1 sigmoid(a1)
a2 np.dot(z1,w2) b2
y softmax(a2)
p np.argmax(y, axis1)print(p)