LSTM和GRU原理及pytorch代码,输入输出大小说明

#RNN
rnn=nn.RNN(10,20,2) #(each_input_size, hidden_state, num_layers)
input=torch.randn(5,3,10) # (seq_len, batch, input_size)
h0=torch.randn(2,3,20) #(num_layers * num_directions, batch, hidden_size)
output,hn=rnn(input,h0)
print(output.size(),hn.size())


#LSTM
rnn=nn.LSTM(10,20,2) #(each_input_size, hidden_state, num_layers)
input=torch.randn(5,3,10) # (seq_len, batch, input_size)
h0=torch.randn(2,3,20) #(num_layers * num_directions, batch, hidden_size)
c0=torch.randn(2,3,20) #(num_layers * num_directions, batch, hidden_size)
output,(hn,cn)=rnn(input,(h0,c0))
print(output.size(),hn.size(),cn.size())


#GRU
rnn=nn.GRU(10,20,2)
input=torch.randn(5,3,10)
h0=torch.randn(2,3,20)
output,hn=rnn(input,h0)
print(output.size(),hn.size())

先上结论:

  1. output保存了最后一层,每个time step的输出h,如果是双向LSTM,每个time step的输出h = [h正向, h逆向] (同一个time step的正向和逆向的h连接起来)。
  2. h_n保存了每一层,最后一个time step的输出h,如果是双向LSTM,单独保存前向和后向的最后一个time step的输出h。
  3. c_n与h_n一致,只是它保存的是c的值。

output是一个三维的张量,第一维表示序列长度,第二维表示一批的样本数(batch),第三维是 hidden_size(隐藏层大小) * num_directions ,双向的时候num_directions是2

h_n是一个三维的张量,第一维是num_layers*num_directions,num_layers是我们定义的神经网络的层数,num_directions在上面介绍过,取值为1或2,表示是否为双向LSTM

c_n与h_n的结构一样,就不重复赘述了。

猜你喜欢

转载自blog.csdn.net/qq_30468133/article/details/85244957
今日推荐