pytorch报错:AttributeError: ‘module’ object has no attribute ‘_rebuild_tensor_v2’
原因:由于训练模型时使用的是新版本的pytorch,而加载时使用的是旧版本的pytorch。
解决方法:
在文件的顶部加上这段代码
import torch._utils
try:
torch._utils._rebuild_tensor_v2
except AttributeError:
def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks):
tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride)
tensor.requires_grad = requires_grad
tensor._backward_hooks = backward_hooks
return tensor
torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2
来源:CSDN
作者:summer2day
链接:https://blog.csdn.net/summer2day/article/details/88670643