我有这个解码器模型,它应该将成批的句子嵌入(batchsize = 50,hidden size=300)作为输入,并输出一批预测句子的热表示:
class DecoderLSTMwithBatchSupport(nn.Module):
# Your code goes here
def __init__(self, embedding_size,batch_size, hidden_size, output_size):
super(DecoderLSTMwithBatchSupport, self).__init__()
self.hidden_size = hidden_size
self.batch_size = batch_size
self.lstm = nn.LSTM(input_size=embedding_size,num_layers=1, hidden_size=hidden_size, batch_first=True)
self.out = nn.Linear(hidden_size, output_size)
self.softmax = nn.LogSoftmax(dim=1)
def forward(self, my_input, hidden):
print(type(my_input), type(hidden))
output, hidden = self.lstm(my_input, hidden)
output = self.softmax(self.out(output[0]))
return output, hidden
def initHidden(self):
return Variable(torch.zeros(1, self.batch_size, self.hidden_size)).cuda()
但是,当我使用以下命令运行它时:
decoder=DecoderLSTMwithBatchSupport(vocabularySize,batch_size, 300, vocabularySize)
decoder.cuda()
decoder_input=np.zeros([batch_size,vocabularySize])
for i in range(batch_size):
decoder_input[i] = embeddings[SOS_token]
decoder_input=Variable(torch.from_numpy(decoder_input)).cuda()
decoder_hidden = (decoder.initHidden(),decoder.initHidden())
for di in range(target_length):
decoder_output, decoder_hidden = decoder(decoder_input.view(1,batch_size,-1), decoder_hidden)
我得到他以下错误:
预期 hidden[0] 大小 (1, 1, 300),得到 (1, 50, 300)
为了使模型期望成批的隐藏状态,我缺少什么?