一、步骤:

  1. 导入包以及读取数据

  2. 设置参数

  3. 数据预处理

  4. 构建模型

  5. 编译模型

  6. 训练以及测试模型

二、代码:

1、导入包以及读取数据

#导入包
import numpy as np
np.random.seed(1337) #设置之后每次执行代码,产生的随机数都一样 from tensorflow.examples.tutorials.mnist import input_data
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import SimpleRNN , Activation , Dense
from keras.optimizers import Adam #读取数据
mnist = input_data.read_data_sets('E:\jupyter\TensorFlow\MNIST_data',one_hot = True)
X_train = mnist.train.images
Y_train = mnist.train.labels
X_test = mnist.test.images
Y_test = mnist.test.labels

2、设置参数

#设置参数
time_steps = 28 # same as the height of the image
input_size = 28 # same as the width of the image
batch_size = 50
batch_index = 0
output_size = 10
cell_size = 50
lr = 0.001

3、数据预处理

#数据预处理
X_train = X_train.reshape(-1,28,28)/255
X_test = X_test.reshape(-1,28,28)/255

4.1、构建RNN模型

#构建模型
model = Sequential() #RNN层
model.add(SimpleRNN(
batch_input_shape =(None,time_steps,input_size), # 输入维度
output_dim = cell_size, #输出维度
)) #输出层
model.add(Dense(output_size))
model.add(Activation('softmax'))

4.2、构建LSTM模型

def builtLSTMModel(time_steps,input_size,cell_size,output_size):
model = Sequential() #添加LSTM
model.add(LSTM(
batch_input_size = (None,time_steps,input_size),
output_dim = cell_size,
return_sequences = True, #要不要每个时间点的输出都输出
stateful = True, #batch和batch有联系,batch和batch之间的状态需要连接起来
)) #添加输出层
model.add(TimeDistributed(Dense(output_size))) #每一个时间点的输出都要加入全连接层。
return model

5、训练模型以及测试

#训练模型
for step in range(4001): X_batch = X_train[batch_index:batch_size + batch_index,:,:]
Y_batch = Y_train[batch_index:batch_size + batch_index,:]
cost = model.train_on_batch(X_batch,Y_batch) batch_index += batch_size
batch_index = 0 if batch_index >= X_train.shape[0] else batch_index if step % 500 ==0:
loss , acc = model.evaluate(X_test,Y_test,batch_size =Y_test.shape[0])
print(loss,',',acc)

莫烦大大keras学习Mnist识别(4)-----RNN的更多相关文章

  1. 莫烦大大keras学习Mnist识别(3)-----CNN

    一.步骤: 导入模块以及读取数据 数据预处理 构建模型 编译模型 训练模型 测试 二.代码: 导入模块以及读取数据 #导包 import numpy as np np.random.seed(1337 ...

  2. 莫烦大大keras的Mnist手写识别(5)----自编码

    一.步骤: 导入包和读取数据 数据预处理 编码层和解码层的建立 + 构建模型 编译模型 训练模型 测试模型[只用编码层来画图] 二.代码: 1.导入包和读取数据 #导入相关的包 import nump ...

  3. 莫烦大大TensorFlow学习笔记(9)----可视化

      一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...

  4. 莫烦python教程学习笔记——总结篇

    一.机器学习算法分类: 监督学习:提供数据和数据分类标签.--分类.回归 非监督学习:只提供数据,不提供标签. 半监督学习 强化学习:尝试各种手段,自己去适应环境和规则.总结经验利用反馈,不断提高算法 ...

  5. 莫烦大大TensorFlow学习笔记(8)----优化器

    一.TensorFlow中的优化器 tf.train.GradientDescentOptimizer:梯度下降算法 tf.train.AdadeltaOptimizer tf.train.Adagr ...

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

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

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

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

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

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

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

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

随机推荐

  1. JAVA大数据项目+整理的Mysql数据库32条军规

    http://www.jianshu.com/users/a9b2d43bb94e/latest_articles

  2. 通过指针访问C++对象的私有成员

    C++对象的私有成员是禁止类外的访问的.但是我们仍然可以通过指针访问C++对象的私有成员. #include <iostream> using namespace std; class A ...

  3. What is the difference between task and thread?

    http://stackoverflow.com/questions/4130194/what-is-the-difference-between-task-and-thread 回答一: A tas ...

  4. bzoj3555 [Ctsc2014]企鹅QQ——字符串哈希

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3555 很久以前就讲过哈希,但一直没写过题,所以这是哈希第一题! 哈希就是把一个字符串映射成一 ...

  5. django自带权限控制系统的使用和分析

    1.django的权限控制相关表及其相互间的关系: django的所有权限信息存放在auth_permission表中,用户user和用户组group都可以有对应的权限permission.分别存放在 ...

  6. php的string编码类型

    在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ...

  7. hdu1429胜利大逃亡(bfs)

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  8. B - String Task

    Problem description Petya started to attend programming lessons. On the first lesson his task was to ...

  9. C#Cookie操作类,删除Cookie,给Cookie赋值

    using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security ...

  10. mvc action 缓存的清楚更新办法

    https://www.cnblogs.com/waynechan/p/3232672.html