【问题标题】:Understanding the output of LSTM predictions了解 LSTM 预测的输出
【发布时间】:2021-06-30 07:19:14
【问题描述】:
  • 这是一个 15 类分类模型,OUTPUT_DIM = 15。我正在尝试输入一个像 'hi my name is' => [1,43,2,56] 这样的频率向量。

  • 当我调用predictions = model(x_train[0]) 时,我得到一个大小为torch.Size([100, 15]) 的数组,而不是像这样具有15 个类的一维数组:torch.Size([15])。发生了什么?为什么这是输出?我该如何解决?提前感谢您,下面有更多信息。

型号(from main docs)如下:

import torch.nn as nn

class RNN(nn.Module):
    def __init__(self, vocab_size, embedding_dim, hidden_dim, output_dim):
        
        super().__init__()
        
        self.word_embeddings = nn.Embedding(vocab_size, embedding_dim)
        self.lstm = nn.LSTM(embedding_dim, hidden_dim)
        self.hidden2tag = nn.Linear(hidden_dim, output_dim)
        
    def forward(self, text):
                
        embeds = self.word_embeddings(text)
        lstm_out, _ = self.lstm(embeds.view(len(text), 1, -1))
        tag_space = self.hidden2tag(lstm_out.view(len(text), -1))
        tag_scores = F.log_softmax(tag_space, dim=1)   

        return tag_scores

参数:

INPUT_DIM = 62288
EMBEDDING_DIM = 64
HIDDEN_DIM = 128
OUTPUT_DIM = 15

【问题讨论】:

    标签: machine-learning nlp pytorch lstm text-processing


    【解决方案1】:

    Pytorch 中的 LSTM 函数不仅返回最后一个时间步的输出,还返回所有输出(这在某些情况下很有用)。因此,在您的示例中,您似乎正好有 100 个时间步长(时间步长的数量就是您的序列长度)。

    但是由于您正在进行分类,因此您只关心最后的输出。你通常可以这样得到它:

    outputs, _ = self.lstm(embeddings)
    # shape: batch_size x 100 x 15
    output = outputs[:, -1]    
    # shape: batch_size x 1 x 15
    

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 2021-10-15
      • 2016-02-05
      • 2017-03-19
      • 2018-02-16
      • 2020-02-23
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多