RuntimeError: set_storage_offset is not allowed on a Tensor created from .data or .detach().

报错信息

Traceback (most recent call last):
  File "selfdeblur_ycbcr.py", line 91, in
    net_input_kernel.squeeze_()
RuntimeError: set_storage_offset is not allowed on a Tensor created from .data or .detach().
If your intent is to change the metadata of a Tensor (such as sizes / strides / storage / storage_offset)
without autograd tracking the change, remove the .data / .detach() call and wrap the change in a `with torch.no_grad():` block.
For example, change:
    x.data.set_(y)
to:
    with torch.no_grad():
        x.set_(y)

报错代码

net_input_kernel = get_noise(n_k, INPUT, (1, 1)).type(dtype).detach()
net_input_kernel.squeeze_()

 

报错原因与解决方案

应该是已经.data()或者是.detach()的Tensor不能进行set_storage_offset的操作,并且报错里其实已经给出了解决方案,即通过with torch.no_grad():代替.detach()或.data()

 

改正后的代码

net_input_kernel = get_noise(n_k, INPUT, (1, 1)).type(dtype)
with torch.no_grad():
     net_input_kernel.squeeze_()

 

改正成功

你可能感兴趣的:(#,Code,pytorch,squeeze,no_grad)