如何解决就地操作中的梯度计算错误
我有一个编码器-解码模型和一个迭代计算某些东西的函数:
f(a,b,c):
# a: [batchsize,n,channel]
res_a = torch.empty(a.shape)
res_b = torch.empty(b.shape)
for i in range(a.shape[1]):
res_a[:,i] = res_a[:,i-1] * xxx + xxx
res_b[:,i] * res_b[i-1] + xxx
# something like the two lines above
return torch.cat([res_a,res_b,...],-1)
这是前向函数的主要过程:
def forward(x):
a,c = x[0:i],x[i:j],x[j:k]
q = f(a,c)
x1 = decoder(encoder(x))
a,c = x1[0:i],x1[i:j],x1[j:k]
q1 = f(a,c)
return q,q1
损失是q和q1的差
然后在向后计算 q1
时发生错误:
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.FloatTensor [64,3]],which is output 0 of SelectBackward,is at version 21; expected version 20 instead. Hint: enable anomaly detection to find the operation that failed to compute its gradient,with torch.autograd.set_detect_anomaly(True).
我真的不知道在哪里修改梯度计算的变量,我在函数 f
中所做的只是从输入中索引一些数据并进行一些计算(加、乘),然后将结果存储到新张量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。