报错:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [256, 3, 32, 32]] is at version 768; expected version 512 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient, with torch.autograd.set_detect_anomaly(True).
问题原因:
利用a变量进行一次网络运算后,使用修改后的a变量又进行一次网络运算,而网络在后向传播时,发现第一次网络运算的梯度溯源失败,a变量被修改了。
解决方案:
增加一个z变量,用copy函数浅拷贝一份原数据,在原数据的基础上进行改动。让梯度计算时能够溯源。
z=copy.copy(imgs[:, net_idx, ...])
with torch.no_grad():
for img_id, imgg in enumerate(z):
z[img_id]=(cutout(imgg))