LSTM 错误:AttributeError:“tuple”对象没有属性“dim”

Mar*_*l B 2 python lstm pytorch

我有以下代码:

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)
Run Code Online (Sandbox Code Playgroud)

我得到:

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'
Run Code Online (Sandbox Code Playgroud)

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

/编辑:等等,有一个*input!?我怎样才能覆盖这个?

Iva*_*van 7

您将无法在 ann.RNN内部使用 a nn.Sequential,因为图层nn.LSTM将输出一个包含(1)输出特征和(2)隐藏状态和单元状态的元组。

必须首先对输出进行解包,以便在后续层中使用输出特征:nn.Linear。例如,如果您对隐藏状态和单元状态感兴趣:

rnn = nn.LSTM(300, 300)
output, (h_n, c_n) = rnn(x)
Run Code Online (Sandbox Code Playgroud)

您可以定义一个自定义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
Run Code Online (Sandbox Code Playgroud)

这样:

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

>>> model(s).shape
torch.Size([1, 50, 7])
Run Code Online (Sandbox Code Playgroud)