码迷,mamicode.com
首页 > 其他好文 > 详细

pytorch版本问题:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

时间:2018-08-14 21:58:53      阅读:906      评论:0      收藏:0      [点我收藏+]

标签:tee   Fix   图片   error:   try   ref   网上   pytorch   statistic   

用pytorch加载训练好的模型的时候遇到了如下的问题:

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

技术分享图片

 

到网上查了一下是由于训练模型时使用的是新版本的pytorch,而加载时使用的是旧版本的pytorch。

解决方法:

1、既然是pytorch版本较老,那最简单的解决方法当然是简单的升级一下pytorch就ok了。

2、国外的大神给了另一种解决方法,就是在程序开头添加下面的代码,即可以使老版本pytorch兼容新版本pytorch,参考链接https://discuss.pytorch.org/t/question-about-rebuild-tensor-v2/14560

 1 import torch._utils
 2 try:
 3     torch._utils._rebuild_tensor_v2
 4 except AttributeError:
 5     def _rebuild_tensor_v2(storage, storage_offset, size, stride, requires_grad, backward_hooks):
 6         tensor = torch._utils._rebuild_tensor(storage, storage_offset, size, stride)
 7         tensor.requires_grad = requires_grad
 8         tensor._backward_hooks = backward_hooks
 9         return tensor
10     torch._utils._rebuild_tensor_v2 = _rebuild_tensor_v2

 

pytorch版本问题:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

标签:tee   Fix   图片   error:   try   ref   网上   pytorch   statistic   

原文地址:https://www.cnblogs.com/yourcool/p/9477972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!