【问题标题】:I meet a problem which is "RuntimeError: Input and hidden tensors are not at the same device, found input tensor at cuda:0 and hidden tensor at cpu"我遇到了一个问题,即“RuntimeError:输入和隐藏张量不在同一个设备上,在 cuda:0 找到输入张量,在 cpu 找到隐藏张量”
【发布时间】:2020-05-01 22:37:24
【问题描述】:

我猜隐藏张量是我需要在 RNN 开始时初始化的 Tenor。所以我通过 .cuda() 设置了 h0 和 c0,但这没有用。以下是我的代码。请问谁能帮帮我?

class LSTM_net(nn.Module):
def __init__(self, Embedding, vocab, label, batch):
    super(LSTM_net, self).__init__()
    self.Hidden_dim = Embedding
    self.Embedding = nn.Embedding(vocab, Embedding)  
    self.lstm = nn.LSTM(Embedding, self.Hidden_dim)  # 32 * 32
    self.hidden2label = nn.Linear(Embedding, label)
    self.hidden = self.init_hidden(batch)

def init_hidden(self, batch):  
    # the first is the hidden h
    # the second is the cell c

    if torch.cuda.is_available():
        return (autograd.Variable(torch.zeros(1, batch, self.Hidden_dim)),
                autograd.Variable(torch.zeros(1, batch, self.Hidden_dim)))
    else:
        return (autograd.Variable(torch.zeros(1, batch, self.Hidden_dim).cuda()),
                autograd.Variable(torch.zeros(1, batch, self.Hidden_dim).cuda()))

def forward(self, sentence):
    # 64 * 52
    x = self.Embedding(sentence)
    # x = 64 * 52 * 32
    x = x.permute(1, 0, 2)
    # x = 52 * 64 * 32
    lstm_y, lstm_hidden = self.lstm(x, self.hidden)
    # lstm_y = batch * 52 * 32
    # input: lstm_y[-1] = batch * 32
    y = self.hidden2label(lstm_y[-1])
    # y = batch * 5
    log_probs = F.log_softmax(y)
    return log_probs

【问题讨论】:

标签: pytorch lstm recurrent-neural-network


【解决方案1】:

最初,我认为您在将 .cuda() 包装到变量中之前没有将其放入,就像这里描述的那样: https://discuss.pytorch.org/t/tensor-cuda-vs-variable-cuda/12549

但是,您在初始化时已正确转换它并将其包装在变量中。你的小错误是你的循环。在您的if 语句 中,当您正在使用 GPU 时,您 激活 cuda,而是在它处于 else 循环时激活。不管有没有 GPU,它都会失败。

与其纠正你的 if-else,我可以建议你推荐的做法:

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

class LSTM_net(nn.Module):
    ...
    ...
    def init_hidden(self, batch):  
        return (autograd.Variable(torch.zeros(1, batch, self.Hidden_dim).to(device)),
                autograd.Variable(torch.zeros(1, batch, self.Hidden_dim).to(device)))

使用 .to(device) 可以避免大部分设备错误。

【讨论】:

    【解决方案2】:

    错误可能源于您的代码执行方式。您可能会遇到此问题,因为您在具有可用 GPU 的机器上运行此代码,这会强制隐藏张量为 cuda 张量。但是,在主代码中执行时,如果您提供 cpu 输入张量 pytorch 会引发不匹配错误。为防止这种情况发生,请按如下所述进行更改。

    def forward(self, sentence):
        # 64 * 52
        x = self.Embedding(sentence)
        # x = 64 * 52 * 32
        x = x.permute(1, 0, 2).cuda() # <----- this change; add if condition to check for GPUs
        # x = 52 * 64 * 32
        lstm_y, lstm_hidden = self.lstm(x, self.hidden)
        # lstm_y = batch * 52 * 32
        # input: lstm_y[-1] = batch * 32
        y = self.hidden2label(lstm_y[-1])
        # y = batch * 5
        log_probs = F.log_softmax(y)
        return log_probs
    

    【讨论】:

      猜你喜欢
      • 2020-12-06
      • 1970-01-01
      • 2019-06-27
      • 2020-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-30
      • 1970-01-01
      相关资源
      最近更新 更多