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

博客指出PyTorch出现AttributeError: ‘module’ object has no attribute ‘_rebuild_tensor_v2’报错,原因是训练模型用新版本PyTorch,加载时用旧版本。并给出解决办法,即在文件顶部添加特定代码。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值