pytorch报错:AttributeError: 'module' object has no attribute '_rebuild_tensor_v2'

forward from:

https://blog.csdn.net/qq_24305433/article/details/80844548

 

The use of the training model is a new version of pytorch, and is loaded using an older version of pytorch

Solution:

1, since it is pytorch older version, that the easiest solution is of course a simple upgrade about pytorch ok.

2, the big foreign gods gave another solution is to add the following code at the beginning of the program, which can make older versions pytorch compatible with the new version pytorch, reference links 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

 

Guess you like

Origin www.cnblogs.com/rainsoul/p/11314278.html