【PyTorch】常见错误: RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)

错误:

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

问题原因:

错误内容就在类型不匹配,根据报错内容可以看出Input type为torch.FloatTensor(CPU数据类型),而weight type(即网络权重参数这些)为torch.cuda.FloatTensor(GPU数据类型)。

解决办法:

既然网络参数是GPU类型,那解决方法就是将输入类型转变为GPU类型,需要使用到cuda,没有cuda就解决不了。实现方法有两种:

1,

device = torch.device('cuda:0')
inputs = inputs.to(device)

2,

inputs = inputs.cuda()

总结:

device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(torch.cuda.is_available())

inputs = inputs.to(device)        	# 方法一:将input这个tensor转换成了CUDA 类型
inputs = inputs.cuda()				# 方法二:将input这个tensor转换成了CUDA 类型

类似错误:

若与上面错误是反的,即
RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

那就同理,对net进行转换。

1,

device = torch.device('cuda:0')
net= net.to(device)

2,

net = net.cuda()
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值