目录
1.LSTM-Encoder-Decoder模型结构及简要解析
2.general LSTM Unit——使用pytorch nn.LSTM组件构建LSTM_AE
1.LSTM-Encoder-Decoder模型结构及简要解析
图1 LSTM_AE模型示意图(借图)
LSTM_Encoder-Decoder结构包含一个Encoder和一个Decoder,输入以时间序列方式给出(图中左侧绿色节点),LSTM E你coder Unit对输入计算得到输出(图中左侧蓝色方块,需要注意示意图中是按照时序方向展开的结构,实际上其所有LSTM Unit都是同一个),得到编码结果(图中黄色节点);将编码结果当作Decoder的初始输入,计算得到Decoder第一个输出,将前一个Decoder单元的输出结果当作下一个单元的输入,依次进行计算得到结果...
进行下面的内容前需要一些注释:
general LSTM Unit是指【最原始版本的LSTM方式】在单元内进行计算的【LSTM单元】
自定义LSTM Unit是指【自定义LSTM Unit中需要的计算】得到的【LSTM单元】
2.general LSTM Unit——使用pytorch nn.LSTM组件构建LSTM_AE
首先给出general LSTM Unit中完成的计算:
图2 基本LSTM单元中进行的计算
其思想简单易懂:定义【输入门,遗忘门,输出们】,根据【输入-输入门,上一个时间步的记忆状态c_t-1 - 遗忘门】得到当前时间步的记忆状态c_t;根据数据对【输出门-当前时间步的记忆状态c_t】得到当前时间步的输出h_t
如果基本LSTM单元就能满足需要,可以使用nn.Module中提供的组件LSTM搭建模型:
1.定义Encoder:
class lstm_encoder(nn.Module):
def __init__(self,input_size,hidden_size,num_layers=1):
'''
param:
input_size:the feature dim of input X
hidden_size:the feature dim of hidden state h
num_layers:the num of stacked lstm layers
'''
super(lstm_encoder,self).__init__()
self.input_size=input_size
self.hidden_size=hidden_size
self.num_layers=num_layers
self.lstm=nn.LSTM(input_size=input_size,hidden_size=hidden_size,num_layers=num_layers)
def forward(self,x):
'''
param:
x:input of lstm unit,(seq_len,batch,feature_dim)
output:
seq_out:give all the hidden state in the sequence
last:the last h & c
'''
self.seq_out,self.last=self.lstm(x.view(x.shape[0],x.shape[1],self.