参考:
请注意,虽然通常情况下张量的第一个维度是批次大小batch size,但是PyTorch建议我们输入循环网络的时候张量的第一个维度是序列长度,而第二个维度才是批次数量。
即: 输入LSTM的 input.size() == (seq_len, batch_size, inp_dim)
其中:seq_len: 时间序列的长度
batch_size:同个批次中输入的序列条数
inp_dim: 输入数据的维度,例如输入数据由【客流量,年份,月份】三组数据构成,则 inp_dim == 3
PyTorch中示例:
Examples::
>>> 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))
class RegLSTM(nn.Module):
def __init__(self, inp_dim, out_dim, mid_dim, mid_layers):
super(RegLSTM, self).__init__()
self.rnn = nn.LSTM(inp_dim, mid_dim, mid_layers) # rnn
self.reg = nn.Sequential(
nn.Linear(mid_dim, mid_dim),
nn.Tanh(),
nn.Linear(mid_dim, out_dim),
) # regression
def forward(self, x):
y = self.rnn(x)[0] # y, (h, c) = self.rnn(x)
seq_len, batch_size, hid_dim = y.shape
y = y.view(-1, hid_dim)
y = self.reg(y)
y = y.view(seq_len, batch_size, -1)
return y
"""
PyCharm Crtl+click nn.LSTM() jump to code of PyTorch:
Examples::
>>> rnn = nn.LSTM(10, 20, 2)