欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace.

程序员文章站 2022-03-01 12:38:30
...

1. 问题描述:

在使用utils中的make_grid函数时

utils.make_grid(fmap, normalize=True, scale_each=True, nrow=nrow)

如果出现:

Traceback (most recent call last):
  File "visualization.py", line 94, in <module>
    fmap_grid = vutils.make_grid(fmap, normalize=True, scale_each=True, nrow=nrow)
  File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 75, in make_grid
    norm_range(t, range)
  File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 71, in norm_range
    norm_ip(t, float(t.min()), float(t.max()))
  File "/home/lab/anaconda3/envs/instagan/lib/python3.6/site-packages/torchvision/utils.py", line 64, in norm_ip
    img.clamp_(min=min, max=max)
RuntimeError: Output 0 of UnbindBackward is a view and is being modified inplace. This view is the output of a function that returns multiple views. Such functions do not allow the output views to be modified inplace. You should replace the inplace operation by an out-of-place one.

2. 问题解释:

为pytorch版本问题,在1.7 之后使用该函数汇报错。

3.解决:

进入utils.py文件,将第74行进行修改:

        if scale_each is True:
            for t in tensor:  # loop over mini-batch dimension
                norm_range(t, range)

修改后:

        if scale_each is True:
            ######## modified by yuss in 2022.02.15, for RuntimeError
            for idx, _ in enumerate([jj for jj in tensor]):
                t = tensor[idx]