【问题标题】:Stuck at Collab RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:卡在 Collab RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cpu and cuda:
【发布时间】:2021-12-24 09:31:23
【问题描述】:

我检查并看到我已经在 GPU (context_vector) 中定义了我的张量,但是当我在 Colab 上运行时,它总是显示错误“预期所有张量都在同一设备上,但发​​现至少两个设备、cpu 和 cuda" 错误轨迹是这样的:

RuntimeError                              Traceback (most recent call last)
<ipython-input-16-bf7edd8e474b> in <module>()
     85     for context, target in data:
     86         context_vector = make_context_vector(context)
---> 87         log_probs = model(context_vector)
     88         total_loss += loss_function(log_probs, torch.tensor([target]).to(device))
     89     #optimize at the end of each epoch

这是我的 make_context_vector 函数:

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')    
def make_context_vector(context):
    return torch.tensor(context, dtype=torch.long).to(device)

这是我的转发功能:

def forward(self, inputs):
        embeds = sum(self.embeddings(inputs)).view(1,-1)
        # print('embeds size: {}'.format(embeds.shape))
        out = self.linear1(embeds)
        out = self.activation_function1(out)
        # print('out1 size: {}'.format(out.shape))
        out = self.linear2(out)
        out = self.activation_function2(out)
        return out

#TRAINING
for epoch in range(50):
    total_loss = 0
    for context, target in data:
        context_vector = make_context_vector(context)  
        log_probs = model(context_vector)
        total_loss += loss_function(log_probs, torch.tensor([target], device = device))
    #optimize at the end of each epoch
    optimizer.zero_grad()
    total_loss.backward()
    optimizer.step()

请大家帮忙! 谢谢

【问题讨论】:

    标签: python gpu google-colaboratory


    【解决方案1】:

    哦,我的朋友支持我我发现不仅要为输入设置GPU,还需要为模型设置GPU。 因此,在训练代码中,我将其更改为:

    model = CBOW(vocab_size, embedding_dim)**.to(device)**
    

    然后,它运行良好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-27
      • 2021-03-03
      • 2021-07-18
      • 2021-05-11
      • 2022-01-03
      • 2020-02-01
      • 2021-03-26
      • 2020-03-24
      相关资源
      最近更新 更多