我在使用pytorch预训练模型之后:
net=net_construct(3,[1,2,1,1],10)
net=nn.DataParallel(net) # cuda()
# 加载模型的参数
net.load_state_dict(torch.load("/workshop/user_data/code/C-ResNet-master/C_BasicBlock_A_1,2,1,1_cifar10.pt"))
然后用一个张量传入模型中,看看最后会不会输出类别:
y = net(torch.randn(1,3,32,32))
print(y.size())
发生了报错,报错如下:
runtimeerror: module must have its parameters and buffers on device cuda:0 (device_ids[0]) but found one of them on device: cpu
解决方案,把net转化成gpu类型模型:
# 加上这一句 net=net.cuda()
net=net.cuda()
y = net(torch.randn(1,3,32,32))
print(y.size())
问题得解,输出:
torch.Size([1, 10])