【Python实战】——神经网络识别手写数字

🍉CSDN小墨&晓末:https://blog.csdn.net/jd1813346972

   个人介绍: 研一|统计学|干货分享
         擅长Python、Matlab、R等主流编程软件
         累计十余项国家级比赛奖项,参与研究经费10w、40w级横向

文章目录

  • 1 探索数据集
    • 1.1 读取并显示数据示例
    • 1.2 数据集大小
    • 1.3 自变量因变量构建
    • 1.4 One-hot编码
    • 1.5 图像数据示例
    • 1.6 pickle包保存python对象
  • 2 构建神经网络并训练
    • 2.1 读取pickle文件
    • 2.2 神经网络核心关键函数定义
    • 2.3 神经网络模型定义
    • 2.4 模型训练
      • 2.4.1 预测概率
      • 2.4.2 训练集正确率
      • 2.4.3 测试集正确率
      • 2.4.4 训练集判别矩阵
      • 2.4.5 不同数字预测精确率
    • 2.5 结果可视化
      • 2.5.1 每次epoch训练预测情况
      • 2.5.2 迭代30次正确率绘图
  • 3 模型优化
    • 3.1 调整神经元数量
      • 3.1.1 每次epoch训练预测情况
      • 3.1.2 正确率绘图
    • 3.2 更换隐藏层层数
      • 3.2.1 每次epoch训练预测情况
      • 3.2.2 正确率绘图

该篇文章以Python实战的形式利用神经网络识别mnist手写数字数据集,包括pickle操作,神经网络关键模型关键函数定义,识别效果评估及可视化等内容,建议收藏练手!

1 探索数据集

1.1 读取并显示数据示例

  运行程序:

import numpy as np
import matplotlib.pyplot as pltimage_size = 28 # width and length
num_of_different_labels = 10 #  i.e. 0, 1, 2, 3, ..., 9
image_pixels = image_size * image_sizetrain_data = np.loadtxt("D:\\mnist_train.csv", delimiter=",")
test_data = np.loadtxt("D:\\mnist_test.csv", delimiter=",") 
test_data[:10]#测试集前十行

  运行结果:

array([[7., 0., 0., ..., 0., 0., 0.],[2., 0., 0., ..., 0., 0., 0.],[1., 0., 0., ..., 0., 0., 0.],...,[9., 0., 0., ..., 0., 0., 0.],[5., 0., 0., ..., 0., 0., 0.],[9., 0., 0., ..., 0., 0., 0.]])

1.2 数据集大小

  运行程序:

print(test_data.shape)
print(train_data.shape)

  运行结果:

(10000, 785)
(60000, 785)

  该mnist数据集训练集共10000个数据,有785维,测试集有60000个数据,785维。

1.3 自变量因变量构建

  运行程序:

##第一列为预测类别
train_imgs = np.asfarray(train_data[:, 1:]) / 255
test_imgs = np.asfarray(test_data[:, 1:]) / 255 
train_labels = np.asfarray(train_data[:, :1])
test_labels = np.asfarray(test_data[:, :1])

1.4 One-hot编码

  运行程序

import numpy as nplable_range = np.arange(10)for label in range(10):one_hot = (lable_range==label).astype(int)print("label: ", label, " in one-hot representation: ", one_hot)# 将数据集的标签转换为one-hot labellabel_range = np.arange(num_of_different_labels)train_labels_one_hot = (label_range==train_labels).astype(float)
test_labels_one_hot = (label_range==test_labels).astype(float)

1.5 图像数据示例

  运行程序:

# 示例
for i in range(10):img = train_imgs[i].reshape((28,28))plt.imshow(img, cmap="Greys")plt.show()

  运行结果:

1.6 pickle包保存python对象

因为csv文件读取到内存比较慢,我们用pickle这个包来保存python对象(这里面python对象指的是numpy array格式的train_imgs, test_imgs, train_labels, test_labels)

  运行程序:

import picklewith open("D:\\pickled_mnist.pkl", "bw") as fh:data = (train_imgs, test_imgs, train_labels,test_labels)pickle.dump(data, fh)

2 构建神经网络并训练

2.1 读取pickle文件

  运行程序:

