RNN中,最普通的一种LSTM,每个小RNNCell具有4个gate,即4个layer,对于同样的一个RNNCell在不同的资料上面,表达公式略有出入。
这三个layer,activation function:三个sigmoid,一个tanh。
正常的LSTM:
附上程序实现1:
import torch as t
from torch import nn
from torch.autograd import Variable as V
t.manual_seed(1000)
# 输入 batch_size=3,序列长度都为2,序列中每个元素占4维
input=V(t.randn(2,3,4))
print(input)
# lstm 输入向量4维,3个隐藏元,1层
lstm=nn.LSTM(4,3,1)
# 初始状态:1层,batch_size=3,3个隐层元
h0=V(t.randn(1,3,3))
c0=V(t.randn(1,3,3))
out,hn=lstm(input,(h0,c0))
print(out)
程序实现2:
>>> rnn = nn.LSTM(10, 20, 2)
>>> input = torch.randn(5, 3, 10)
>>> h0 = torch.randn(2, 3, 20)
>>> c0 = torch.randn(2, 3, 20)
>>> output, (hn, cn) = rnn(input, (h0, c0))
资料1:
https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html?highlight=lstm#torch.nn.LSTM
资料2: