pytorch 简单RNN错误“输入Tensor和隐藏Tensor不在同一设备上,发现输入Tensor位于cuda:0,隐藏Tensor位于cpu”如何?

mgdq6dx1  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(220)

我正在使用PyTorch开发一个基本的RNN-NLP分类器,并尝试使用CUDA进行加速。(在Google_Colab上)但是,我无法解决这个错误。代码是这样写的。

错误消息

输入Tensor和隐藏Tensor不在同一设备上,发现输入Tensor位于cuda:0,隐藏Tensor位于cpu

RNN类

class RNN(nn.Module):
  def __init__(self, vocab_size, emb_size, hidden_size, output_size):
    super().__init__()
    self.hidden_size = hidden_size
    self.emb = nn.Embedding(vocab_size, emb_size)
    self.rnn = nn.RNN(emb_size, hidden_size, nonlinearity='tanh', batch_first=True)
    self.fc = nn.Linear(hidden_size, output_size)

  def forward(self, x):
    self.batch_size = x.size()[0]
    hidden = self.init_hidden()
    emb = self.emb(x)
    out, hidden = self.rnn(emb, hidden)
    out = self.fc(out[:, -1, :])
    return out

  def init_hidden(self):
    hidden = torch.zeros(1, self.batch_size, self.hidden_size)
    return hidden

器械

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

设置变量

VOCAB_SIZE = len(word_id.keys()) +1
EMB_SIZE = 300
OUTPUT_SIZE = 4
HIDDEN_SIZE = 50

model = RNN(VOCAB_SIZE,EMB_SIZE, HIDDEN_SIZE, OUTPUT_SIZE)

model = model.to(device)

预测

for i in range(10):
  # datasetet の、リスト0indexに入力要素
  X, y = dataset_train[i]
  X = X.to(device)
  print(torch.softmax(model(X.unsqueeze(0)), dim=1))

此代码在CPU上工作。但是,不能在“GPU”上工作。遵循此错误,我尝试做一些修复代码。例如)hidden.to(设备),,,,
但是,我不能解决......请告诉我怎么解决。非常感谢我的问题。

j9per5c4

j9per5c41#

难道做一些像下面这样的工作就不行了吗?

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

class RNN(nn.Module):
  def __init__(self, vocab_size, emb_size, hidden_size, output_size):
    super().__init__()
    self.hidden_size = hidden_size
    self.emb = nn.Embedding(vocab_size, emb_size)
    self.rnn = nn.RNN(emb_size, hidden_size, nonlinearity='tanh', batch_first=True)
    self.fc = nn.Linear(hidden_size, output_size)
    self.to(device)

  def forward(self, x):
    self.batch_size = x.size()[0]
    hidden = self.init_hidden()
    emb = self.emb(x)
    out, hidden = self.rnn(emb, hidden)
    out = self.fc(out[:, -1, :])
    return out

  def init_hidden(self):
    hidden = torch.zeros(1, self.batch_size, self.hidden_size).to(device)
    return hidden

相关问题