Pytorch 如何在LSTM中修复此错误RuntimeError:输入必须有3个维度,得到4个维度

Pytorch 如何在LSTM中修复此错误RuntimeError:输入必须有3个维度,得到4个维度,pytorch,lstm,Pytorch,Lstm,我面临这个错误。。如何在LSTM中修复此错误RuntimeError:输入必须有3个维度,得到4个维度 self.check_forward_args(input, hx, batch_sizes) File "/home/user/.conda/envs/renv/lib/python3.9/site-packages/torch/nn/modules/rnn.py", line 605, in check_forward_args self.

我面临这个错误。。如何在LSTM中修复此错误RuntimeError:输入必须有3个维度,得到4个维度


      self.check_forward_args(input, hx, batch_sizes)
  File "/home/user/.conda/envs/renv/lib/python3.9/site-packages/torch/nn/modules/rnn.py", line 605, in check_forward_args  
    self.check_input(input, batch_sizes)
  File "/home/user/.conda/envs/renv/lib/python3.9/site-packages/torch/nn/modules/rnn.py", line 198, in check_input
    raise RuntimeError(
RuntimeError: input must have 3 dimensions, got 4
代码行

 st_gcn(256, 256, kernel_size, 1, **kwargs),
    ))

    # initialize parameters for edge importance weighting
    if edge_importance_weighting:
        self.edge_importance = nn.ParameterList([
            nn.Parameter(torch.ones(self.A.size()))
            for i in self.st_gcn_networks
        ])
    else:
        self.edge_importance = [1] * len(self.st_gcn_networks)

    self.lstm = nn.LSTM(input_size=256, hidden_size=128, num_layers=32, batch_first=True) #lstm

    # fcn for prediction
    self.fcn = nn.Conv2d(256, num_class, kernel_size=1)
这里是错误。。顺便说一下,我用的是GPU 当我注释这些行时,错误消失了,但我必须申请forwad

        # forwad
        for gcn, importance in zip(self.st_gcn_networks, self.edge_importance):
            x, _ = gcn(x, self.A * importance)

        # global pooling
        x = F.avg_pool2d(x, x.size()[2:])
        x = x.view(N, M, -1, 1, 1).mean(dim=1)
        
        h_0 = torch.randn(32, 5, 128)
        c_0 = torch.randn(32, 5, 128)
        output, (hn, cn) = self.lstm(x, (h_0, c_0)) #lstm with input, hidden, and internal state
        #hn = hn.view(-1, self.hidden_size) #reshaping the data for Dense layer next
        # prediction
        x = self.fcn(hn)