LSTM错误:AttributeError:'tuple'对象没有属性'dim' [英] LSTM error: AttributeError: 'tuple' object has no attribute 'dim'

查看:44
本文介绍了LSTM错误:AttributeError:'tuple'对象没有属性'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'

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

/edit:等待,存在*input!?如何覆盖此设置?

推荐答案

您将无法在nn.Sequential中使用nn.RNN,因为nn.LSTM层将输出包含(1)输出功能和(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])

这篇关于LSTM错误:AttributeError:&#39;tuple&#39;对象没有属性&#39;dim&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