Keras【Deep Learning With Python】RNN Classifier 循环神经网络
文章目录
- 1 前言
- 2 RNN-循环神经网络
- 2.1 序列数据
- 2.2 处理序列数据的神经网络
- 2.3 应用
- 3 代码实现
- 4 代码讲解
- 5 输出
1 前言
本文分为RNN简单讲解,与Keras快速搭建RNN网络两部分。
2 RNN-循环神经网络
2.1 序列数据
我们想象现在有一组序列数据 data 0,1,2,3. 在当预测 result0 的时候,我们基于的是 data0, 同样在预测其他数据的时候, 我们也都只单单基于单个的数据. 每次使用的神经网络都是同一个 NN. 不过这些数据是有关联 顺序的 , 就像在厨房做菜, 酱料 A要比酱料 B 早放, 不然就串味了. 所以普通的神经网络结构并不能让 NN 了解这些数据之间的关联.
2.2 处理序列数据的神经网络
2.3 应用
RNN 的形式不单单这有这样一种, 他的结构形式很自由. 如果用于分类问题, 比如说一个人说了一句话, 这句话带的感情色彩是积极的还是消极的. 那我们就可以用只有最后一个时间点输出判断结果的RNN.
又或者这是图片描述 RNN, 我们只需要一个 X 来代替输入的图片, 然后生成对图片描述的一段话.
或者是语言翻译的 RNN, 给出一段英文, 然后再翻译成中文.
有了这些不同形式的 RNN, RNN 就变得强大了. 有很多有趣的 RNN 应用. 比如之前提到的, 让 RNN 描述照片. 让 RNN 写学术论文, 让 RNN 写程序脚本, 让 RNN 作曲. 我们一般人甚至都不能分辨这到底是不是机器写出来的.
3 代码实现
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 Dense,Activation,SimpleRNN
from keras.optimizers import AdamTIME_STEPS=28 # same as the height of the image 每一次读取一行,读取28个时间点
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(X_train,y_train),(X_test,y_test)=mnist.load_data()X_train=X_train.reshape(-1,28,28)/255 # normalize 变成0-1 训练数据要进行归一化处理,因为原始数据是8bit灰度图像所以需要除以255。
X_test=X_test.reshape(-1,28,28)/255
y_train=np_utils.to_categorical(y_train)
y_test=np_utils.to_categorical(y_test)model=Sequential()# RNN cell 首先添加RNN层,输入为训练数据,输出数据大小由CELL_SIZE定义。
model.add(SimpleRNN(
# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.# Otherwise, model.evaluate() will get error.batch_input_shape=(None,TIME_STEPS,INPUT_SIZE),output_dim=CELL_SIZE,
))#output layermodel.add(Dense(OUTPUT_SIZE))
model.add(Activation('softmax'))adam=Adam(LR)model.compile(optimizer=adam,loss="categorical_crossentropy",metrics=['accuracy']
)
#每次训练的时候并不是取所有的数据,只是取BATCH_SIZE个序列,或者称为BATCH_SIZE张图片
# training
for step in range(4001):X_batch = X_train[BATCH_INDEX: BATCH_INDEX + BATCH_SIZE, :, :]Y_batch = y_train[BATCH_INDEX: BATCH_INDEX + BATCH_SIZE, :]cost = model.train_on_batch(X_batch, Y_batch)BATCH_INDEX+=BATCH_SIZEBATCH_INDEX=0 if BATCH_INDEX>=X_train.shape[0] else BATCH_INDEXif step % 500 == 0:cost, accuracy = model.evaluate(X_test, y_test, batch_size=y_test.shape[0], verbose=False)print('test cost: ', cost, 'test accuracy: ', accuracy)
4 代码讲解
方法介绍
这次我们用循环神经网络(RNN, Recurrent Neural Networks)进行分类(classification),采用MNIST数据集,主要用到SimpleRNN层。
from keras.layers import SimpleRNN, Activation, Dense
MNIST里面的图像分辨率是28×28,为了使用RNN,我们将图像理解为序列化数据。 每一行作为一个输入单元,所以输入数据大小INPUT_SIZE = 28; 先是第1行输入,再是第2行,第3行,第4行,…,第28行输入, 这就是一张图片也就是一个序列,所以步长TIME_STEPS = 28。
训练数据要进行归一化处理,因为原始数据是8bit灰度图像所以需要除以255。
X_train = X_train.reshape(-1, 28, 28) / 255. # normalize
X_test = X_test.reshape(-1, 28, 28) / 255. # normalize
搭建模型
首先添加RNN层,输入为训练数据,输出数据大小由CELL_SIZE定义。
model.add(SimpleRNN(
# for batch_input_shape, if using tensorflow as the backend, we have to put None for the batch_size.
# Otherwise, model.evaluate() will get error.
batch_input_shape=(None, TIME_STEPS, INPUT_SIZE),
output_dim=CELL_SIZE,
unroll=True,
))
然后添加输出层,激励函数选择softmax
model.add(Dense(OUTPUT_SIZE))
model.add(Activation(‘softmax’))
设置优化方法,loss函数和metrics方法之后就可以开始训练了。 每次训练的时候并不是取所有的数据,只是取BATCH_SIZE个序列,或者称为BATCH_SIZE张图片,这样可以大大降低运算时间,提高训练效率。
训练
for step in range(4001):
# data shape = (batch_num, steps, inputs/outputs)
X_batch = X_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :, :]
Y_batch = y_train[BATCH_INDEX: BATCH_INDEX+BATCH_SIZE, :]
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
输出test上的loss和accuracy结果
if step % 500 == 0:
cost, accuracy = model.evaluate(X_test, y_test, batch_size=y_test.shape[0], verbose=False)
print('test cost: ', cost, 'test accuracy: ', accuracy)
有兴趣的话可以修改BATCH_SIZE和CELL_SIZE的值,试试这两个参数对训练时间和精度的影响。
5 输出
"C:\Program Files\Python36\pythonw.exe" C:/Users/88304/PycharmProjects/bianyiyuanli/cifafenxi.py
Using TensorFlow backend.
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint8 = np.dtype([("qint8", np.int8, 1)])
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint8 = np.dtype([("quint8", np.uint8, 1)])
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:521: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint16 = np.dtype([("qint16", np.int16, 1)])
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:522: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_quint16 = np.dtype([("quint16", np.uint16, 1)])
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'._np_qint32 = np.dtype([("qint32", np.int32, 1)])
C:\Program Files\Python36\lib\site-packages\tensorflow\python\framework\dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.np_resource = np.dtype([("resource", np.ubyte, 1)])
C:/Users/88304/PycharmProjects/bianyiyuanli/cifafenxi.py:32: UserWarning: Update your `SimpleRNN` call to the Keras 2 API: `SimpleRNN(batch_input_shape=(None, 28,..., units=50)`output_dim=CELL_SIZE,
2020-04-12 09:36:28.887040: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
test cost: 2.413351535797119 test accuracy: 0.03869999945163727
test cost: 0.5956025123596191 test accuracy: 0.8270000219345093
test cost: 0.43980643153190613 test accuracy: 0.8687000274658203
test cost: 0.32598525285720825 test accuracy: 0.9072999954223633
test cost: 0.287875771522522 test accuracy: 0.9194999933242798
test cost: 0.27394893765449524 test accuracy: 0.9208999872207642
test cost: 0.26940980553627014 test accuracy: 0.9225999712944031
test cost: 0.23819409310817719 test accuracy: 0.9297000169754028
test cost: 0.21854043006896973 test accuracy: 0.9369000196456909进程已结束,退出代码0
Keras【Deep Learning With Python】RNN Classifier 循环神经网络相关推荐
- Keras【Deep Learning With Python】LSTM 循环神经网络解决Regressor回归问题
文章目录 1 前言 2 RNN 的弊端 3 LSTM 4 代码实现 5 重要部份讲解 6 输出: 1 前言 和前几篇文章一样,依旧是分为讲解和代码实现. 2 RNN 的弊端 之前我们说过, RNN 是 ...
- Deep Learning × ECG (5) :利用循环神经网络RNN对心律失常ECG数据进行分类
文章目录 1. RNN 介绍 2. 搭建RNN模型进行训练 1. RNN 介绍 循环神经网络RNN的提出主要针对于时间序列数据. 类似于股票.心律失常 ECG 和 电力数据 等数据都是属于时间序列数据 ...
- Keras【Deep Learning With Python】CNN卷积神经网络(看不懂你打我系列)
文章目录 前言 1 CNN概述 2 我的分析 3 代码实现 4 代码分析 6 结果 前言 本文分为CNN讲解和keras实现CNN(mnist数据集)两部分. 1 CNN概述 卷积神经网络(Convo ...
- Keras Tutorial: Deep Learning in Python
This Keras tutorial introduces you to deep learning in Python: learn to preprocess your data, model, ...
- Deep learning with Python 学习笔记(6)
本节介绍循环神经网络及其优化 循环神经网络(RNN,recurrent neural network)处理序列的方式是,遍历所有序列元素,并保存一个状态(state),其中包含与已查看内容相关的信息. ...
- Deep learning with Python 学习笔记(9)
神经网络模型的优化 使用 Keras 回调函数 使用 model.fit()或 model.fit_generator() 在一个大型数据集上启动数十轮的训练,有点类似于扔一架纸飞机,一开始给它一点推 ...
- python思想读后感_《Deep Learning with Python》读后感精选
<Deep Learning with Python>是一本由Francois Chollet著作,Manning Publications出版的Paperback图书,本书定价:USD ...
- Python深度学习:基于PyTorch [Deep Learning with Python and PyTorch]
作者:吴茂贵,郁明敏,杨本法,李涛,张粤磊 著 出版社:机械工业出版社 品牌:机工出版 出版时间:2019-11-01 Python深度学习:基于PyTorch [Deep Learning with ...
- Keras——用Keras搭建RNN回归循环神经网络
文章目录 1.前言 2.用Keras搭建RNN回归循环神经网络 2.1.导入必要模块 2.2.超参数设置 2.3.构造数据 2.4.搭建模型 2.5.激活模型 2.6.训练+测试 1.前言 这次我们用 ...
最新文章
- SHELL-命令解释程序
- 开发一个用户喜欢的ABAP接口日志程序
- spring @order控制对象的顺序
- SpringBoot添加JSP支持
- 【NLP】Google T5速读
- Python判断一个字符串是否可以转换为数字(字符串转数字、字符串转整形、字符串转int、字符串转浮点型)
- C++:VS2019调试dump文件
- 【luogu4185】 [USACO18JAN]MooTube [并查集]
- 理解Go的Goroutine和channel
- opencv摄像头拍摄视频并保存方法
- 怎样把图片转换成线条图?
- WinRAR和WinZIP 密码找回
- 基于Pytorch对凸函数采用SGD算法优化实例(附源码)
- 单片机三角波c语言程序,基于51单片机的三角波发生器
- 自然语言处理—文本分类综述/什么是文本分类
- 2017年的Microsoft Imagine Cup提供的免费Azure申请及使用方法
- 牛顿法求临界水深c语言程序,基于牛顿迭代法的圆形断面临界水深直接计算法邹武停.pdf...
- gazebo设置_gazebo教程(六)插件配置
- SOUI总结之常用功能
- 智慧树工业机器人测试答案_智慧树工业机器人答案章节单元测试答案
热门文章
- pytorch relu6
- python websocket server 解决中文乱码
- PyTorch中如何使用tensorboard可视化
- 暗通道去雾算法 python实现
- laravel学习资料整理
- OpenCV优化:图像的遍历4种方式
- SNMP OID是什么?
- 二十二、redis持久化之AOF
- linux内存转换成功,linux系统内存转换成硬盘使用
- php中的网页漂浮代码,JavaScript_Javascript实现带关闭按钮的网页漂浮广告代码,复制代码 代码如下: html - phpStudy...