当前位置:网站首页>12.RNN应用于手写数字识别
12.RNN应用于手写数字识别
2022-07-07 23:11:00 【booze-J】
代码运行平台为jupyter-notebook,文章中的代码块,也是按照jupyter-notebook中的划分顺序进行书写的,运行文章代码,直接分单元粘入到jupyter-notebook即可。整体代码给出的注释还是挺简单明了的。这里我们以使用
SimpleRNN
为例。 1.导入第三方库
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.layers.recurrent import SimpleRNN
from tensorflow.keras.optimizers import Adam
2.加载数据及数据预处理
# 载入数据
# 数据长度-一行有28个像素
input_size=28
# 序列长度-一共有28行
time_steps=28
# 隐藏层cell个数
cell_size=50
# 载入数据
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000,28,28)
x_train = x_train/255.0
x_test = x_test/255.0
# 换one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
3.训练模型
# 创建模型
model = Sequential()
# 循环神经网络
model.add(SimpleRNN(
units=cell_size,# 输出
input_shape=(time_steps,input_size),# 输入
))
# 输出层
model.add(Dense(10,activation="softmax"))
# 定义优化器 设置学习率为1e-4
adam = Adam(lr=1e-4)
# 定义优化器,loss function,训练过程中计算准确率
model.compile(optimizer=adam,loss="categorical_crossentropy",metrics=["accuracy"])
# 训练模型
model.fit(x_train,y_train,batch_size=64,epochs=10)
# 评估模型
loss,accuracy=model.evaluate(x_test,y_test)
print("test loss:",loss)
print("test accuracy:",accuracy)
代码运行结果:
代码中需要注意的一些点,在代码注释中也给出了解释和提醒。从运行结果中可以看到RNN训练出来的模型在测试集上的准确率相对于10.CNN应用于手写数字识别中CNN训练出来的模型在测试集上的准确率效果要更差一些。
边栏推荐
- ReentrantLock 公平锁源码 第0篇
- 【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
- Experience of autumn recruitment in 22 years
- 大数据开源项目,一站式全自动化全生命周期运维管家ChengYing(承影)走向何方?
- Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
- Deep dive kotlin synergy (XXII): flow treatment
- 玩轉Sonar
- Summary of weidongshan phase II course content
- Is it safe to open an account on the official website of Huatai Securities?
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
猜你喜欢
Play sonar
国外众测之密码找回漏洞
RPA云电脑,让RPA开箱即用算力无限?
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
赞!idea 如何单窗口打开多个项目?
"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points
基于人脸识别实现课堂抬头率检测
C language 001: download, install, create the first C project and execute the first C language program of CodeBlocks
5g NR system messages
随机推荐
Service Mesh的基本模式
How to learn a new technology (programming language)
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
LeetCode刷题
[C language] objective questions - knowledge points
NVIDIA Jetson测试安装yolox过程记录
他们齐聚 2022 ECUG Con,只为「中国技术力量」
NVIDIA Jetson test installation yolox process record
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation
FOFA-攻防挑战记录
tourist的NTT模板
基于卷积神经网络的恶意软件检测方法
《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
Class head up rate detection based on face recognition
Huawei switch s5735s-l24t4s-qa2 cannot be remotely accessed by telnet
ReentrantLock 公平锁源码 第0篇
新库上线 | CnOpenData中国星级酒店数据
基于微信小程序开发的我最在行的小游戏