后端CPU的预期对象,但参数#4'mat1'获得了后端CUDA

时间:2019-05-14 01:09:41

标签: backend pytorch

我正在尝试在数据集中重新训练resnet50的FC层。加载数据集和预训练模型后,我将两者都移到了设备上,但是我得到了

  
    

错误:     后端CPU的预期对象,但参数#4'mat1'获得了后端CUDA

  

我在网上进行了大量搜索,发现这种错误主要是由于模型未移动或模型输入未移动到GPU引起的。我都做到了,但还是没有运气。这是代码和错误。

    if steps % print_every == 0:
        test_loss = 0
        accuracy = 0
        resnet50.eval()
        with torch.no_grad():
            for inputs, labels in testloader:
                inputs, labels = inputs.to(device),labels.to(device)
                logps = resnet50.forward(inputs)
                batch_loss = criterion(logps, labels)
                test_loss += batch_loss.item()

                ps = torch.exp(logps)
                top_p, top_class = ps.topk(1, dim=1)
                equals = top_class == labels.view(*top_class.shape)

               accuracy+=torch.mean(equals.type(torch.FloatTensor)).item()
        train_losses.append(running_loss/len(trainloader))
        test_losses.append(test_loss/len(testloader))                    
        print(f"Epoch {epoch+1}/{Epochs}.. "
              f"Train loss: {running_loss/print_every:.3f}.. "
              f"Test loss: {test_loss/len(testloader):.3f}.. "
              f"Test accuracy: {accuracy/len(testloader):.3f}")
        running_loss = 0
        resnet50.train()

0 个答案:

没有答案