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

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

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

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

  1. 嵌入张量
  2. 将该嵌入转换回 float32 张量
  3. 重塑嵌入形状(batch_size、seq_len、input_size)
  4. 通过lstm。

嵌入后我已经转换回 float32 张量,所以我知道为什么会出现此错误。

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()}")
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] 不起作用,因为整数不能被索引。

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

output, hidden = lstm(sequence)