Python 两个张量在同一个设备上,但我得到了一个错误:预期所有张量都在同一个设备上,但至少找到了两个设备,cuda:0和cpu

Python 两个张量在同一个设备上,但我得到了一个错误:预期所有张量都在同一个设备上,但至少找到了两个设备,cuda:0和cpu,python,pytorch,tensor,Python,Pytorch,Tensor,我有: 如您所见,我正在确保它位于同一台设备上,但仍然会出现错误: 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 = NegSTOILo

我有:

如您所见,我正在确保它位于同一台设备上,但仍然会出现错误:

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)

有什么想法吗?

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

它们位于同一个设备上device@Shamoon当前位置但派托克显然不这么认为。你们中只有一个人是对的。。。。。。
Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu