LSTM error: AttributeError: 'tuple' object has no attribute 'dim'

LSTM error: AttributeError: 'tuple' object has no attribute 'dim'

我有以下代码:

import torch
import torch.nn as nn

model = nn.Sequential(
          nn.LSTM(300, 300),
          nn.Linear(300, 100),
          nn.ReLU(),
          nn.Linear(300, 7),
          )

s = torch.ones(1, 50, 300)
a = model(s)

我得到:

My-MBP:Desktop myname$ python3 testmodel.py 
Traceback (most recent call last):
  File "testmodel.py", line 12, in <module>
    a = model(s)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/container.py", line 117, in forward
    input = module(input)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/module.py", line 727, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/modules/linear.py", line 93, in forward
    return F.linear(input, self.weight, self.bias)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/torch/nn/functional.py", line 1688, in linear
    if input.dim() == 2 and bias is not None:
AttributeError: 'tuple' object has no attribute 'dim'

为什么?尺寸应该没问题。当在 model.forward 中定义 *input 时,我看到了此问题的相关修复,但我什至还没有实现任何东西。

/编辑:等等,有一个*input!?我该如何覆盖它?

您将无法使用 nn.RNN inside a nn.Sequential,因为 nn.LSTM 层将输出包含 (1)[ 的 元组 =30=] 输出特征和 (2) 隐藏状态和细胞状态。

必须首先解压缩输出才能在后续层中使用输出功能:nn.Linear。比如,如果您对隐藏状态和单元格状态感兴趣:

rnn = nn.LSTM(300, 300)
output, (h_n, c_n) = rnn(x)

您可以定义一个自定义 nn.Module 并实现一个简单的转发函数:

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()

        self.rnn = nn.LSTM(300, 300)
        
        self.body = nn.Sequential(
          nn.Linear(300, 100),
          nn.ReLU(),
          nn.Linear(100, 7)) # <- had it set to in_features=300

    def forward(self, x):
        x, _ = self.rnn(x) # <- ignore second output
        x = self.body(x)
        return x

这样:

>>> model = Model()
>>> s = torch.ones(1, 50, 300)

>>> model(s).shape
torch.Size([1, 50, 7])

错误是因为 nn.LSTM returns 你的输出和你的模型状态,这是一个包含隐藏状态和记忆状态的元组。

您可以通过定义自己的 nn.Module class 来修复它,returns 只是 LSTM 的输出。

class GetLSTMOutput(nn.Module):
    def forward(self, x):
        out, _ = x
        return out

model = nn.Sequential(
    nn.LSTM(300, 300),
    GetLSTMOutput(),
    nn.Linear(300, 100),
    nn.ReLU(),
    nn.Linear(300, 7))

GetLSTMOutputclass的forward方法是隐式调用的,前一层nn.LSTM(300, 300)的输出作为参数传递。然后 returns 只是输出部分,丢弃模型的状态。