pytorch原地操作无法反向传播

1.错误:

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation: [torch.cuda.FloatTensor [4, 1, 5500]], which is output 0 of SoftplusBackward, 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)

解决:网络中出现原地操作。torch中提供的softplus中出现了原地操作,可以直接用数学表达式直接替换,计算方法与原方法原理一样

sigma_0 = F.softplus(sigma_0)

##替换为
sigma_0 = torch.log(1 + torch.exp(sigma_0))

你可能感兴趣的:(pytorch,人工智能,python)