一、步骤:

  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. HDU 4532

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  2. Raphaeljs入门到精通(二)

    这节我们将介绍Raphaeljs中元素的属性和事件,案例还是以上一篇的代码展开 <!DOCTYPE html> <html xmlns="http://www.w3.org ...

  3. 用sp_executesql执行动态SQL语句及获得返回值

    过去我执行拼凑出来的动态SQL语句,都直接使用EXEC @sql 的方式.有好几次,都看到有资料说,应该尽量使用 sp_executesql. 究其原因,是因为仅仅参数不同的情况下,sp_execut ...

  4. luogu3834 【模板】可持久化线段树1(主席树)

    关键字:线段树 可持久化 线段树:当版本(即对应的原序列的区间[1,r])一定时,每个节点的left,right下标为值域,值为其对应的原序列区间[1,r]中元素大小在值域中的元素个数. 可持久化:新 ...

  5. oc37--类工厂方法

    // // Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @property int ag ...

  6. local_response_normalization 和 batch_normalization

    Normalization   Normalization local_response_normalization local_response_normalization出现在论文”ImageNe ...

  7. Codeforces--630I--Parking Lot(规律)

     I - Parking Lot Crawling in process... Crawling failed Time Limit:500MS     Memory Limit:65536KB  ...

  8. Java.HttpClient绕过Https证书解决方案一

    方案1 import javax.net.ssl.*; import java.io.*; import java.net.URL; import java.security.KeyManagemen ...

  9. CALayer(一)

    CALayer CALayer和UIView CALayer和UIView相比--CALayer少了事件处理的功能,所以更加轻量级,性能更好一点,这就说明如果有一些和用户交互的东西是不建议用CALay ...

  10. ThinkPHP __PUBLIC__的定义 __ROOT__等 常用 常量的定义

    '__TMPL__' => APP_TMPL_PATH, // 项目模板目录 '__ROOT__' => __ROOT__, // 当前网站地址 '__APP__' => __APP ...