关于 python:将嵌入序列传递给 LSTM 并获取 TypeError: \\’int\\’ object is not subscriptable

Passing embedded sequence to LSTM and getting TypeError: 'int' object is not subscriptable

我在这里有一些非常基本的 pytorch 代码,我正在尝试通过最终将成为我的前向函数的内容来测试运行输入张量。

目标:在嵌入每个词数后,将句子视为单个输入序列。

  • 嵌入张量
  • 将该嵌入转换回 float32 张量
  • 将嵌入重塑为形状(batch_size、seq_len、input_size)
  • 通过lstm。
  • 嵌入后我已经转换回了一个 float32 张量,所以我知道为什么我会收到这个错误。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    hidden_size=10
    embedding = nn.Embedding(VOC.n_words, hidden_size)
    lstm = nn.LSTM(hidden_size, hidden_size, # Will output 2x hidden size
                   num_layers=1, dropout=0.5,
                   bidirectional=True, batch_first=True)

    print("Input tensor",idx_sentence)
    # Forward test
    embedded = embedding(idx_sentence.long())
    embedded = torch.tensor(embedded, dtype=torch.float32)
    print(f"embedding: {embedded.size()}")

    # reshape to (batch_size, seq_len, input_size)
    sequence = embedded.view(1,-1,hidden_size)
    print(f"sequence shape: {sequence.size()}")

    output, hidden = lstm(sequence, hidden_size)
    print(f"output shape: {output.size()}")
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    Input tensor tensor([ 3., 20., 21., 90.,  9.])
    embedding: torch.Size([5, 10])
    sequence shape: torch.Size([1, 5, 10])
    /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:10: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
      # Remove the CWD from sys.path while we load stuff.
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-116-ab3d6ed0e51c> in <module>()
         16
         17 # Input have shape (seq_len, batch, input_size)
    ---> 18 output, hidden = lstm(sequence, hidden_size)
         19 print(f"output shape: {output.size()}")

    2 frames
    /usr/local/lib/python3.6/dist-packages/torch/nn/modules/rnn.py in check_forward_args(self, input, hidden, batch_sizes)
        520         expected_hidden_size = self.get_expected_hidden_size(input, batch_sizes)
        521
    --> 522         self.check_hidden_size(hidden[0], expected_hidden_size,
        523                                'Expected hidden[0] size {}, got {}')
        524         self.check_hidden_size(hidden[1], expected_hidden_size,

    TypeError: 'int' object is not subscriptable


    LSTM 接受两个输入,如 nn.LSTM - Inputs:

    中所述

    • input:输入序列
    • (h_0, c_0):具有初始隐藏状态 h_0 和初始单元状态 c_0 的元组。

    但是您将 hidden_size 作为第二个参数传递,它是 int 而不是 tuple。当元组被解包时它会失败,因为 hidden_size[0] 不起作用,因为整数不能被索引。

    第二个参数是可选的,如果您不提供它,隐藏和单元格状态将默认为零。这通常是您想要的,因此您可以不使用它:

    1
    output, hidden = lstm(sequence)