LSTM(long short-time memory,长的短期记忆网络)

https://www.zhihu.com/question/41949741

部分图片来自台大李弘毅的深度学习课程

 

simpleRNN

step1, raw text:
接触LSTM模型不久,简单看了一些相关的论文,还没有动手实现过。然而至今仍然想不通LSTM神经网络究竟是怎么工作的。……

step2, tokenize (中文得分词):
sentence1: 接触 LSTM 模型 不久 ,简单 看了 一些 相关的 论文 , 还 没有 动手 实现过 。
sentence2: 然而 至今 仍然 想不通 LSTM 神经网络 究竟是 怎么 工作的。
……

step3, dictionarize(把句子中每个词编号):
sentence1: 1 34 21 98 10 23 9 23
sentence2: 17 12 21 12 8 10 13 79 31 44 9 23
……

step4, padding every sentence to fixed length(为了保持相同的长度(time_step length),在末尾补0):
sentence1: 1 34 21 98 10 23 9 23 0 0 0 0 0
sentence2: 17 12 21 12 8 10 13 79 31 44 9 23 0
……
 

step5, mapping token to an embeddings:
sentence1:
 LSTM(long short-time memory,长的短期记忆网络),每一列代表一个词向量;矩阵列数固定为time_step length。
sentence2:
……

 

step6, feed into RNNs as input:

取出一个词向量LSTM(long short-time memory,长的短期记忆网络):(LSTM(long short-time memory,长的短期记忆网络)LSTM(long short-time memory,长的短期记忆网络),...),输入到RNN,LSTM(long short-time memory,长的短期记忆网络)

 

RNN的整个训练过程是(将arrive,on分类成“other”;Nov 2分类成“date”;Taipei可分类成“destination”“departure”,取决于前面的单词是arrive 还是leave):

LSTM(long short-time memory,长的短期记忆网络),其中LSTM(long short-time memory,长的短期记忆网络)是一个词向量,红框部分是对一个词语的分类过程。图中所示是对一句话的分析过程。

相比于RNN,LSTM的隐藏层神经元结构更复杂。simpleRNN中,对一个词向量的分析采用全连接神经网络,其输入只有一份;但在LSTM中,memory cell类比于神经元,其输入需要4份(分别控制input/forget/output gate),故其参数的数量是simpleRNN的4倍。

LSTM(long short-time memory,长的短期记忆网络)             LSTM(long short-time memory,长的短期记忆网络)

 

在LSTM中,需要将输入(一个词向量)映射成4个部分Zi、Zf、Zo、Z,分别控制memory cell的input gate,forget gate,output gate,以及作为input。下图是一个memory cell的结构

LSTM(long short-time memory,长的短期记忆网络)

 

处理一个词向量LSTM(long short-time memory,长的短期记忆网络),将LSTM(long short-time memory,长的短期记忆网络)映射成另一个向量LSTM(long short-time memory,长的短期记忆网络),将z的每个元素作为memory cell的输入(此时每个memory cell 的3个gate及input 都是输入一个数值);类似地,把LSTM(long short-time memory,长的短期记忆网络)映射成LSTM(long short-time memory,长的短期记忆网络),...。

记忆单元也是一个向量。以图中红框各部分作为向量元素,组成了记忆单元

每个memory cell的输出是一个数字,所有输出数字作为向量元素,组成该词向量的输出。表示对其分类:(0 1 0 0)表示这个词归在第二类

LSTM(long short-time memory,长的短期记忆网络)   LSTM(long short-time memory,长的短期记忆网络)

 

故针对一个词向量整体来说,对其分析过程如下。此时将LSTM(long short-time memory,长的短期记忆网络)映射成LSTM(long short-time memory,长的短期记忆网络),作为向量输入;LSTM(long short-time memory,长的短期记忆网络)是来自 上一个词向量 传入的记忆(也是向量);LSTM(long short-time memory,长的短期记忆网络)是该词向量的输出(也是一个向量),表示其分类。上面的两图是对下图黄框部分的具体实现过程。下图红框部分是对4个向量及记忆LSTM(long short-time memory,长的短期记忆网络)的操作流程。

LSTM(long short-time memory,长的短期记忆网络)

 

对于一个句子来说,其整个训练是一个循环过程,每个词向量的分析都是经过同样的结构(下图红框)。将整个循环过程展开如下

LSTM(long short-time memory,长的短期记忆网络)