莫烦Python 4
莫烦Python 4

RNN Classifier 循环神经网络
问题描述
使用RNN对MNIST里面的图片进行分类
关键
SimpleRNN()参数
- batch_input_shape
使用状态RNN的注意事项
可以将RNN设置为‘stateful’,意味着由每个batch计算出的状态都会被重用于初始化下一个batch的初始状态。状态RNN假设连续的两个batch之中,相同下标的元素有一一映射关系。
要启用状态RNN,请在实例化层对象时指定参数stateful=True,并在Sequential模型使用固定大小的batch:通过在模型的第一层传入batch_size=(…)和input_shape来实现。在函数式模型中,对所有的输入都要指定相同的batch_size。
如果要将循环层的状态重置,请调用.reset_states(),对模型调用将重置模型中所有状态RNN的状态。对单个层调用则只重置该层的状态。
(samples,timesteps,input_dim)
代码
'''
RNN Classifier 循环神经网络
'''
import numpy as np
np.random.seed(1337)
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import SimpleRNN, Activation, Dense
from keras.optimizers import Adam
time_step = 28
input_size = 28
batch_size = 50
output_size = 10
cell_size = 50
LR = 0.001
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28) / 255. # normalize
X_test = X_test.reshape(-1, 28, 28) / 255. # normalize
y_train = np_utils.to_categorical(y_train, num_classes=10)
y_test = np_utils.to_categorical(y_test, num_classes=10)
model = Sequential()
model.add(
SimpleRNN(
batch_input_shape=(None, time_step, input_size),
units=cell_size
)
)
model.add(
Dense(output_size)
)
model.add(Activation('softmax'))
adam = Adam(LR)
model.compile(
optimizer=adam,
loss='categorical_crossentropy',
metrics=['accuracy']
)
model.summary()
model.fit(X_train, y_train, batch_size=batch_size, epochs=2, verbose=2, validation_data=(X_test, y_test))
结果
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
simple_rnn_2 (SimpleRNN) (None, 50) 3950
_________________________________________________________________
dense_2 (Dense) (None, 10) 510
_________________________________________________________________
activation_2 (Activation) (None, 10) 0
=================================================================
Total params: 4,460
Trainable params: 4,460
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples, validate on 10000 samples
Epoch 1/2
- 12s - loss: 0.6643 - accuracy: 0.7966 - val_loss: 0.4501 - val_accuracy: 0.8550
Epoch 2/2
- 9s - loss: 0.3220 - accuracy: 0.9087 - val_loss: 0.2445 - val_accuracy: 0.9359
莫烦Python 4的更多相关文章
- 莫烦python教程学习笔记——保存模型、加载模型的两种方法
# View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...
- 莫烦python教程学习笔记——validation_curve用于调参
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python教程学习笔记——learn_curve曲线用于过拟合问题
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python教程学习笔记——数据预处理之normalization
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python教程学习笔记——线性回归模型的属性
#调用查看线性回归的几个属性 # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg # ...
- 莫烦python教程学习笔记——使用波士顿数据集、生成用于回归的数据集
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python教程学习笔记——使用鸢尾花数据集
# View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...
- 莫烦python课程里面的bug修复;课程爬虫小练习爬百度百科
我今天弄了一下午修改这个代码,最后还是弄好了.原因是正则表达式的筛选不够准确,有时候是会带http:baidu这些东西的.所以需要一个正则表达式的断言,然后还有一点是如果his里面只有一个元素就不要再 ...
- Tensorflow 教程系列 | 莫烦Python
Tensorflow 简介 1.1 科普: 人工神经网络 VS 生物神经网络 1.2 什么是神经网络 (Neural Network) 1.3 神经网络 梯度下降 1.4 科普: 神经网络的黑盒不黑 ...
随机推荐
- 12月22日内容总结——django中间件的三个了解要求的方法、基于django中间件的功能设计、cookie与session
目录 一.django中间件三个了解的方法 二.django中间件五个方法的执行流程详解 三.基于django中间件的功能设计 功能设计介绍 如何利用字符串导入模块 功能模拟 四.cookie与ses ...
- nginx微信对接
location /MP_verify_l47ZUDtvieeDlVWR.txt { default_type text/html; return 200 'l47ZUDtvieeDlVWR'; }
- spring security登录认证流程解析
转 https://blog.csdn.net/qq_37142346/article/details/80032336 1.说明 用户认证流程 认证结果如何在多个请求之间共享 获取认证用户信息.用户 ...
- docker02-centos上安装
1.前提说明Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3.10 以上.Docker 运行在 CentOS-6.5 或更高的版本的 CentOS 上,要求系统为64位 ...
- Could not execute query ---> MySql.Data.MySqlClient.MySqlException: You have an error in your SQL sy
1.出现问题 执行sql查询时出现如题错误,原因是安装mysql-connector-net的版本过高,当前项目在用的mysql版本不符合:关于当前安装的mysql-connector-net版本的查 ...
- git操作出现 error: The following untracked working tree files would be overwritten by ...
命令行:git clean -d -fx,作用是:删除没有git add 的文件 ,执行之后解决了 error: The following untracked working tree files ...
- 研发效能DevOps推荐书单
专注 300 页之内的经典书籍推荐 研发效能涉及的知识很多,从大的方向去划分包括制度.组织.平台.运营等:单从软件研发的角度去看也包括很多,包括最底层的软工认知.实践,到团队管理和组织.敏捷研发,项目 ...
- JZOJ 2114. 【GDKOI2011】反恐任务
\(\text{Problem}\) 给定一张无向图,\(q\) 次询问,删去一个点或一条相邻两点间的边,问两点是否连通 询问独立 \(\text{Solution}\) 明显的用圆方树把图变成树 然 ...
- JZOJ 2022.01.21【提高A组】模拟
简要题解加心得 不得不说这是我打得比较痛苦且改得比较痛苦的一套题了 \(\text{T1 1085. [GDOI2008]彩球游戏}\) 整整改了三个半小时 直接崩溃了 明明本地可以跑过去,偏偏 \( ...
- 个人博客系统Typecho情侣主题模板Cupid
个人博客系统Typecho情侣主题模板Cupid 转载:https://zcjun.com/3175.html