莫烦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的更多相关文章

  1. 莫烦python教程学习笔记——保存模型、加载模型的两种方法

    # View more python tutorials on my Youtube and Youku channel!!! # Youtube video tutorial: https://ww ...

  2. 莫烦python教程学习笔记——validation_curve用于调参

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  3. 莫烦python教程学习笔记——learn_curve曲线用于过拟合问题

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  4. 莫烦python教程学习笔记——利用交叉验证计算模型得分、选择模型参数

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  5. 莫烦python教程学习笔记——数据预处理之normalization

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  6. 莫烦python教程学习笔记——线性回归模型的属性

    #调用查看线性回归的几个属性 # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg # ...

  7. 莫烦python教程学习笔记——使用波士顿数据集、生成用于回归的数据集

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  8. 莫烦python教程学习笔记——使用鸢尾花数据集

    # View more python learning tutorial on my Youtube and Youku channel!!! # Youtube video tutorial: ht ...

  9. 莫烦python课程里面的bug修复;课程爬虫小练习爬百度百科

    我今天弄了一下午修改这个代码,最后还是弄好了.原因是正则表达式的筛选不够准确,有时候是会带http:baidu这些东西的.所以需要一个正则表达式的断言,然后还有一点是如果his里面只有一个元素就不要再 ...

  10. Tensorflow 教程系列 | 莫烦Python

    Tensorflow 简介 1.1 科普: 人工神经网络 VS 生物神经网络 1.2 什么是神经网络 (Neural Network) 1.3 神经网络 梯度下降 1.4 科普: 神经网络的黑盒不黑 ...

随机推荐

  1. 【多线程】交替输出abc

    package com.xf; public class WaitNotify { // 等待标记 private int flag; // 循环次数 private final int loopNu ...

  2. 学习Java Day14

    今天进一步学习了Java的类,学习了LocalDay:

  3. uboot启动过程 1

    打开 u-boot.lds OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-li ...

  4. 记一次 .NET 某医保平台 CPU 爆高分析

    一:背景 1. 讲故事 一直在追这个系列的朋友应该能感受到,我给这个行业中无数的陌生人分析过各种dump,终于在上周有位老同学找到我,还是个大妹子,必须有求必应 . 妹子公司的系统最近在某次升级之后, ...

  5. [USACO17JAN]Promotion Counting P

    题目大意 大小为 \(n\) 以 \(1\) 为根的树,点带权,求每个子树内大于本点的点的数量 \(1 \le n \le 10^5,1 \le p_i \le 10^9\) 题解 一眼静态链分治,然 ...

  6. zerologon复现

    zerologon漏洞复现 目录 zerologon漏洞复现 漏洞介绍 实验环境 实验开始 一.在github上下载脚本 二.检测是否可以利用 三.利用漏洞 1.清空域控账号密码 2.导出admin凭 ...

  7. 题解 P2602 [ZJOI2010] 数字计数

    虽然说是数位 dp 入门题但是还是不是很会(悲 看了题解才做出来,中途因为各种 SB 问题调了很长时间(悲 聪明的你一定能看出来这是数位 dp,因此令 \(i\) 为当前填的位数,\(limit\) ...

  8. 关于php imagettftext 函数错误解决问题

    imagettftext 这个函数是给图片添加水印的,但是不知道为什么我用不起,直到在网上找到了答案: 是因为字体文件路径原因,相对路径可能我位置不对,该成绝对路径就没问题了! 把'Facon-2.t ...

  9. K8S 1.20 弃用 Docker 评估之 Docker CLI 的替代产品

    title: K8S 1.20 弃用 Docker 评估之 Docker CLI 的替代产品 tags: - Docker - K8S - OCI - 容器 - 最佳实践 - RedHat - Sko ...

  10. 自己动手从零写桌面操作系统GrapeOS系列教程——8.x86介绍

    由于GrapeOS目前只支持x86,所以本讲我们简单的介绍一下x86. 百度百科上是这样介绍的:x86泛指一系列基于Intel 8086且向后兼容的中央处理器指令集架构. 中央处理器就是我们平时说的C ...