AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

非 Y 不嫁゛ 提交于 2019-12-04 22:00:21

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!