成功解决RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be

error code

Cause of the problem: This is because the input type does not match the type of the weight type. It can be noticed that the input type does not have cuda, and the weight type is loaded on cuda, that is, with gpu. (The or after the error code can be ignored)

Solution: Since the error is caused by type inconsistency, then convert the type to be consistent. See the code below for details:

1.
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
input=input.to(device)
2.
input=input.cuda()#这两种代码任选其一即可

Guess you like

Origin blog.csdn.net/m0_58508552/article/details/124126319