Python 如何使用批处理运行RNN模型,使每行中的文本具有不同的长度?

Python 如何使用批处理运行RNN模型,使每行中的文本具有不同的长度?,python,nlp,pytorch,data-mining,torch,Python,Nlp,Pytorch,Data Mining,Torch,我想在pytorch中使用torch.nn.utils.rnn.pack\u padded\u序列来处理这个问题,但是我不知道当最后一个维度中的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 long

我想在pytorch中使用
torch.nn.utils.rnn.pack\u padded\u序列来处理这个问题,但是我不知道当最后一个维度中的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)

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

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

救命啊

我从该代码中了解到,在使用
torch.nn.utils.rnn.pack\u padded\u序列之前,应使用零或其他任何无意义的数字完成火炬