梯度被原地修改,破坏了计算图

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [1, 18, 32, 32]] is at version 2; expected version 0 instead. Hint: the backtrace further above shows the operation that failed to compute its gradient.

出现错误片段

python 复制代码
def forward_slice(self, x_slice, x_channel, color, hp, cc_tsf, ctx_tsf, h_tsf, color_tsf, parameter_aggregation, entropy, entropy_mode):
    h = h_tsf(hp)
    support = h
    if color != None:
        clr = color_tsf(color)
        support = torch.cat([support, clr], dim=1)
    if x_channel != None:
        ch = cc_tsf(x_channel)
        support = torch.cat([support, ch], dim=1)

    x_slice_anchor = torch.zeros_like(x_slice).to(x_slice.device)
    ctx_anchor = ctx_tsf(x_slice_anchor)
    support_anchor = torch.cat([support, ctx_anchor], dim=1)
    parameters = parameter_aggregation(support_anchor)

    if entropy_mode == "gmm":
        mean_anchor,sigma_anchor,weight_anchor = torch.chunk(parameters, 3, dim=1)
        weight_anchor = F.softmax(weight_anchor, dim=1)
    else:
        mean_anchor,sigma_anchor = torch.chunk(parameters, 2, dim=1)
        weight_anchor = None
    probs_anchor = entropy.likelihood(x_slice, mean_anchor, sigma_anchor, weight_anchor)

    probs = torch.zeros_like(x_slice).to(x_slice.device)
    probs[:,:,0::2,0::2] = probs_anchor[:,:,0::2,0::2]
    probs[:,:,1::2,1::2] = probs_anchor[:,:,1::2,1::2]

    x_slice_anchor[:, :, 0::2, 0::2] = x_slice[:,:, 0::2, 0::2]
    x_slice_anchor[:,:, 1::2, 1::2] = x_slice[:,:, 1::2, 1::2]
    ctx_non_anchor = ctx_tsf(x_slice_anchor)
    support_non_anchor = torch.cat([support, ctx_non_anchor], dim=1)
    parameters_non_anchor = parameter_aggregation(support_non_anchor)

    if entropy_mode == "gmm":
        mean_non_anchor,sigma_non_anchor,weight_non_anchor = torch.chunk(parameters_non_anchor, 3, dim=1)
        weight_non_anchor = F.softmax(weight_non_anchor, dim=1)
    else:
        mean_non_anchor,sigma_non_anchor = torch.chunk(parameters_non_anchor, 2, dim=1)
        weight_non_anchor = None
    probs_non_anchor = entropy.likelihood(x_slice, mean_non_anchor, sigma_non_anchor, weight_non_anchor)
    probs[:,:,0::2,1::2] = probs_non_anchor[:,:,0::2,1::2]
    probs[:,:,1::2,0::2] = probs_non_anchor[:,:,1::2,0::2]
    return probs

错误原因:

x_slice_anchor[:, :, 0::2, 0::2] = x_slice[:,:, 0::2, 0::2]

x_slice_anchor[:,:, 1::2, 1::2] = x_slice[:,:, 1::2, 1::2]

这一步对x_slice_anchor进行了修改,但是x_slice_anchor在前面已经用到过,其已经在计算图中,虽然在数值上仍然等于0,但是对其修改会破坏原有的计算图,导致上述错误。

解决办法是新开一个tensor用来存储x_slice的对应位置参数。

所以在修改一个变量的时候,一定要慎重。

解决代码:

python 复制代码
def forward_slice(self, x_slice, x_channel, color, hp, cc_tsf, ctx_tsf, h_tsf, color_tsf, parameter_aggregation, entropy, entropy_mode):
        h = h_tsf(hp)
        support = h
        if color != None:
            clr = color_tsf(color)
            support = torch.cat([support, clr], dim=1)
        if x_channel != None:
            ch = cc_tsf(x_channel)
            support = torch.cat([support, ch], dim=1)
    
        x_slice_anchor = torch.zeros_like(x_slice).to(x_slice.device)
        ctx_anchor = ctx_tsf(x_slice_anchor)
        support_anchor = torch.cat([support, ctx_anchor], dim=1)
        parameters = parameter_aggregation(support_anchor)

        if entropy_mode == "gmm":
            mean_anchor,sigma_anchor,weight_anchor = torch.chunk(parameters, 3, dim=1)
            weight_anchor = F.softmax(weight_anchor, dim=1)
        else:
            mean_anchor,sigma_anchor = torch.chunk(parameters, 2, dim=1)
            weight_anchor = None
        probs_anchor = entropy.likelihood(x_slice, mean_anchor, sigma_anchor, weight_anchor)

		# 开了一个新的tensor用来存储其中的变量,既能保证原有的计算图不被破坏,又能保证数值传递正确,梯度传递正确
        probs = torch.zeros_like(x_slice).to(x_slice.device)
        probs[:,:,0::2,0::2] = probs_anchor[:,:,0::2,0::2]
        probs[:,:,1::2,1::2] = probs_anchor[:,:,1::2,1::2]

        anchor = torch.zeros_like(x_slice).to(x_slice.device)
        anchor[:, :, 0::2, 0::2] = x_slice[:,:, 0::2, 0::2]
        anchor[:,:, 1::2, 1::2] = x_slice[:,:, 1::2, 1::2]
        ctx_non_anchor = ctx_tsf(anchor)
        support_non_anchor = torch.cat([support, ctx_non_anchor], dim=1)
        parameters_non_anchor = parameter_aggregation(support_non_anchor)

        if entropy_mode == "gmm":
            mean_non_anchor,sigma_non_anchor,weight_non_anchor = torch.chunk(parameters_non_anchor, 3, dim=1)
            weight_non_anchor = F.softmax(weight_non_anchor, dim=1)
        else:
            mean_non_anchor,sigma_non_anchor = torch.chunk(parameters_non_anchor, 2, dim=1)
            weight_non_anchor = None
        probs_non_anchor = entropy.likelihood(x_slice, mean_non_anchor, sigma_non_anchor, weight_non_anchor)
        probs[:,:,0::2,1::2] = probs_non_anchor[:,:,0::2,1::2]
        probs[:,:,1::2,0::2] = probs_non_anchor[:,:,1::2,0::2]
        return probs
相关推荐
喵~来学编程啦20 分钟前
【论文精读】LPT: Long-tailed prompt tuning for image classification
人工智能·深度学习·机器学习·计算机视觉·论文笔记
深圳市青牛科技实业有限公司33 分钟前
【青牛科技】应用方案|D2587A高压大电流DC-DC
人工智能·科技·单片机·嵌入式硬件·机器人·安防监控
水豚AI课代表1 小时前
分析报告、调研报告、工作方案等的提示词
大数据·人工智能·学习·chatgpt·aigc
几两春秋梦_1 小时前
符号回归概念
人工智能·数据挖掘·回归
用户691581141652 小时前
Ascend Extension for PyTorch的源码解析
人工智能
用户691581141652 小时前
Ascend C的编程模型
人工智能
-Nemophilist-2 小时前
机器学习与深度学习-1-线性回归从零开始实现
深度学习·机器学习·线性回归
成富3 小时前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
CSDN云计算3 小时前
如何以开源加速AI企业落地,红帽带来新解法
人工智能·开源·openshift·红帽·instructlab
艾派森3 小时前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