Pytorch nn.LSTM:运行时错误:对于未批处理的 2 维输入,hx 和 cx 也应该是 2 维,但得到了(3 维,3 维)张量

问题描述 投票:0回答:1

我正在尝试编写一个英语到西班牙语的 seq2seq 翻译器,包括 LSTM 编码和 LSTM 解码层。
在转发过程中,我收到以下错误:

RuntimeError: For unbatched 2-D input, hx and cx should also be 2-D but got (3-D, 3-D) tensors 

有趣的是,编码层似乎在转发过程中起作用,是解码层抛出错误。

这是我的神经网络。我添加了几个打印语句来了解数据的可能转换和形状,以供学习和调试。

class Seq2SeqModel(nn.Module):
  def __init__(self, input_size, hidden_size, output_size, num_layers):
    super(Seq2SeqModel, self).__init__()
    self.encoder = nn.LSTM(input_size=input_size, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)
    self.decoder = nn.LSTM(input_size=68, hidden_size=hidden_size, num_layers=num_layers, batch_first=True)
    self.fc = nn.Linear(in_features=hidden_size, out_features=output_size)

  def forward(self, x, y):
    print(f"x shape: {x.shape}")
    print(f"y shape: {y.shape}")
    print("Set Memories")
    h_0 = torch.autograd.Variable(torch.zeros(num_layers, x.size(0), hidden_size))
    c_0 = torch.autograd.Variable(torch.zeros(num_layers, x.size(0), hidden_size))
    print(f"h: {h_0.shape}")
    print(f"c: {c_0.shape}")
    print("Encoder...")
    _, (hidden, cell) = self.encoder(x, (h_0, c_0))
    print("Results:")
    print(hidden.shape, cell.shape)
    print("Decoder:")
    decoder_output, _ = self.decoder(y, (hidden, cell)), #self.decoder(y, (hidden.view(1, 32, 256), cell.view(1, 32, 256)))
    print("Results: ", decoder_output.shape)
    output = self.fc(decoder_output)
    return output

训练的输出如下所示:

x shape: torch.Size([32, 70, 100])
y shape: torch.Size([32, 68])
Set Memories
h: torch.Size([1, 32, 256])
c: torch.Size([1, 32, 256])
Encoder...
Results:
torch.Size([1, 32, 256]) torch.Size([1, 32, 256])
Decoder:

x 是英文单词。批量大小为 32,包括填充在内的最大长度为 70(所有句子长 70 个单词),所有这 70 个单词都由 100 个数字表示,在 Word2Vec 嵌入中形成一个向量。
y 是西班牙语单词。同样,批量大小为 32,但包括填充在内的最大长度为 68。西班牙语单词由一个数字表示(无向量嵌入)

神经网络的初始化如下所示:

model = Seq2SeqModel(input_size=100, hidden_size=256, output_size=68, num_layers=1)

我对 LSTM 的了解相当模糊,因此也许数据必须采用不同的格式才能正确输入神经网络。在通过网络输入数据之前,我尝试了不同维度的数据,但没有任何效果。

有人知道要改什么吗?

提前致谢!😇

python pytorch neural-network lstm seq2seq
1个回答
0
投票

您无法将整数张量发送到 LSTM 模型中。

y
张量需要像
x
张量一样是 3D 的。您还需要迭代解码器输入。

您可以查看 this pytorch 中 seq2seq 的示例。

© www.soinside.com 2019 - 2024. All rights reserved.