前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >双向 LSTM

双向 LSTM

作者头像
杨熹
发布2018-04-03 16:12:51
4.4K0
发布2018-04-03 16:12:51
举报
文章被收录于专栏:杨熹的专栏杨熹的专栏

本文结构:

  • 为什么用双向 LSTM
  • 什么是双向 LSTM
  • 例子

为什么用双向 LSTM?

单向的 RNN,是根据前面的信息推出后面的,但有时候只看前面的词是不够的, 例如,

我今天不舒服,我打算____一天。

只根据‘不舒服‘,可能推出我打算‘去医院‘,‘睡觉‘,‘请假‘等等,但如果加上后面的‘一天‘,能选择的范围就变小了,‘去医院‘这种就不能选了,而‘请假‘‘休息‘之类的被选择概率就会更大。


什么是双向 LSTM?

双向卷积神经网络的隐藏层要保存两个值, A 参与正向计算, A' 参与反向计算。 最终的输出值 y 取决于 A 和 A':

即正向计算时,隐藏层的 s_t 与 s_t-1 有关;反向计算时,隐藏层的 s_t 与 s_t+1 有关:

在某些任务中,双向的 lstm 要比单向的 lstm 的表现要好:


例子

下面是一个 keras 实现的 双向LSTM 应用的小例子,任务是对序列进行分类, 例如如下 10 个随机数:

0.63144003 0.29414551 0.91587952 0.95189228 0.32195638 0.60742236 0.83895793 0.18023048 0.84762691 0.29165514

累加值超过设定好的阈值时可标记为 1,否则为 0,例如阈值为 2.5,则上述输入的结果为:

0 0 0 1 1 1 1 1 1 1

和单向 LSTM 的区别是用到 Bidirectional: model.add(Bidirectional(LSTM(20, return_sequences=True), input_shape=(n_timesteps, 1)))

from random import random
from numpy import array
from numpy import cumsum
from keras.models import Sequential
from keras.layers import LSTM
from keras.layers import Dense
from keras.layers import TimeDistributed
from keras.layers import Bidirectional

# create a sequence classification instance
def get_sequence(n_timesteps):
    # create a sequence of random numbers in [0,1]
    X = array([random() for _ in range(n_timesteps)])
    # calculate cut-off value to change class values
    limit = n_timesteps/4.0
    # determine the class outcome for each item in cumulative sequence
    y = array([0 if x < limit else 1 for x in cumsum(X)])
    # reshape input and output data to be suitable for LSTMs
    X = X.reshape(1, n_timesteps, 1)
    y = y.reshape(1, n_timesteps, 1)
    return X, y

# define problem properties
n_timesteps = 10

# define LSTM
model = Sequential()
model.add(Bidirectional(LSTM(20, return_sequences=True), input_shape=(n_timesteps, 1)))
model.add(TimeDistributed(Dense(1, activation='sigmoid')))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])

# train LSTM
for epoch in range(1000):
    # generate new random sequence
    X,y = get_sequence(n_timesteps)
    # fit model for one epoch on this sequence
    model.fit(X, y, epochs=1, batch_size=1, verbose=2)
    
# evaluate LSTM
X,y = get_sequence(n_timesteps)
yhat = model.predict_classes(X, verbose=0)
for i in range(n_timesteps):
    print('Expected:', y[0, i], 'Predicted', yhat[0, i])

学习资料: https://zybuluo.com/hanbingtao/note/541458 https://maxwell.ict.griffith.edu.au/spl/publications/papers/ieeesp97_schuster.pdf http://machinelearningmastery.com/develop-bidirectional-lstm-sequence-classification-python-keras/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.11 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 为什么用双向 LSTM?
  • 什么是双向 LSTM?
  • 例子
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档