Deep learning 解码器LSTM Pytorch的图像字幕示例输入大小

Deep learning 解码器LSTM Pytorch的图像字幕示例输入大小,deep-learning,lstm,torch,pytorch,Deep Learning,Lstm,Torch,Pytorch,我是Pytorch的新手,在图像字幕中有一个疑问。在DcoderRNN类中,lstm定义为: self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True) 在前进功能中 embeddings = self.embed(captions) embeddings = torch.cat((features.unsqueeze(1), embeddings), 1) 我们首先嵌入标题,然后使用EncoderCN

我是Pytorch的新手,在图像字幕中有一个疑问。在DcoderRNN类中,lstm定义为:

self.lstm = nn.LSTM(embed_size, hidden_size, num_layers, batch_first=True)
在前进功能中

embeddings = self.embed(captions)
embeddings = torch.cat((features.unsqueeze(1), embeddings), 1)
我们首先嵌入标题,然后使用EncoderCNN的上下文功能对嵌入进行concat,但是concat增加了嵌入大小,我们如何将其转发到lstm?因为lstm的输入大小已经定义为嵌入大小

embeddings = torch.cat((features.unsqueeze(1), embeddings), 1)

我是不是遗漏了什么?提前感谢。

您可以分析所有输入和输出张量的形状,这样您就更容易理解需要进行哪些更改

假设:字幕=
B x s
其中
s
=句子(字幕)长度

现在,embeddings=
bxse
其中
E
=嵌入大小

embeddings = torch.cat((features.unsqueeze(1), embeddings), 1)
这里,embeddings=
bx(S+1)xe

我的理解是你在这里做错了。我想应该沿轴=2连接特征。因为您可能希望将图像特征与标题中每个单词的单词嵌入连接起来。因此,如果你这样做:

embeddings = torch.cat((features.unsqueeze(1), embeddings), 2)
它的结果是,嵌入=
bxsx(E+F)
其中
E+F
=嵌入大小+img大小

然后您需要修改您的LSTM定义,如下所示

self.lstm = nn.LSTM(embed_size+img_feat_size, hidden_size, num_layers, batch_first=True)

我的经验是,通常,人们将图像特征与单词特征连接起来,并将其传递给LSTM层

非常感谢,我在show n tell图像字幕纸中发现,它们只将上下文传递到解码器的第一个时间步骤。非常感谢你的回复