本文介绍了pytoch RuntimeError:尺寸超出范围(预计在[-1,0]范围内,但得到1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在演员和评论家中使用LSTM来训练演员评论模型.我对这一切还是陌生的,不明白为什么"RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)"会来.

I am trying to train a Actor Critic Model with LSTM in both actor and critic.I am new to all this and can not understand why "RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)" is comming.

我正在从演员传播并出现错误

I am forwardPropagating from actor and getting error

下面是我的代码和错误消息.我正在使用pytorch版本0.4.1

below is my code and error message.I am using pytorch version 0.4.1

有人可以帮忙检查一下此代码出了什么问题.

Can someone please help to check what is wrong with this code.

import os
import time
import random
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import StandardScaler
import torch
import torch.nn as nn
import torch.nn.functional as F
from random import random as rndm
from torch.autograd import Variable
from collections import deque

torch.set_default_tensor_type('torch.DoubleTensor')


class Actor(nn.Module):

  def __init__(self, state_dim, action_dim, max_action):
    super(Actor, self).__init__()
    self.lstm = nn.LSTMCell(state_dim, 256)
    self.layer_1 = nn.Linear(256, 400)
    self.layer_2 = nn.Linear(400, 300)
    self.layer_3 = nn.Linear(300, action_dim)
    self.hx = torch.zeros(1,256)
    self.cx = torch.zeros(1,256)
    self.max_action = max_action

  def forward(self, x):
    self.hx, self.cx = self.lstm(x, (self.hx, self.cx))
    x = F.relu(self.layer_1(self.hx))
    x = F.relu(self.layer_2(x))
    x = self.max_action * torch.tanh(self.layer_3(x))
    return x

state_dim = 3
action_dim = 3
max_action = 1

policy = Actor(state_dim, action_dim, max_action)

s = torch.tensor([20,20,100])
next_action = policy(s)

,错误消息是:

next_action = policy(s)
Traceback (most recent call last):

  File "<ipython-input-20-de717f0ad3d2>", line 1, in <module>
    next_action = policy(s)

  File "C:\Users\granthjain\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)

  File "<ipython-input-4-aed4daf511cb>", line 14, in forward
    self.hx, self.cx = self.lstm(x, (self.hx, self.cx))

  File "C:\Users\granthjain\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 477, in __call__
    result = self.forward(*input, **kwargs)

  File "C:\Users\granthjain\anaconda3\lib\site-packages\torch\nn\modules\rnn.py", line 704, in forward
    self.check_forward_input(input)

  File "C:\Users\granthjain\anaconda3\lib\site-packages\torch\nn\modules\rnn.py", line 523, in check_forward_input
    if input.size(1) != self.input_size:

RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)

我正在使用pytorch版本0.4.1

I am using pytorch version 0.4.1

有人可以帮忙检查一下此代码出了什么问题.

Can someone please help to check what is wrong with this code.

推荐答案

知道了.

lstm层的输入具有不同的形状. https://pytorch.org/docs/master/generated/torch. nn.LSTMCell.html

The input of the lstm layer has different shape.https://pytorch.org/docs/master/generated/torch.nn.LSTMCell.html

这篇关于pytoch RuntimeError:尺寸超出范围(预计在[-1,0]范围内,但得到1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:56