神经网络入门学习中,进行了手写字体识别实践,该篇博客用于记录实践代码,以备后续使用。

关键词:神经网络,前向传播、反向传播、梯度下降、权值更新、手写字体识别

1. 实践代码

import numpy as np
from sklearn.datasets import load_digits
from sklearn.preprocessing import LabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt# 载入数据
digits = load_digits()
# 显示图片
for i in range(min(digits.images.shape[0], 2)):plt.imshow(digits.images[i], cmap='gray')plt.show()# 数据
X = digits.data
# 标签
y = digits.target# 定义一个神经网络,结构,64-100-
# 定义输入层到隐藏层之间的权值矩阵
V = np.random.random((64, 100)) * 2 - 1
# 定义隐藏层到输出层之间的权值矩阵
W = np.random.random((100, 10)) * 2 - 1# 数据切分
# 1/4为测试集,3/4为训练集
X_train, X_test, y_train, y_test = train_test_split(X, y)# 标签二值化
# 0 -> 1000000000
# 3 -> 0003000000
# 9 -> 0000000001
labels_train = LabelBinarizer().fit_transform(y_train)# 激活函数
def sigmoid(x):return 1 / (1 + np.exp(-x))# 激活函数的导数
def dsigmoid(x):return x * (1 - x)#  训练模型
def train(X, y, steps=10000, lr=0.011):global V, Wfor n in range(steps + 1):# 随机选取一个数据i = np.random.randint(X.shape[0])# 获取一个数据x = X[i]x = np.atleast_2d(x)# BP算法公式# 计算隐藏层的输出L1 = sigmoid(np.dot(x, V))# 计算输出层的输出L2 = sigmoid(np.dot(L1, W))# 计算L2_delta,L1_deltaL2_delta = (y[i] - L2) * dsigmoid(L2)L1_delta = L2_delta.dot(W.T) * dsigmoid(L1)# 更新权值W += lr * L1.T.dot(L2_delta)V += lr * x.T.dot(L1_delta)# 每训练1000次预测一次准确率if n % 1000 == 0:output = predict(X_test)predictions = np.argmax(output, axis=1)acc = np.mean(np.equal(predictions, y_test))dW = L1.T.dot(L2_delta)dV = x.T.dot(L1_delta)gradient = np.sum([np.sqrt(np.sum(np.square(j))) for j in [dW, dV]])print('steps', n, 'accuracy', acc, 'gradient', gradient)# print(classification_report(predictions,y_test))def predict(x):# 计算隐藏层的输出L1 = sigmoid(np.dot(x, V))# 计算输出层的输出L2 = sigmoid(np.dot(L1, W))return L2# 开始训练
train(X_train, labels_train, 20000, lr=0.11)
train(X_train, labels_train, 20000, lr=0.011)
# 训练后结果对比
output = predict(X_test)
predictions = np.argmax(output, axis=1)
acc = np.mean(np.equal(predictions, y_test))
print('accuracy', acc)
print(classification_report(predictions, y_test, digits=4))

2. 运行结果

