我正在尝试编写一个英语到西班牙语的 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 的了解相当模糊,因此也许数据必须采用不同的格式才能正确输入神经网络。在通过网络输入数据之前,我尝试了不同维度的数据,但没有任何效果。
有人知道要改什么吗?
提前致谢!😇