问题解决:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

 于训练模型时使用的是新版本的pytorch,而加载时使用的是旧版本的pytorch。

解决办法:

  • 升级pytorch

看起来理所当然,其实有可能有坑。说不定还要转回来。

  • 桥接

 在程序开头添加下面的代码,即可以使老版本pytorch兼容新版本pytorch,参考链接https://discuss.pytorch.org/t/question-about-rebuild-tensor-v2/14560

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

猜你喜欢

转载自blog.csdn.net/quantum7/article/details/83511554