将嵌入式序列传递给LSTM并获得TypeError:'int'对象不可下标

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

目标:嵌入每个单词编号后,将句子作为单个输入序列处理。

  1. 嵌入张量
  2. 将嵌入转换回float32张量
  3. 将形状整形为形状(batch_size,seq_len,input_size)
  4. 通过lstm。

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

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_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],TypeError: 'int' object is not subscriptable
iCMS 回答:将嵌入式序列传递给LSTM并获得TypeError:'int'对象不可下标

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

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

但是您将hidden_size作为第二个参数传递,它是一个int而不是一个tuple。打开元组的包装后,由于hidden_size[0]不起作用,因为整数无法索引,因此它失败了。

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

output,hidden = lstm(sequence)
本文链接:https://www.f2er.com/2291897.html

大家都在问