【问题标题】:Two tensors are on the same device, but I get the error: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu [closed]两个张量在同一个设备上,但我收到错误:预期所有张量都在同一个设备上,但找到至少两个设备,cuda:0 和 cpu [关闭]
【发布时间】:2021-03-23 13:55:05
【问题描述】:

我有:

def loss_fn(self, pred, truth):        
    truth_flat = torch.reshape(truth, (truth.size(0),-1)).to(truth.device)
    pred_flat = torch.reshape(pred, (pred.size(0),-1)).to(pred.device)
    
    stoi_loss = NegSTOILoss(sample_rate=16000)(pred_flat, truth_flat)
    print('truth', truth.size(), truth_flat.size(), stoi_loss)

    return torch.nn.MSELoss()(pred, truth)

如您所见,我确保它在同一设备上,但我仍然收到错误:

Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu

有什么想法吗?

【问题讨论】:

    标签: python pytorch tensor


    【解决方案1】:

    您正在分配给两个不同的设备,truth.devicepred.device

    【讨论】:

    • 它们在同一台设备上
    • @Shamoon:但 PyTorch 显然不这么认为。只有你们一个人是对的......
    猜你喜欢
    • 2022-10-13
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2020-12-06
    • 2023-02-16
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多