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 ...
随机推荐
- 关于Xcode10的那些事
前言 这里主要介绍一下Xcode10 版本主要更新的内容. 随着iOS12的发布,Xcode10已经可以从Mac App Store下载. Xcode10包含了iOS12.watchOS 5.macO ...
- IntelliJ IDEA 最新激活码
C40PF37RR0-eyJsaWNlbnNlSWQiOiJDNDBQRjM3UlIwIiwibGljZW5zZWVOYW1lIjoiemhhbmcgeW9uZyIsImFzc2lnbmVlTmFtZ ...
- 【spring源码分析】IOC容器初始化(二)
前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...
- 多种解法解决n皇后问题
多种解法解决n皇后问题 0x1 目的 深入掌握栈应用的算法和设计 0x2 内容 编写一个程序exp3-8.cpp求解n皇后问题. 0x3 问题描述 即在n×n的方格棋盘上,放置n个皇后,要求每 ...
- Linux安装Sqlite
下载SQLite源代码sqlite-3.6.23.1.tar.gz 复制sqlite-3.6.23.1.tar.gz到linux上的/usr/src目录 解压源代码 tar -xvzf sqlite- ...
- 【递归】hex2dec
自己捉摸了好久,由于不熟悉. #include <stdio.h> int dec2hex(char *p); int base; int num; int main(void) { ch ...
- keras01 - hello world ~ 搭建第一个神经网络
import numpy as np from keras.datasets import mnist from keras.models import Sequential, Model from ...
- 《为大量出现的KPI流快速部署异常检测模型》 笔记
以下我为这篇<Rapid Deployment of Anomaly Detection Models for Large Number of Emerging KPI Streams>做 ...
- React笔记:React基础(2)
1. JSX JSX是一种拥有描述UI的JavaScript扩展语法,React使用这种语法描述组件的UI. 1.1 基本语法 JSX可以嵌套多个HTML标签,可以使用大部分符号HTML规范的属性. ...
- 【linux】Python3.6安装报错 configure: error: no acceptable C compiler found in $PATH
安装python的时候出现如下的错误: [root@master ~]#./configure --prefix=/usr/local/python3.6 checking build system ...