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

简介: 问题解决: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
目录
相关文章
|
2月前
|
计算机视觉 Python
解决 NoneType‘ object has no attribute ‘astype’ 问题
解决 NoneType‘ object has no attribute ‘astype’ 问题
52 0
|
16天前
|
Python
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
【已解决】AttributeError: ‘Index‘ object has no attribute ‘to_list‘
7 0
|
2月前
|
机器学习/深度学习 监控 数据可视化
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
【已解决】 ‘Conv2d’ object has no attribute ‘register_full_backward_hook’
|
2月前
|
安全 计算机视觉 Python
【已解决】attributeerror: ‘FreeTypeFont‘ object has no attribute ‘getsize‘
【已解决】attributeerror: ‘FreeTypeFont‘ object has no attribute ‘getsize‘
|
2月前
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
AttributeError: 'NoneType' object has no attribute 'to_capabilities'
459 0
|
2月前
AttributeError ‘NoneType‘ object has no attribute ‘to_capabilities‘
AttributeError ‘NoneType‘ object has no attribute ‘to_capabilities‘
159 0
|
2月前
|
数据采集
运行爬虫时报错AttributeError—— 'str' object has no attribute 'capabilities'
运行爬虫时报错AttributeError—— 'str' object has no attribute 'capabilities'
439 0
|
2月前
|
Java
Java Object 类
5月更文挑战第16天
|
3天前
|
Java
Java中的Object类 ( 详解toString方法 | equals方法 )
Java中的Object类 ( 详解toString方法 | equals方法 )
|
2月前
|
存储 算法 Java
滚雪球学Java(42):探索对象的奥秘:解析Java中的Object类
【5月更文挑战第17天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
28 2
滚雪球学Java(42):探索对象的奥秘:解析Java中的Object类