steps 1000 accuracy 0.39555555555555555 gradient 1.1540293157343329
steps 2000 accuracy 0.5822222222222222 gradient 0.24060995286476866
steps 3000 accuracy 0.6822222222222222 gradient 0.5377184040200084
steps 4000 accuracy 0.7088888888888889 gradient 0.3105382024238891
steps 5000 accuracy 0.7422222222222222 gradient 0.6367500261759913
steps 6000 accuracy 0.8288888888888889 gradient 0.7925874111147599
steps 7000 accuracy 0.8311111111111111 gradient 0.4219865590227713
steps 8000 accuracy 0.8866666666666667 gradient 0.19398854322579862
steps 9000 accuracy 0.9088888888888889 gradient 0.059956981030795414
steps 10000 accuracy 0.94 gradient 0.060926927213079046
steps 11000 accuracy 0.9222222222222223 gradient 0.0940042676753109
steps 12000 accuracy 0.9488888888888889 gradient 0.007252581282821429
steps 13000 accuracy 0.9422222222222222 gradient 0.0005117412876631392
steps 14000 accuracy 0.9577777777777777 gradient 0.020721130471145828
steps 15000 accuracy 0.9177777777777778 gradient 0.16479326937586997
steps 16000 accuracy 0.9422222222222222 gradient 3.7564619456548263
steps 17000 accuracy 0.9644444444444444 gradient 0.06896903217933802
steps 18000 accuracy 0.9577777777777777 gradient 0.027949995263778806
steps 19000 accuracy 0.9688888888888889 gradient 0.0006993893497620089
steps 20000 accuracy 0.9622222222222222 gradient 0.20674307169673445
steps 0 accuracy 0.9622222222222222 gradient 0.00872206168693661
steps 1000 accuracy 0.9666666666666667 gradient 0.022782881906241878
steps 2000 accuracy 0.9666666666666667 gradient 0.014290994295273446
steps 3000 accuracy 0.9688888888888889 gradient 0.48748927217168325
steps 4000 accuracy 0.9666666666666667 gradient 0.005666292598739775
steps 5000 accuracy 0.9688888888888889 gradient 0.0443512717424438
steps 6000 accuracy 0.9688888888888889 gradient 0.0061561535761366
steps 7000 accuracy 0.9688888888888889 gradient 0.020829652956511197
steps 8000 accuracy 0.9688888888888889 gradient 0.19995065353583805
steps 9000 accuracy 0.9688888888888889 gradient 0.5650831905730958
steps 10000 accuracy 0.9688888888888889 gradient 0.003948413021913768
steps 11000 accuracy 0.9688888888888889 gradient 0.13861557486472081
steps 12000 accuracy 0.9688888888888889 gradient 0.010983126563059315
steps 13000 accuracy 0.9711111111111111 gradient 0.024410425339698696
steps 14000 accuracy 0.9711111111111111 gradient 0.001296347083650798
steps 15000 accuracy 0.9711111111111111 gradient 0.0018664511288084774
steps 16000 accuracy 0.9711111111111111 gradient 0.0032670292289213856
steps 17000 accuracy 0.9711111111111111 gradient 0.017069352701509097
steps 18000 accuracy 0.9688888888888889 gradient 0.0033675869477754346
steps 19000 accuracy 0.9688888888888889 gradient 0.00043959401991603385
steps 20000 accuracy 0.9688888888888889 gradient 0.09278100040345633
accuracy 0.9688888888888889precision    recall  f1-score   support0     1.0000    1.0000    1.0000        431     0.9787    1.0000    0.9892        462     1.0000    1.0000    1.0000        373     0.9787    0.9388    0.9583        494     0.9091    1.0000    0.9524        505     0.9800    0.9800    0.9800        506     1.0000    0.9375    0.9677        487     0.9565    1.0000    0.9778        448     0.9189    0.9714    0.9444        359     0.9767    0.8750    0.9231        48micro avg     0.9689    0.9689    0.9689       450macro avg     0.9699    0.9703    0.9693       450
weighted avg     0.9701    0.9689    0.9687       450

