试图了解Pytorch对LSTM的实施

时间:2019-03-28 23:27:43

标签: machine-learning deep-learning lstm pytorch recurrent-neural-network

我有一个包含1000个示例的数据集,其中每个示例具有 5 个功能(a,b,c,d,e)。我想将 7 个示例提供给LSTM,以便它预测第8天的功能(a)。

阅读nn.LSTM()的Pytorchs文档时,我想到了以下内容:

input_size = 5
hidden_size = 10
num_layers = 1
output_size = 1

lstm = nn.LSTM(input_size, hidden_size, num_layers)
fc = nn.Linear(hidden_size, output_size)

out, hidden = lstm(X)  # Where X's shape is ([7,1,5])
output = fc(out[-1])

output  # output's shape is ([7,1])

根据文档:

nn.LSTM的输入为“ 形状的输入( seq_len,批处理,input_size )”,并带有“ input_size –期望的特征数量输入x “,

输出为:“形状的输出( seq_len,批处理,num_directions * hidden_​​size ):张量包含每个t的LSTM最后一层的输出特征(h_t)。”

在这种情况下,我认为seq_len将是7个示例的序列,batch是1,而input_size是5。因此lstm将使用每个包含5个特征的示例,这些特征将每次迭代都隐藏层。

我想念什么?

1 个答案:

答案 0 :(得分:0)

在将您的代码扩展为完整示例时-我还添加了一些注释可能会有所帮助-我得到以下信息:

import torch
import torch.nn as nn

input_size = 5
hidden_size = 10
num_layers = 1
output_size = 1

lstm = nn.LSTM(input_size, hidden_size, num_layers)
fc = nn.Linear(hidden_size, output_size)

X = [
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
    [[1,2,3,4,5]],
]

X = torch.tensor(X, dtype=torch.float32)

print(X.shape)         # (seq_len, batch_size, input_size) = (7, 1, 5)
out, hidden = lstm(X)  # Where X's shape is ([7,1,5])
print(out.shape)       # (seq_len, batch_size, hidden_size) = (7, 1, 10)
out = out[-1]          # Get output of last step
print(out.shape)       # (batch, hidden_size) = (1, 10)
out = fc(out)          # Push through linear layer
print(out.shape)       # (batch_size, output_size) = (1, 1)

鉴于您的batch_size = 1output_size = 1,这对我来说很有意义(我想,您正在做回归)。我不知道您的output.shape = (7, 1)来自哪里。

您确定X的尺寸正确吗?您是否使用nn.LSTM创建了batch_first=True?有很多小东西可以潜入。