【问题标题】:How to use batch to run RNN model with texts that have different length in each row?如何使用批处理运行 RNN 模型,其中每行具有不同长度的文本?
【发布时间】:2020-10-25 17:45:19
【问题描述】:

我想在pytorch中使用torch.nn.utils.rnn.pack_padded_sequence来解决这个问题,但是我不知道当torch变量在最后一维的大小不同时如何获取?

torch.nn.utils.rnn.pack_padded_sequence(input, lengths, batch_first=False, enforce_sorted=True)
input can be of size T x B x * where T is the length of the longest sequence (equal to lengths[0]), B is the batch size, and * is any number of dimensions (including 0)

pack_padded_sequence

如果我运行以下代码,我会得到错误。

x_torch = torch.from_numpy(np.ones((2, 3)))
y_torch = torch.from_numpy(np.ones((2, 5)))
ans_torch = torch.cat([x_torch, y_torch], dim = 0)

# ouput
RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 3 and 5 in dimension 1 at c:\a\w\1\s\windows\pytorch\aten\src\th\generic/THTensorMoreMath.cpp:1333

救命!

【问题讨论】:

    标签: python nlp pytorch data-mining torch


    【解决方案1】:

    我从这段代码中学到,在使用torch.nn.utils.rnn.pack_padded_sequence之前,应该用零或其他任何没有意义的数字来完成火炬。

    pad_packed_demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-15
      • 2021-09-19
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多