【问题标题】:Image Captioning Example input size of Decoder LSTM Pytorch解码器 LSTM Pytorch 的图像字幕示例输入大小
【发布时间】:2018-08-11 15:03:15
【问题描述】:

我是 Pytorch 的新手,在图像字幕 example code 中存在疑问。在 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 的上下文特征连接起来,但是连接会增加嵌入大小的大小,我们如何将其转发到 lstm?因为 lstm 的输入大小已经定义为 embed_size。

我在这里遗漏了什么吗?提前致谢。

【问题讨论】:

    标签: deep-learning lstm torch pytorch


    【解决方案1】:

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

    假设:标题 = B x S 其中S = 句子(标题)长度。

    embeddings = self.embed(captions)
    

    现在,嵌入 = B x S x E 其中E = embed_size。

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

    在这里,嵌入 = B x (S + 1) X E

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

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

    结果是,嵌入 = B X S X (E + F) 其中E + F = embed_size + img_feat_size

    那么你需要修改你的 LSTM 定义如下。

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

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

    【讨论】:

    • 非常感谢我在节目中发现 n tell image captioning paper 他们只将上下文传递给解码器的第一步。非常感谢您的回复
    猜你喜欢
    • 2018-09-24
    • 2021-07-25
    • 2020-08-21
    • 2021-11-02
    • 2019-10-23
    • 1970-01-01
    • 2021-08-19
    • 2017-07-27
    相关资源
    最近更新 更多