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

简介: 【Python实战】——神经网络识别手写数字(三)

【Python实战】——神经网络识别手写数字(一)+https://developer.aliyun.com/article/1506500

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()

  运行结果:

【Python实战】——神经网络识别手写数字(三)+https://developer.aliyun.com/article/1506502

相关文章
|
3天前
|
Python Windows
|
4天前
|
运维 知识图谱 Python
专为运维工程师设计!阿里藏经阁出品的Python实战手册被我搞来了
Python 可能是极少数既简单又强大的编程语言中的一种。更重要的是,用它来编程是非常快乐的事。 今天给小伙伴们分享的是阿里“藏经阁”出品的专门给运维工程师设计的Python实战手册
|
4天前
|
机器学习/深度学习 人工智能 算法
【坚果识别】果实识别+图像识别系统+Python+计算机课设+人工智能课设+卷积算法
坚果识别系统,使用Python语言进行开发,通过TensorFlow搭建卷积神经网络算法模型,对10种坚果果实('杏仁', '巴西坚果', '腰果', '椰子', '榛子', '夏威夷果', '山核桃', '松子', '开心果', '核桃')等图片数据集进行训练,得到一个识别精度较高的模型文件,让后使用Django搭建Web网页端界面操作平台,实现用户上传一张坚果图片 识别其名称。
9 0
|
5天前
|
数据采集 Python
揭秘淘宝商品信息:Python爬虫技术入门与实战指南
Python爬虫用于获取淘宝商品详情,依赖`requests`和`beautifulsoup4`库。安装这两个库后,定义函数`get_taobao_product_details`,发送GET请求模拟浏览器,解析HTML获取标题和价格。注意选择器需随页面结构更新,遵守爬虫政策,控制请求频率,处理异常,且数据只能用于合法目的。
|
6天前
|
Java UED Python
Python多线程编程实战技巧与性能优化策略
Python多线程编程实战技巧与性能优化策略
|
8天前
|
数据采集 XML 存储
【Python实战】Python多线程批量采集图片
【Python实战】Python多线程批量采集图片
|
8天前
|
数据采集 XML 存储
【Python实战】Python采集二手车数据——超详细讲解
【Python实战】Python采集二手车数据——超详细讲解
|
24天前
|
机器学习/深度学习 PyTorch 算法框架/工具
【从零开始学习深度学习】26.卷积神经网络之AlexNet模型介绍及其Pytorch实现【含完整代码】
【从零开始学习深度学习】26.卷积神经网络之AlexNet模型介绍及其Pytorch实现【含完整代码】
|
24天前
|
机器学习/深度学习 PyTorch 算法框架/工具
【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】
【从零开始学习深度学习】28.卷积神经网络之NiN模型介绍及其Pytorch实现【含完整代码】
|
5天前
|
机器学习/深度学习 编解码 数据可视化
图神经网络版本的Kolmogorov Arnold(KAN)代码实现和效果对比
目前我们看到有很多使用KAN替代MLP的实验,但是目前来说对于图神经网络来说还没有类似的实验,今天我们就来使用KAN创建一个图神经网络Graph Kolmogorov Arnold(GKAN),来测试下KAN是否可以在图神经网络方面有所作为。
26 0