错误信息: RuntimeError: in-place operations can be only used on variables that don’t share storage with any other variables, but detected that there are 4 objects sharing it
自动求导是很方便, 但是想想, 如果两个Variable共享内存, 再对这个共享的内存的数据进行修改, 就会引起错误!
一般是由于 inplace操作或是indexing或是转置. 这些都是共享内存的.
@staticmethod
def backward(ctx, grad_output):
ind_lst = ctx.ind_lst
flag = ctx.flag
c = grad_output.size(1)
grad_former_all = grad_output[:, 0:c//3, :, :]
grad_latter_all = grad_output[:, c//3: c*2//3, :, :]
grad_swapped_all = grad_output[:, c*2//3:c, :, :]
spatial_size = ctx.h * ctx.w
W_mat_all = Variable(ctx.Tensor(ctx.bz, spatial_size, spatial_size).zero_())
for idx in range(ctx.bz):
W_mat = W_mat_all.select(0,idx)
for cnt in range(spatial_size):
indS = ind_lst[idx][cnt]