RuntimeError: expected device cuda:0 but got device cpu

RuntimeError: expected device cuda:0 but got device cpu

cuda = True if torch.cuda.is_available() else False

g_acc_t = ((pred_t.max(1)[1] == y_tar).float().mean())

方法:直接在变量后面加上cuda(见下图)

cuda = True if torch.cuda.is_available() else False

g_acc_t = (pred_t.cuda().max(1)[1] == y_tar.cuda()).float().mean()

或者

cuda = True if torch.cuda.is_available() else False
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor

g_acc_t = (pred_t.type(Tensor).max(1)[1] == y_tar.type(Tensor)).float().mean()

你可能感兴趣的:(Python3,python,深度学习,cuda)