【问题标题】:Variable size input for LSTM in PytorchPytorch 中 LSTM 的可变大小输入
【发布时间】:2018-09-24 17:49:37
【问题描述】:

我正在使用可变长度视频的特征来训练一层 LSTM。视频大小从 10 帧变为 35 帧。我使用的批量大小为 1。 我有以下代码:

lstm_model = LSTMModel(4096, 4096, 1, 64)
for step, (video_features, label) in enumerate(data_loader):
    bx = Variable(score.view(-1, len(video_features), len(video_features[0]))) #examples = 1x12x4096, 1x5x4096
    output = lstm_model(bx)

Lstm 模型是;

class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, num_classes):
    super(LSTMModel, self).__init__()
    self.l1 = nn.LSTM(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)
    self.out = nn.Linear(hidden_size, num_classes)
def forward(self, x):
    r_out, (h_n, h_c) = self.l1(x, None) #None represents zero initial hidden state
    out = self.out(r_out[:, -1, :])
    return out

我只是想问一下;我在训练具有可变大小输入的 LSTM 方面做得对吗?代码工作正常,损失减少,但我不确定我是否做对了。因为我之前没有在 Pytorch 中使用过 LSTM。

【问题讨论】:

    标签: deep-learning lstm pytorch


    【解决方案1】:

    是的,您的代码是正确的,并且始终适用于 1 的批量大小。但是,如果您想使用 1 以外的批量大小,则需要将可变大小的输入打包成一个序列,然后在 LSTM 之后解压。您可以在my answer to a similar question找到更多详细信息。

    附: - 您应该将此类问题发布到 codereview

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-31
      • 2018-08-11
      • 2019-10-23
      • 2021-08-19
      • 2018-10-24
      • 1970-01-01
      • 2017-07-27
      • 2021-07-04
      相关资源
      最近更新 更多