Pytorch —— AttributeError: ‘DataParallel’ object has no attribute ‘xxxx’

在 pytorch 多GPU训练下,存储 整个模型 ( 而不是model.state_dict() )后再调用模型可能会遇到下面的情况:

AttributeError: ‘DataParallel’ object has no attribute ‘xxxx’

解决的方法是:

model = torch.load('path/to/model')
if isinstance(model,torch.nn.DataParallel):
		model = model.module

#下面就可以正常使用了
model.eval()
...
...
...

版权声明:本文为Chris_zhangrx原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。