当前位置:网站首页>【深度学习实践(二)】上手手写数字识别
【深度学习实践(二)】上手手写数字识别
2022-08-04 05:39:00 【梦想new的出来】
活动地址:CSDN21天学习挑战赛
学习的最大理由是想摆脱平庸,早一天就多一份人生的精彩;迟一天就多一天平庸的困扰。
热爱写作,愿意让自己成为更好的人…
【深度学习实践(二)】上手手写数字识别
1 设置运行设备
tf.config.set_visible_devices([gpu0],"GPU")
2 加载数据集
先得到训练数据集,这里使用MNIST手写数字数据集,该数据集来源于美国国家标准与技术研究所,是著名的公开数据集之一。数据集中的数字图片是由250个不同职业的人纯手写绘制,如下图所示:
datasets.mnist.load_data()
- 打印出一部分训练数据查看
3 构建神经网络模型并进行训练
图片识别的原理就是 每张图可以看成是含有28*28的像素,然后转化为向量的形式就是 (1,784),数据训练集中一共含有60000张图片,那么向量组可以表示为(60000,784)
然后构建神经网络模型,通过卷积层将数据进行压缩**,池化层**进行数据(图像特征)的进一步抽样,以达到减少训练量的目的,最后全连接层 起到“特征提取器”的作用,输出层进行输出
将构建好的模型 通过优化,损失函数,性能评估,然后进行训练,通过反馈自行调整模型
#调整数据到我们需要的格式
train_images = train_images.reshape((60000, 28, 28, 1))
test_images = test_images.reshape((10000, 28, 28, 1))
train_images.shape,test_images.shape,train_labels.shape,test_labels.shape
""" 输出:((60000, 28, 28, 1), (10000, 28, 28, 1), (60000,), (10000,)) """
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)), # 卷积层1,卷积核3*3
layers.MaxPooling2D((2, 2)), # 池化层1,2*2采样
layers.Conv2D(64, (3, 3), activation='relu'), # 卷积层2,卷积核3*3
layers.MaxPooling2D((2, 2)), # 池化层2,2*2采样
layers.Flatten(), # Flatten层
layers.Dense(64, activation='relu'), # 全连接层
layers.Dense(10) # 输出层
])
# 打印网络结构
model.summary()
- 编译与训练模型
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
history = model.fit(train_images, train_labels, epochs=10,
validation_data=(test_images, test_labels))
4 预测结果
将训练好的模型 进行预测,输入一张图片,这里选择image[3],预测后得到图像特征结果(1,10)的一个向量
plt.imshow(test_images[3])
plt.show()
newT=test_images[3].reshape(1,784)
print('Test图片向量化为:'+str(newT))
print('预测ing...')
resT=test_images[3].reshape(1,28,28,1)
pre=model.predict(resT)
print('即得到预测结果'+str(pre))
学习随笔
1,学习的收货
动手实践深度学习,搭建神经网络,预测模型结果,体验炼丹入门的快乐
2,学习遇到的问题
提示:关于在Pycharm上想要通过qt窗口出图时,需要将设置plt.show(block=True)此时程序会在此被阻塞,当绘图窗口关闭后才会继续往下运行
边栏推荐
- Database: Organize Four Practical SQL Server Scripting Functions
- Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案
- 为什么不使用VS管理QT项目
- Hardware Knowledge: Introduction to RTMP and RTSP Traditional Streaming Protocols
- 反射与枚举
- A semi-supervised Laplace skyhawk optimization depth nuclear extreme learning machine for classification
- Gramm Angle field GAF time-series data into the image and applied to the fault diagnosis
- golang rtsp拉流测试
- EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)
- unicloud 腾讯云 上传文件 Have no access right to the storage uniapp
猜你喜欢
SegNet——论文笔记
Network skills: teach you to install batteries on the router, you can still surf the Internet when the power is cut off!
基于爬行动物搜索RSA优化LSTM的时间序列预测
SQL去重的三种方法汇总
Detailed explanation of DenseNet and Keras reproduction code
matlab科研绘图模板,直接奉上源代码!
MySQL(4)
MySQL大总结
如何用matlab做高精度计算?【第一辑】
Computer knowledge: desktop computers should choose the brand and assembly, worthy of collection
随机推荐
MySQL大总结
2DCNN, 1DCNN, BP, SVM fault diagnosis and result visualization of matlab
MATLAB版量化交易技术分析工具TA-Lib【不付费也可获取,不要被付费吓跑】
idea使用@Autowired注解爆红原因及解决方法
Jenkins pipeline 自动部署实践
数据库技巧:整理SQLServer非常实用的脚本
CMDB 腾讯云部分实现
[漏洞问题] log4j漏洞 关于2.17.0升级到2.18.0 方案
IDEA中创建编写JSP
RHCE之路----全
Database: Organize Four Practical SQL Server Scripting Functions
matlab封闭曲线拟合 (针对一些列离散点)
【C# - 爬虫】使用Selenium实现爬虫,获取近七天天气信息(包含完整代码)
RuntimeError: You called this URL via POST, but the URL doesn‘t end in a slash and you have APPEND_S
EfficientNet解读:神经网络的复合缩放方法(基于tf-Kersa复现代码)
异步编程之promise,任务队列,事件循环
SQL去重的三种方法汇总
软件稳定性思考
微软电脑管家2.0公测版体验
手把手教你Charles抓包工具使用