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

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
相关推荐
ZHOU_WUYI3 小时前
3.langchain中的prompt模板 (few shot examples in chat models)
人工智能·langchain·prompt
如若1233 小时前
主要用于图像的颜色提取、替换以及区域修改
人工智能·opencv·计算机视觉
老艾的AI世界4 小时前
AI翻唱神器,一键用你喜欢的歌手翻唱他人的曲目(附下载链接)
人工智能·深度学习·神经网络·机器学习·ai·ai翻唱·ai唱歌·ai歌曲
DK221514 小时前
机器学习系列----关联分析
人工智能·机器学习
Robot2514 小时前
Figure 02迎重大升级!!人形机器人独角兽[Figure AI]商业化加速
人工智能·机器人·微信公众平台
浊酒南街5 小时前
Statsmodels之OLS回归
人工智能·数据挖掘·回归
畅联云平台5 小时前
美畅物联丨智能分析,安全管控:视频汇聚平台助力智慧工地建设
人工智能·物联网
加密新世界5 小时前
优化 Solana 程序
人工智能·算法·计算机视觉
hunteritself6 小时前
ChatGPT高级语音模式正在向Web网页端推出!
人工智能·gpt·chatgpt·openai·语音识别
Che_Che_6 小时前
Cross-Inlining Binary Function Similarity Detection
人工智能·网络安全·gnn·二进制相似度检测