import picklewith open("D:\\19实验\\实验课大作业\\pickled_mnist.pkl", "br") as fh:data = pickle.load(fh)train_imgs = data[0]
test_imgs = data[1]
train_labels = data[2]
test_labels = data[3]train_labels_one_hot = (lable_range==train_labels).astype(float)
test_labels_one_hot = (label_range==test_labels).astype(float)image_size = 28 # width and length
num_of_different_labels = 10 #  i.e. 0, 1, 2, 3, ..., 9
image_pixels = image_size * image_size

2.2 神经网络核心关键函数定义

  运行程序:

import numpy as npdef sigmoid(x):return 1 / (1 + np.e ** -x)
##激活函数
activation_function = sigmoidfrom scipy.stats import truncnorm
##数据标准化
def truncated_normal(mean=0, sd=1, low=0, upp=10):return truncnorm((low - mean) / sd, (upp - mean) / sd, loc=mean, scale=sd)
##构建神经网络模型
class NeuralNetwork:def __init__(self, num_of_in_nodes, #输入节点数num_of_out_nodes, #输出节点数num_of_hidden_nodes,#隐藏节点数learning_rate):#学习率self.num_of_in_nodes = num_of_in_nodesself.num_of_out_nodes = num_of_out_nodesself.num_of_hidden_nodes = num_of_hidden_nodesself.learning_rate = learning_rate self.create_weight_matrices()#初始为一个隐藏节点    def create_weight_matrices(self):#创建权重矩阵# A method to initialize the weight #matrices of the neural network#一种初始化神经网络权重矩阵的方法rad = 1 / np.sqrt(self.num_of_in_nodes)  X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)  #形成指定分布self.weight_1 = X.rvs((self.num_of_hidden_nodes, self.num_of_in_nodes)) #rvs:产生服从指定分布的随机数rad = 1 / np.sqrt(self.num_of_hidden_nodes)X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)self.weight_2 = X.rvs((self.num_of_out_nodes, self.num_of_hidden_nodes)) #rvs: 产生服从指定分布的随机数def train(self, input_vector, target_vector):## input_vector and target_vector can #be tuple, list or ndarray#input_vector = np.array(input_vector, ndmin=2).T#输入target_vector = np.array(target_vector, ndmin=2).T#输出output_vector1 = np.dot(self.weight_1, input_vector) #隐藏层值output_hidden = activation_function(output_vector1)#删除不激活output_vector2 = np.dot(self.weight_2, output_hidden)#输出output_network = activation_function(output_vector2)##删除不激活# calculate output errors:计算输出误差output_errors = target_vector - output_network# update the weights:更新权重tmp = output_errors * output_network * (1.0 - output_network)     self.weight_2 += self.learning_rate  * np.dot(tmp, output_hidden.T)# calculate hidden errors:计算隐藏层误差hidden_errors = np.dot(self.weight_2.T, output_errors)# update the weights:tmp = hidden_errors * output_hidden * (1.0 - output_hidden)self.weight_1 += self.learning_rate * np.dot(tmp, input_vector.T)#测试集def run(self, input_vector):# input_vector can be tuple, list or ndarrayinput_vector = np.array(input_vector, ndmin=2).Toutput_vector = np.dot(self.weight_1, input_vector)output_vector = activation_function(output_vector)output_vector = np.dot(self.weight_2, output_vector)output_vector = activation_function(output_vector)return output_vector#判别矩阵def confusion_matrix(self, data_array, labels):cm = np.zeros((10, 10), int)for i in range(len(data_array)):res = self.run(data_array[i])res_max = res.argmax()target = labels[i][0]cm[res_max, int(target)] += 1return cm    #精确度def precision(self, label, confusion_matrix):col = confusion_matrix[:, label]return confusion_matrix[label, label] / col.sum()#评估def evaluate(self, data, labels):corrects, wrongs = 0, 0for i in range(len(data)):res = self.run(data[i])res_max = res.argmax()if res_max == labels[i]:corrects += 1else:wrongs += 1return corrects, wrongs

2.3 神经网络模型定义

  运行程序:

ANN = NeuralNetwork(num_of_in_nodes = image_pixels, #输入num_of_out_nodes = 10, #输出节点数num_of_hidden_nodes = 100,#隐藏节点learning_rate = 0.1)#学习率

2.4 模型训练

2.4.1 预测概率

  运行程序:

for i in range(len(train_imgs)):ANN.train(train_imgs[i], train_labels_one_hot[i])for i in range(20):res = ANN.run(test_imgs[i])print(test_labels[i], np.argmax(res), np.max(res))

  运行结果:

[7.] 7 0.9992648448921
[2.] 2 0.9040034245332168
[1.] 1 0.9992201001324703
[0.] 0 0.9923701545281887
[4.] 4 0.989297708155559
[1.] 1 0.9984582148795715
[4.] 4 0.9957673752296046
[9.] 9 0.9889417895800644
[5.] 6 0.5009071817613537
[9.] 9 0.9879513019542627
[0.] 0 0.9932950902790246
[6.] 6 0.9387061553685657
[9.] 9 0.9962530965286298
[0.] 0 0.9974524110371016
[1.] 1 0.9991354417269441
[5.] 5 0.7607733657668813
[9.] 9 0.9968080255475414
[7.] 7 0.9967748204232602
[3.] 3 0.8820920415159276
[4.] 4 0.9978584850755227

2.4.2 训练集正确率

  运行程序:

corrects, wrongs = ANN.evaluate(train_imgs, train_labels)#训练集判别正确和错误数量
print("accuracy train: ", corrects / ( corrects + wrongs))##正确率

  运行结果:

accuracy train:  0.9425333333333333

2.4.3 测试集正确率

  运行程序:

corrects, wrongs = ANN.evaluate(test_imgs, test_labels)
print("accuracy: test", corrects / ( corrects + wrongs))#测试集正确率

  运行结果:

accuracy: test 0.9412

2.4.4 训练集判别矩阵

  运行程序:

cm = ANN.confusion_matrix(train_imgs, train_labels)
print(cm)   #训练集判别矩阵

  运行结果:

[[5822    1   54   35   15   41   47   12   31   31][   2 6638   62   31   17   24   21   64  163   14][   6   19 5487   57   16    9    2   45   16    4][   7   27   87 5773    3  130    3   16  148   67][  11   11   68    8 5332   34   12   48   28   44][  10    4    6   69    0 4952   34    5   32    5][  31    5   53   19   49   96 5782    5   37    2][   1    9   45   35    6    6    0 5812    5   28][  20    9   70   32    9   37   15   11 5209    9][  13   19   26   72  395   92    2  247  182 5745]]

2.4.5 不同数字预测精确率

  运行程序:

for i in range(10):print("digit: ", i, "precision: ", ANN.precision(i, cm))

  运行结果:

digit:  0 precision:  0.9829478304913051
digit:  1 precision:  0.9845743102936814
digit:  2 precision:  0.9209466263846928
digit:  3 precision:  0.9416082205186755
digit:  4 precision:  0.9127011297500855
digit:  5 precision:  0.9134845969378343
digit:  6 precision:  0.9770192632646164
digit:  7 precision:  0.9276935355147645
digit:  8 precision:  0.8902751666381815
digit:  9 precision:  0.9657085224407463

2.5 结果可视化

2.5.1 每次epoch训练预测情况

  运行程序:

epochs = 30
train_acc=[]
test_acc=[]
NN = NeuralNetwork(num_of_in_nodes = image_pixels, num_of_out_nodes = 10, num_of_hidden_nodes = 100,learning_rate = 0.1)for epoch in range(epochs):  print("epoch: ", epoch)for i in range(len(train_imgs)):NN.train(train_imgs[i], train_labels_one_hot[i])corrects, wrongs = NN.evaluate(train_imgs, train_labels)print("accuracy train: ", corrects / ( corrects + wrongs))train_acc.append(corrects / ( corrects + wrongs))corrects, wrongs = NN.evaluate(test_imgs, test_labels)print("accuracy: test", corrects / ( corrects + wrongs))test_acc.append(corrects / ( corrects + wrongs))

运行结果:

epoch:  0
accuracy train:  0.94455
accuracy: test 0.9422
epoch:  1
accuracy train:  0.9628
accuracy: test 0.9579
epoch:  2
accuracy train:  0.9699
accuracy: test 0.9637
epoch:  3
accuracy train:  0.9761166666666666
accuracy: test 0.9649
epoch:  4
accuracy train:  0.979
accuracy: test 0.9662
epoch:  5
accuracy train:  0.9820833333333333
accuracy: test 0.9679
epoch:  6
accuracy train:  0.9838166666666667
accuracy: test 0.9697
epoch:  7
accuracy train:  0.9845666666666667
accuracy: test 0.97
epoch:  8
accuracy train:  0.9855333333333334
accuracy: test 0.9703
epoch:  9
accuracy train:  0.9868166666666667
accuracy: test 0.97
epoch:  10
accuracy train:  0.9878166666666667
accuracy: test 0.9714
epoch:  11
accuracy train:  0.98845
accuracy: test 0.9716
epoch:  12
accuracy train:  0.98905
accuracy: test 0.9721
epoch:  13
accuracy train:  0.9898166666666667
accuracy: test 0.9723
epoch:  14
accuracy train:  0.9903
accuracy: test 0.9722
epoch:  15
accuracy train:  0.9907666666666667
accuracy: test 0.9719
epoch:  16
accuracy train:  0.9910833333333333
accuracy: test 0.9715
epoch:  17
accuracy train:  0.9918
accuracy: test 0.9714
epoch:  18
accuracy train:  0.9924166666666666
accuracy: test 0.971
epoch:  19
accuracy train:  0.99265
accuracy: test 0.9712
epoch:  20
accuracy train:  0.9932833333333333
accuracy: test 0.972
epoch:  21
accuracy train:  0.9939333333333333
accuracy: test 0.9716
epoch:  22
accuracy train:  0.9944333333333333
accuracy: test 0.972
epoch:  23
accuracy train:  0.9948
accuracy: test 0.9719
epoch:  24
accuracy train:  0.9950833333333333
accuracy: test 0.9718
epoch:  25
accuracy train:  0.9950833333333333
accuracy: test 0.9722
epoch:  26
accuracy train:  0.99525
accuracy: test 0.9725
epoch:  27
accuracy train:  0.9955833333333334
accuracy: test 0.972
epoch:  28
accuracy train:  0.9958166666666667
accuracy: test 0.9717
epoch:  29
accuracy train:  0.9962666666666666
accuracy: test 0.9717

2.5.2 迭代30次正确率绘图

  运行程序:

#正确率绘图
# matplotlib其实是不支持显示中文的 显示中文需要一行代码设置字体  
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'SimHei'  
plt.rcParams['axes.unicode_minus'] = False   import matplotlib.pyplot as plt x=np.arange(1,31,1)plt.title('迭代30次正确率')
plt.plot(x, train_acc, color='green', label='训练集')
plt.plot(x, test_acc, color='red', label='测试集')plt.legend() # 显示图例
plt.show()

  运行结果:

3 模型优化

3.1 调整神经元数量

3.1.1 每次epoch训练预测情况

  运行程序:

##更换隐藏神经元数量为50
epochs = 50
train_acc=[]
test_acc=[]
NN = NeuralNetwork(num_of_in_nodes = image_pixels, num_of_out_nodes = 10, num_of_hidden_nodes = 50,learning_rate = 0.1)for epoch in range(epochs):  print("epoch: ", epoch)for i in range(len(train_imgs)):NN.train(train_imgs[i], train_labels_one_hot[i])corrects, wrongs = NN.evaluate(train_imgs, train_labels)print("accuracy train: ", corrects / ( corrects + wrongs))train_acc.append(corrects / ( corrects + wrongs))corrects, wrongs = NN.evaluate(test_imgs, test_labels)print("accuracy: test", corrects / ( corrects + wrongs))test_acc.append(corrects / ( corrects + wrongs))

  运行结果:

epoch:  0
accuracy train:  0.93605
accuracy: test 0.935
epoch:  1
accuracy train:  0.95185
accuracy: test 0.9501
epoch:  2
accuracy train:  0.9570333333333333
accuracy: test 0.9526
epoch:  3
accuracy train:  0.9630833333333333
accuracy: test 0.9556
epoch:  4
accuracy train:  0.9640166666666666
accuracy: test 0.9556
epoch:  5
accuracy train:  0.9668333333333333
accuracy: test 0.957
epoch:  6
accuracy train:  0.96765
accuracy: test 0.957
epoch:  7
accuracy train:  0.9673166666666667
accuracy: test 0.9566
epoch:  8
accuracy train:  0.96875
accuracy: test 0.9559
epoch:  9
accuracy train:  0.97145
accuracy: test 0.957
epoch:  10
accuracy train:  0.974
accuracy: test 0.9579
epoch:  11
accuracy train:  0.9730666666666666
accuracy: test 0.9569
epoch:  12
accuracy train:  0.9730166666666666
accuracy: test 0.9581
epoch:  13
accuracy train:  0.9747666666666667
accuracy: test 0.959
epoch:  14
accuracy train:  0.9742166666666666
accuracy: test 0.9581
epoch:  15
accuracy train:  0.97615
accuracy: test 0.9596
epoch:  16
accuracy train:  0.9759
accuracy: test 0.9586
epoch:  17
accuracy train:  0.9773166666666666
accuracy: test 0.9596
epoch:  18
accuracy train:  0.9778833333333333
accuracy: test 0.9606
epoch:  19
accuracy train:  0.9789166666666667
accuracy: test 0.9589
epoch:  20
accuracy train:  0.9777333333333333
accuracy: test 0.9582
epoch:  21
accuracy train:  0.9774
accuracy: test 0.9573
epoch:  22
accuracy train:  0.9796166666666667
accuracy: test 0.9595
epoch:  23
accuracy train:  0.9792666666666666
accuracy: test 0.959
epoch:  24
accuracy train:  0.9804333333333334
accuracy: test 0.9591
epoch:  25
accuracy train:  0.9806
accuracy: test 0.9589
epoch:  26
accuracy train:  0.98105
accuracy: test 0.9596
epoch:  27
accuracy train:  0.9806833333333334
accuracy: test 0.9587
epoch:  28
accuracy train:  0.9809833333333333
accuracy: test 0.9595
epoch:  29
accuracy train:  0.9813333333333333
accuracy: test 0.9595

3.1.2 正确率绘图

  运行程序:

#正确率绘图
# matplotlib其实是不支持显示中文的 显示中文需要一行代码设置字体  
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'SimHei'  
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)  import matplotlib.pyplot as plt x=np.arange(1,31,1)plt.title('神经元数量为50时正确率')
plt.plot(x, train_acc, color='green', label='训练集')
plt.plot(x, test_acc, color='red', label='测试集')plt.legend() # 显示图例
plt.show()

  运行结果:

3.2 更换隐藏层层数

3.2.1 每次epoch训练预测情况

  运行程序:

#隐藏层层数为2
class NeuralNetwork:def __init__(self, num_of_in_nodes, #输入节点数num_of_out_nodes, #输出节点数num_of_hidden_nodes1,#隐藏第一层节点数num_of_hidden_nodes2,#隐藏第二层节点数learning_rate):#学习率self.num_of_in_nodes = num_of_in_nodesself.num_of_out_nodes = num_of_out_nodesself.num_of_hidden_nodes1 = num_of_hidden_nodes1self.num_of_hidden_nodes2 = num_of_hidden_nodes2self.learning_rate = learning_rate self.create_weight_matrices()#初始为一个隐藏节点    def create_weight_matrices(self):#创建权重矩阵#A method to initialize the weight #matrices of the neural network#一种初始化神经网络权重矩阵的方法rad = 1 / np.sqrt(self.num_of_in_nodes)  X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)  #形成指定分布self.weight_1 = X.rvs((self.num_of_hidden_nodes1, self.num_of_in_nodes)) #rvs:产生服从指定分布的随机数rad = 1 / np.sqrt(self.num_of_hidden_nodes1)X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)self.weight_2 = X.rvs((self.num_of_hidden_nodes2, self.num_of_hidden_nodes1)) #rvs: 产生服从指定分布的随机数rad = 1 / np.sqrt(self.num_of_hidden_nodes2)X = truncated_normal(mean=0, sd=1, low=-rad, upp=rad)self.weight_3 = X.rvs((self.num_of_out_nodes, self.num_of_hidden_nodes2)) #rvs: 产生服从指定分布的随机数def train(self, input_vector, target_vector):#input_vector and target_vector can #be tuple, list or ndarrayinput_vector = np.array(input_vector, ndmin=2).T#输入target_vector = np.array(target_vector, ndmin=2).T#输出output_vector1 = np.dot(self.weight_1, input_vector) #隐藏层值output_hidden1 = activation_function(output_vector1)#删除不激活output_vector2 = np.dot(self.weight_2, output_hidden1)#输出output_hidden2 = activation_function(output_vector2)#删除不激活output_vector3 = np.dot(self.weight_3, output_hidden2)#输出output_network = activation_function(output_vector3)##删除不激活# calculate output errors:计算输出误差output_errors = target_vector - output_network# update the weights:更新权重tmp = output_errors * output_network * (1.0 - output_network)     self.weight_3 += self.learning_rate  * np.dot(tmp, output_hidden2.T)hidden1_errors = np.dot(self.weight_3.T, output_errors)tmp = hidden1_errors * output_hidden2 * (1.0 - output_hidden2)     self.weight_2 += self.learning_rate  * np.dot(tmp, output_hidden1.T)# calculate hidden errors:计算隐藏层误差hidden_errors = np.dot(self.weight_2.T, hidden1_errors)# update the weights:tmp = hidden_errors * output_hidden1 * (1.0 - output_hidden1)self.weight_1 += self.learning_rate * np.dot(tmp, input_vector.T)#测试集def run(self, input_vector):# input_vector can be tuple, list or ndarrayinput_vector = np.array(input_vector, ndmin=2).Toutput_vector = np.dot(self.weight_1, input_vector)output_vector = activation_function(output_vector)output_vector = np.dot(self.weight_2, output_vector)output_vector = activation_function(output_vector)output_vector = np.dot(self.weight_3, output_vector)output_vector = activation_function(output_vector)return output_vector#判别矩阵def confusion_matrix(self, data_array, labels):cm = np.zeros((10, 10), int)for i in range(len(data_array)):res = self.run(data_array[i])res_max = res.argmax()target = labels[i][0]cm[res_max, int(target)] += 1return cm    #精确度def precision(self, label, confusion_matrix):col = confusion_matrix[:, label]return confusion_matrix[label, label] / col.sum()#评估def evaluate(self, data, labels):corrects, wrongs = 0, 0for i in range(len(data)):res = self.run(data[i])res_max = res.argmax()if res_max == labels[i]:corrects += 1else:wrongs += 1return corrects, wrongs##迭代30次
epochs = 30
train_acc=[]
test_acc=[]
NN = NeuralNetwork(num_of_in_nodes = image_pixels, num_of_out_nodes = 10, num_of_hidden_nodes1 = 100,num_of_hidden_nodes2 = 100,learning_rate = 0.1)for epoch in range(epochs):  print("epoch: ", epoch)for i in range(len(train_imgs)):NN.train(train_imgs[i], train_labels_one_hot[i])corrects, wrongs = NN.evaluate(train_imgs, train_labels)print("accuracy train: ", corrects / ( corrects + wrongs))train_acc.append(corrects / ( corrects + wrongs))corrects, wrongs = NN.evaluate(test_imgs, test_labels)print("accuracy: test", corrects / ( corrects + wrongs))test_acc.append(corrects / ( corrects + wrongs))

  运行结果:

epoch:  0
accuracy train:  0.8972333333333333
accuracy: test 0.9005
epoch:  1
accuracy train:  0.8891833333333333
accuracy: test 0.8936
epoch:  2
accuracy train:  0.9146833333333333
accuracy: test 0.9182
epoch:  3
D:\ananconda\lib\site-packages\ipykernel_launcher.py:5: RuntimeWarning: overflow encountered in power"""
accuracy train:  0.8974833333333333
accuracy: test 0.894
epoch:  4
accuracy train:  0.8924166666666666
accuracy: test 0.8974
epoch:  5
accuracy train:  0.91295
accuracy: test 0.914
epoch:  6
accuracy train:  0.9191166666666667
accuracy: test 0.9205
epoch:  7
accuracy train:  0.9117666666666666
accuracy: test 0.9162
epoch:  8
accuracy train:  0.9220333333333334
accuracy: test 0.9222
epoch:  9
accuracy train:  0.9113833333333333
accuracy: test 0.9112
epoch:  10
accuracy train:  0.9134333333333333
accuracy: test 0.911
epoch:  11
accuracy train:  0.9112166666666667
accuracy: test 0.9103
epoch:  12
accuracy train:  0.914
accuracy: test 0.9126
epoch:  13
accuracy train:  0.9206833333333333
accuracy: test 0.9214
epoch:  14
accuracy train:  0.90945
accuracy: test 0.9073
epoch:  15
accuracy train:  0.9225166666666667
accuracy: test 0.9287
epoch:  16
accuracy train:  0.9226
accuracy: test 0.9205
epoch:  17
accuracy train:  0.9239833333333334
accuracy: test 0.9202
epoch:  18
accuracy train:  0.91925
accuracy: test 0.9191
epoch:  19
accuracy train:  0.9223166666666667
accuracy: test 0.92
epoch:  20
accuracy train:  0.9113
accuracy: test 0.9084
epoch:  21
accuracy train:  0.9241666666666667
accuracy: test 0.925
epoch:  22
accuracy train:  0.9236333333333333
accuracy: test 0.9239
epoch:  23
accuracy train:  0.9301166666666667
accuracy: test 0.9259
epoch:  24
accuracy train:  0.9195166666666666
accuracy: test 0.9186
epoch:  25
accuracy train:  0.9200833333333334
accuracy: test 0.9144
epoch:  26
accuracy train:  0.9204833333333333
accuracy: test 0.9186
epoch:  27
accuracy train:  0.9288666666666666
accuracy: test 0.9259
epoch:  28
accuracy train:  0.9293
accuracy: test 0.9282
epoch:  29
accuracy train:  0.9254666666666667
accuracy: test 0.9242

3.2.2 正确率绘图

  运行程序:

#正确率绘图
# matplotlib其实是不支持显示中文的 显示中文需要一行代码设置字体  
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams['font.family'] = 'SimHei'  
plt.rcParams['axes.unicode_minus'] = False   # 步骤二(解决坐标轴负数的负号显示问题)  import matplotlib.pyplot as plt x=np.arange(1,31,1)plt.title('隐藏层数为2时正确率')
plt.plot(x, train_acc, color='green', label='训练集')
plt.plot(x, test_acc, color='red', label='测试集')plt.legend() # 显示图例
plt.show()

  运行结果:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/769070.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

鸿蒙APP应用开发教程—超详细的项目结构说明

1. 新建项目 打开DevEco Studio, 选择 Create Project: 1.1 选择模版 Create Project - Choose Template 1.2 配置项目 Create Project - Configure Project 如果使用的是 DevEco 3.X 版本, 可以根据 Compile SDK版本选择不同的模式, 比如: 3.0.0(API 8)及更早 - 仅支持 …

数字图像处理学习笔记(五)

数字图像处理学习笔记(五) 表示与描述表示链码最小周长多边形的多边形近似(MPP)标记边界片段骨骼(表示平面区域结构形状) SIFT原理(尺度不变特征变换匹配算法:Scale-invariant feature transform)SIFT算法分…

有名的爬虫框架 colly 的特性及2个详细采集案例

一. Colly概述 前言:colly 是 Go 实现的比较有名的一款爬虫框架,而且 Go 在高并发和分布式场景的优势也正是爬虫技术所需要的。它的主要特点是轻量、快速,设计非常优雅,并且分布式的支持也非常简单,易于扩展。 框架简…

Linux之时间子系统(四): tick 层模块(periodic 和dynamic )

一、时间子系统的软件架构 二、tick 层模块的文件 tick-common.c tick-oneshot.c tick-sched.c tick-broadcast.c tick-broadcast-hrtimer.c 这三个文件属于tick device layer。 tick-common.c文件是periodic tick模块,用于管理周期性tick事件。 tick-oneshot.c文…

ubuntu22.04物理机双系统手动分区

ubuntu22.04物理机双系统手动分区 文章目录 ubuntu22.04物理机双系统手动分区1. EFI系统分区2. 交换分区3. /根分区4. /home分区分区后的信息 手动分区顺序:EFI系统分区(/boot/efi)、交换分区(/swap)、/根分区、/home分区。 具体参数设置: 1. EFI系统分…

OpenHarmony使用智能指针管理动态分配内存对象

概述 智能指针是行为类似指针的类,在模拟指针功能的同时提供增强特性,如针对具有动态分配内存对象的自动内存管理等。 自动内存管理主要是指对超出生命周期的对象正确并自动地释放其内存空间,以避免出现内存泄漏等相关内存问题。智能指针对…

Vue复习

1. MVVM 模型 ● Model(模型):表示应用程序中的数据模型。它代表着应用程序中的业务逻辑和状态。 ● View(视图):表示应用程序的用户界面。它是用户与应用程序交互的方式。 ● ViewModel(视图模…

Docker 安装 Nginx 容器,反向代理

Docker官方镜像https://hub.docker.com/ 寻找Nginx镜像 下载Nginx镜像 docker pull nginx #下载最新版Nginx镜像 (其实此命令就等同于 : docker pull nginx:latest ) docker pull nginx:xxx #下载指定版本的Nginx镜像 (xxx指具体版本号)检查当前所有Docker下载的镜像 docker…

关于使用TCP-S7协议读写西门子PLC字符串的问题

我们可以使用TCP-S7协议读写西门子PLC, 比如PLC中定义一个String[50] 的地址DB300.20 地址DB300.20 DB块编号为300,偏移量【地址】是30 S7协议是西门子PLC自定义的协议,默认端口102,本质仍然是TCP协议的一种具体实现&#xff…

HMI界面之:医疗设备界面

一、什么是医疗HMI界面 医疗HMI界面是指医疗设备或系统中的人机界面(Human-Machine Interface),用于与医疗设备进行交互和操作的界面。它是医疗设备中的重要组成部分,通过图形化、直观化的界面,使医护人员能够方便地控…

短剧APP系统开发:探索短剧的发展机遇,提高收益

近年来,短剧在各大社交平台上快速发展,市场规模大幅度上升,成为了大众闲暇时光的娱乐的首选方式之一,深受大众的喜爱。 与传统的影视相比,短剧时间短、节奏快、剧情爽,让给观众更加容易“上头”。对于创业…

举4例说明Python如何使用正则表达式分割字符串

在Python中,你可以使用re模块的split()函数来根据正则表达式分割字符串。这个函数的工作原理类似于Python内置的str.split()方法,但它允许你使用正则表达式作为分隔符。 示例 1: 使用单个字符作为分隔符 假设你有一个由逗号分隔的字符串,你可…

力扣:205. 同构字符串

前言:剑指offer刷题系列 问题: 给定两个字符串 s 和 t ,判断它们是否是同构的。 如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。 每个出现的字符都应当映射到另一个字符,同时不改变字符…

【Lazy ORM 框架学习】

Gitee 点赞关注不迷路 项目地址 快速入门 模块所属层级描述快照版本正式版本wu-database-lazy-lambdalambda针对不同数据源wu-database-lazy-orm-coreorm 核心orm核心处理wu-database-lazy-sqlsql核心处理成处理sql解析、sql执行、sql映射wu-elasticsearch-starterESESwu-hb…

时间的守护者:无硫手指套的神奇传说

在钟表制造的世界里,有一个神奇的工具被誉为“精工制表良器”——那就是无硫手指套。这并不是一个普通的故事,而是一段讲述质量、技术和关怀的传奇。 很久以前,在一个钟表制造工坊里,技师们为了追求完美,不断地探索着提…

服务器被挖矿了怎么办,实战清退

当我们发现服务器资源大量被占用的时候,疑似中招了怎么办 第一时间重启服务是不行的,这些挖矿木马一定是会伴随着你的重启而自动重启,一定时间内重新霸占你的服务器资源 第一步检查高占用进程 top -c ps -ef 要注意这里%CPU,如果…

Linux操作系统及进程(三)进程优先级及特性

目录 一、优先级概念 二、查看系统进程 三、进程切换 一、优先级概念 1.cpu资源分配的先后顺序,就是指进程的优先权(priority)。 2.优先权高的进程有优先执行权利。配置进程优先权对多任务环境的linux很有用,可以改善系统性能。…

pinia的异步以及getter

getter定义 action异步 使用

PyQt:实现菜单栏的点击拖动效果

一、整体步骤 1.设计UI文件 2.调用显示 3.效果展示 二、设计UI文件 1.添加 Scroll Area控件,作为菜单栏的布置区域 2.设置 Scroll Area控件的属性 3.Scroll Area控件内放置 按钮控件 组成菜单栏 此处,放置了需要了6个按钮,并设置按钮的固…

跨境电商测评自养号需要解决哪些问题?

现在做测评工作室这块的,真正有技术的每天单都做不过来,同样也滋生出很多找别人买个设备和账号就以为自己懂了,直接开始教学来割韭菜,很多人没接触过这行业,不知道里面的水很深,花了钱,却没有掌…