AttributeError: 'tuple' object has no attribute 'dim', when feeding input to Pytorch LSTM network

AttributeError: 'tuple' object has no attribute 'dim', when feeding input to Pytorch LSTM network

我正在尝试 运行 以下代码:

import matplotlib.pylab as plt
import numpy as np
import torch
import torch.nn as nn

class LSTM(nn.Module):
    def __init__(self, input_shape, n_actions):
        super(LSTM, self).__init__()

        self.lstm = nn.LSTM(input_shape, 12)
        self.hidden2tag = nn.Linear(12, n_actions)

    def forward(self, x):
        out = self.lstm(x)
        out = self.hidden2tag(out)
        return out


state = [(1,2,3,4,5),(2,3,4,5,6),(3,4,5,6,7),(4,5,6,7,8),(5,6,7,8,9),(6,7,8,9,0)]

device = torch.device("cuda")
net = LSTM(5, 3).to(device)

state_v = torch.FloatTensor(state).to(device)

q_vals_v = net(state_v.view(1, state_v.shape[0], state_v.shape[1]))
_, action = int(torch.max(q_vals_v, dim=1).item())

还有 returns 这个错误:

Traceback (most recent call last):
  File "/home/dikkerj/Documents/PycharmProjects/LSTMReactor/QuestionWhosebug.py", line 26, in <module>
    q_vals_v = net(state_v.view(1, state_v.shape[0], state_v.shape[1]))
  File "/home/dikkerj/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/dikkerj/Documents/PycharmProjects/LSTMReactor/QuestionWhosebug.py", line 15, in forward
    out = self.hidden2tag(out)
  File "/home/dikkerj/.local/lib/python3.5/site-packages/torch/nn/modules/module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/dikkerj/.local/lib/python3.5/site-packages/torch/nn/modules/linear.py", line 55, in forward
    return F.linear(input, self.weight, self.bias)
  File "/home/dikkerj/.local/lib/python3.5/site-packages/torch/nn/functional.py", line 1022, in linear
    if input.dim() == 2 and bias is not None:
AttributeError: 'tuple' object has no attribute 'dim'

有人知道如何解决这个问题吗? (摆脱张量是一个元组,以便它可以被送入 LSTM 网络)

首先在 numpy 数组中转换你的状态:

state = np.array(state)

PyTorch 可能在 API.

中缺少 np.asarray

pytorch LSTM returns 一个元组。
所以你会得到这个错误,因为你的线性层 self.hidden2tag 无法处理这个元组。

所以改变:

out = self.lstm(x)

out, states = self.lstm(x)

这将通过拆分元组来修复您的错误,以便 out 只是您的输出张量。

out 然后存储隐藏状态,而 states 是另一个包含最后隐藏状态和单元状态的元组。

你也可以看这里:
https://pytorch.org/docs/stable/nn.html#torch.nn.LSTM

最后一行会出现另一个错误,因为 max() returns 也是一个元组。但这应该很容易修复并且是不同的错误:)