Keras实现LSTM
一、先看一个Example
1、描述,输入为一个字母,输出为这个字母的下一个顺序字母
- A->B
- B->C
- C->D
2、Code
import numpy
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.utils import np_utils
# 固定每次的随机数都是相同的
numpy.random.seed(7)
# define the raw dataset
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# create mapping of characters to integers (0-25) and the reverse
char_to_int = dict((c, i) for i, c in enumerate(alphabet))
int_to_char = dict((i, c) for i, c in enumerate(alphabet))
# prepare the dataset of input to output pairs encoded as integers
seq_length = 1
dataX = []
dataY = []
for i in range(0, len(alphabet) - seq_length, 1):
seq_in = alphabet[i:i + seq_length]
seq_out = alphabet[i + seq_length]
dataX.append([char_to_int[char] for char in seq_in])
dataY.append(char_to_int[seq_out])
print(seq_in, '->', seq_out)
# reshape X to be [samples, time steps, features]
X = numpy.reshape(dataX, (len(dataX), seq_length, 1))
# normalize
X = X / float(len(alphabet))
print(X.shape[0], X.shape[1], X.shape[2])
# one hot encode the output variable
y = np_utils.to_categorical(dataY)
# create and fit the model
model = Sequential()
model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2])))
model.add(Dense(y.shape[1], activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, nb_epoch=500, batch_size=1, verbose=2)
scores = model.evaluate(X, y, verbose=0)
print("Model Accuracy: %.2f%%" % (scores[1] * 100))
二、Keras中的LSTM
1、Keras中的LSTM被reshape成的形状[samples, time steps, features]。time steps就是所谓的时间序列步骤。上述例子中,句子实际上是多个时间步骤,一个特征而并非一个时间步骤,多个特征。
看上述的这行代码
X = numpy.reshape(dataX, (len(dataX), seq_length, 1))
- Samples:这是dataX的长度,或者数据集的大小
- Time steps:这个和RNN的time steps等价。如果你的网络想有60个字母的记忆,那么这里面的数字就写上60
- Features:这个是每个time steps的特征数量。如果正在处理图片的话,那么这个就是一个图片像素的个数。在这里每个time step就是一个feature
2、X = numpy.reshape(dataX, (len(dataX), 3, 1)) and X = numpy.reshape(dataX, (len(dataX), 1, 3))对LSTM最终结果有什么影响呢?
- (len(dataX), 3, 1) 会使LSTM迭代3次,体现了循环神经网路的特点
- (len(dataX), 1, 3) 会使得LSTM只迭代一次,对与循环神经网络这毫无意义,根本就没有使用到LS的特性
也可以理解TimeSteps就是unfold的意思,就是tensorflow中的NUM_STEPS的意思。
Features其实就是输入的纬度,也就是特征,一个纬度一个特征。
三、进阶
Keras实现LSTM的更多相关文章
- 基于 Keras 用 LSTM 网络做时间序列预测
目录 基于 Keras 用 LSTM 网络做时间序列预测 问题描述 长短记忆网络 LSTM 网络回归 LSTM 网络回归结合窗口法 基于时间步的 LSTM 网络回归 在批量训练之间保持 LSTM 的记 ...
- 使用keras的LSTM进行预测----实战练习
代码 import numpy as np from keras.models import Sequential from keras.layers import Dense from keras. ...
- 基于 Keras 的 LSTM 时间序列分析——以苹果股价预测为例
简介 时间序列简单的说就是各时间点上形成的数值序列,时间序列分析就是通过观察历史数据预测未来的值.预测未来股价走势是一个再好不过的例子了.在本文中,我们将看到如何在递归神经网络的帮助下执行时间序列分析 ...
- 【Python】keras使用LSTM拟合曲线
keras生成的网络结构如下图: 代码如下: from sklearn.preprocessing import MinMaxScaler from keras.models import Seque ...
- Kesci: Keras 实现 LSTM——时间序列预测
博主之前参与的一个科研项目是用 LSTM 结合 Attention 机制依据作物生长期内气象环境因素预测作物产量.本篇博客将介绍如何用 keras 深度学习的框架搭建 LSTM 模型对时间序列做预测. ...
- keras的LSTM函数详解
keras.layers.recurrent.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid', use_bias= ...
- 用keras实现lstm 利用Keras下的LSTM进行情感分析
1 I either LOVE Brokeback Mountain or think it’s great that homosexuality is becoming more accept ...
- 手把手教你用 Keras 实现 LSTM 预测英语单词发音
1. 动机 我近期在研究一个 NLP 项目,根据项目的要求,需要能够通过设计算法和模型处理单词的音节 (Syllables),并对那些没有在词典中出现的单词找到其在词典中对应的押韵词(注:这类单词类似 ...
- keras中 LSTM 的 [samples, time_steps, features] 最终解释
I am going through the following blog on LSTM neural network:http://machinelearningmastery.com/under ...
随机推荐
- Windows苹果安卓手机远程桌面客户端推荐
适用于:Windows 10.Windows 8.1.Windows Server 2012 R2.Windows Server 2016 最近公司电脑从Windows7升级到了Windows10,然 ...
- git tag 打标签
注意:在哪个分支上打tag一定要先提交该分支到远程gitlab仓库 标签(tag)操作 1. 查看所有标签 git tag 默认标签是打在最新提交的commit上的 2.本地打新标签 git tag ...
- 数据库升级到mysql5.7出现的1067 - Invalid default value for '字段名' (docker版)
docker run -d --name xxx mysql:5.7 docker container cp xxx:/etc/mysql/mysql.conf.d . // 取出mysql中的配 ...
- 使用 Flask-Cache 缓存给Flask提速
https://blog.csdn.net/u013205877/article/details/78013289
- springboot开启事务支持时报代理错误
问题:The bean 'xxx' could not be injected as a 'com.github.service.xx' because it is a JDK dynamic pro ...
- NodeJs操作MongoDB之多表查询($lookup)与常见问题
NodeJs操作MongoDB之多表查询($lookup)与常见问题 一,方法介绍 aggregate()方法来对数据进行聚合操作.aggregate()方法的语法如下 1 aggregate(ope ...
- Django(三) ORM 数据库操作
大纲 一.DjangoORM 创建基本类型及生成数据库表结构 1.简介 2.创建数据库 表结构 二.Django ORM基本增删改查 1.表数据增删改查 2.表结构修改 三.Django ORM 字段 ...
- Module build failed: Error: Cannot find module 'babel-runtime/core-js/get-it
npm i babel-loader@7.1.5 -D
- vue实现懒加载
- 深度学习结合SLAM研究总结
博客转载自:https://blog.csdn.net/u010821666/article/details/78793225 原文标题:深度学习结合SLAM的研究思路/成果整理之 1. 深度学习跟S ...