神经网络实现手写字体识别相关推荐

  1. 基于Python神经网络的手写字体识别

    本文将分享实现手写字体识别的神经网络实现,代码中有详细注释以及我自己的一些体会,希望能帮助到大家 (≧∇≦)/ ############################################ ...

  2. pytorch深度学习神经网络实现手写字体识别

    利用平pytorch搭建简单的神经网络实现minist手写字体的识别,采用三层线性函数迭代运算,使得其具备一定的非线性转化与运算能力,其数学原理如下: 其具体实现代码如下所示:import torch ...

  3. 卷积神经网络与循环神经网络实战 --- 手写数字识别及诗词创作

    卷积神经网络与循环神经网络实战 - 手写数字识别及诗词创作 文章目录 卷积神经网络与循环神经网络实战 --- 手写数字识别及诗词创作 一.神经网络相关知识 1. 深度学习 2. 人工神经网络回顾 3. ...

  4. 《MATLAB 神经网络43个案例分析》:第19章 基于SVM的手写字体识别

    <MATLAB 神经网络43个案例分析>:第19章 基于SVM的手写字体识别 1. 前言 2. MATLAB 仿真示例 3. 小结 1. 前言 <MATLAB 神经网络43个案例分析 ...

  5. 神经网络学习(二)Tensorflow-简单神经网络(全连接层神经网络)实现手写字体识别

    神经网络学习(二)神经网络-手写字体识别 框架:Tensorflow 1.10.0 数据集:mnist数据集 策略:交叉熵损失 优化:梯度下降 五个模块:拿数据.搭网络.求损失.优化损失.算准确率 一 ...

  6. MATLAB实现基于BP神经网络的手写数字识别+GUI界面+mnist数据集测试

    文章目录 MATLAB实现基于BP神经网络的手写数字识别+GUI界面+mnist数据集测试 一.题目要求 二.完整的目录结构说明 三.Mnist数据集及数据格式转换 四.BP神经网络相关知识 4.1 ...

  7. python手写字体程序_深度学习---手写字体识别程序分析(python)

    我想大部分程序员的第一个程序应该都是"hello world",在深度学习领域,这个"hello world"程序就是手写字体识别程序. 这次我们详细的分析下手 ...

  8. MNIST手写字体识别入门编译过程遇到的问题及解决

    MNIST手写字体识别入门编译过程遇到的问题及解决 以MNIST手写字体识别作为神经网络及各种网络模型的作为练手,将遇到的问题在这里记录与交流. 激活tensorflow环境后,运行spyder或者j ...

  9. 深度学习,实现手写字体识别(大数据人工智能公司)

    手写字体识别是指给定一系列的手写字体图片以及对应的标签,构建模型进行学习,目标是对于一张新的手写字体图片能够自动识别出对应的文字或数字.通过深度学习构建普通神经网络和卷积神经网络,处理手写字体数据.通 ...

最新文章

  1. 阿里技术专家推荐的几个公众号!
  2. 面了小 100 人,90% 都背了面试题......
  3. 新建并保存一个空的Excel
  4. 修改input file默认样式
  5. Web前端开发笔记——第二章 HTML语言 第一节 标签、元素、属性
  6. 致年轻开发人员的一封信
  7. Gartner: 2017年11大信息安全技术(解读版)
  8. java map 泛型 反射_Java通过反射读取泛型
  9. Unix EM乱码问题
  10. Linux htop工具使用详解
  11. oracle ORA-00001 违反唯一约束条件 SYS_C009225问题
  12. Oracle增量跟新
  13. 山西台达plc可编程控制器_(PLC)可编程控制器的编程语言你了解吗?不妨看看...
  14. 深度学习与自然语言处理 主要概念一览
  15. 活动目录(Active Directory)域故障解决实例(转载)
  16. ZOJ 3429 Cube Simulation (思维题)
  17. 电子元器件筛选公司/费用-电子元器件筛选方法与技术要求
  18. Java微服务实战项目推荐
  19. html 链接excel,如何把excel表格中的文本链接变成可点击打开的网址链接(超链接)?...
  20. 离散数学中 集合、关系、群 的证明方法(英文证明附例题)

热门文章

  1. 微信小程序-tempfilePath文件的上传问题
  2. 【day15】每日强训编程题——查找输入整数二进制中1的个数手套
  3. 【操作系统】RR算法(时间片轮转,假设时间片q=1)
  4. Docker容器中启动Arthas异常
  5. 美国散户从90降到6他们是如何被消灭
  6. RealSync Quest SharePlex 技术对比
  7. reformer代码阅读
  8. jquery单独元素实现全屏显示
  9. 7-2 货币转换 (10 分)
  10. J2SE在线中文API