莫烦大大keras学习Mnist识别(4)-----RNN
一、步骤:
导入包以及读取数据
设置参数
数据预处理
构建模型
编译模型
训练以及测试模型
二、代码:
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的更多相关文章
- 莫烦大大keras学习Mnist识别(3)-----CNN
一.步骤: 导入模块以及读取数据 数据预处理 构建模型 编译模型 训练模型 测试 二.代码: 导入模块以及读取数据 #导包 import numpy as np np.random.seed(1337 ...
- 莫烦大大keras的Mnist手写识别(5)----自编码
一.步骤: 导入包和读取数据 数据预处理 编码层和解码层的建立 + 构建模型 编译模型 训练模型 测试模型[只用编码层来画图] 二.代码: 1.导入包和读取数据 #导入相关的包 import nump ...
- 莫烦大大TensorFlow学习笔记(9)----可视化
一.Matplotlib[结果可视化] #import os #os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' import tensorflow as tf i ...
- 莫烦python教程学习笔记——总结篇
一.机器学习算法分类: 监督学习:提供数据和数据分类标签.--分类.回归 非监督学习:只提供数据,不提供标签. 半监督学习 强化学习:尝试各种手段,自己去适应环境和规则.总结经验利用反馈,不断提高算法 ...
- 莫烦大大TensorFlow学习笔记(8)----优化器
一.TensorFlow中的优化器 tf.train.GradientDescentOptimizer:梯度下降算法 tf.train.AdadeltaOptimizer tf.train.Adagr ...
- 莫烦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 ...
随机推荐
- Android 应用启动动画代码
requestWindowFeature(Window.FEATURE_NO_TITLE);//设置无标题 setContentView(R.layout.activity_main); getWin ...
- Rust 中项目构建管理工具 Cargo简单介绍
cargo是Rust内置的项目管理工具.用于Rust 项目的创建.编译.执行,同一时候对项目的依赖进行管理,自己主动推断使用的第三方依赖库,进行下载和版本号升级. 一.查看 cargo 版本号 安装R ...
- java wait 与 notify sleep
来自:http://blog.csdn.net/zyplus/article/details/6672775 有适当的代码修改. 在JAVA中,是没有类似于PV操作.进程互斥等相关的方法的.JAVA的 ...
- [ACM] ZOJ 3819 Average Score (水题)
Average Score Time Limit: 2 Seconds Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...
- tolua reference
Using Lua API and tag method facilities, tolua maps C/C++ constants, external variables, functions, ...
- SQL数据库问题 解释一下下面的代码 sql 存储过程学习
SQL数据库问题 解释一下下面的代码 2008-08-13 11:30wssqyl2000 | 分类:数据库DB | 浏览1154次 use mastergocreate proc killspid( ...
- kafka的topic和分区策略——log entry和消息id索引文件
Topic在逻辑上可以被认为是一个在的queue,每条消费都必须指定它的topic,可以简单理解为必须指明把这条消息放进哪个queue里. 为了使得Kafka的吞吐率可以水平扩展,物理上把topic分 ...
- hdu 2988(最小生成树 kruskal算法)
Dark roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- c++ 写进文件并读出
#include <iostream>#include <fstream>#include <vector>#include <string> int ...
- 75.培训管理-培训信息发布 Extjs 页面
1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&quo ...