RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

发布时间 2023-04-10 18:16:01作者: 一点飞鸿

错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [21, 21]], which is output 0 of SqrtBackward, is at version 1; expected version 0 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).

场景描述:

  github上找的batch all triplet loss代码在运行过程中出错(https://github.com/Yuol96/pytorch-triplet-loss)

调试修改过程:

 1.  查找代码中Sqrt相关操作,发现:

 

 distance在取平方根之后,将值赋给了自己,

修改:squared=True后正常