pytorch RuntimeError: one of the variables needed for gradient computation has been modified by

我的错误是逐元素赋值:

dist_mat[i,j]=dist_mat[i,j]+beta*1/(1+exp(alpha*(p_-0.5)))

改进:

先用一个tmp: dist_mat2=torch.zeros_like(dist_mat)

对tmp逐元素:dist_mat[i,j]2=beta*1/(1+exp(alpha*(p_-0.5)))

之后整体加上即可:dist_mat=dist_mat+dist_mat2

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