我有一个叫做torch.Size([N, 2**n, 2**n])
形state
的张量,我想应用以下运算:
state[[0,1]] = state[[1,0]]
state[0] = -1*state[0]
这两项都是就地操作.是否有一些不适合我的操作可以替代它们?这些行是inside a for-loop行,所以创建新变量有点困难.
我有一个叫做torch.Size([N, 2**n, 2**n])
形state
的张量,我想应用以下运算:
state[[0,1]] = state[[1,0]]
state[0] = -1*state[0]
这两项都是就地操作.是否有一些不适合我的操作可以替代它们?这些行是inside a for-loop行,所以创建新变量有点困难.
我设法弄明白了!
Replace:
state[[0,1]] = state[[1,0]] # in-place operation
with:
state = state[[1,0]] # out-of-place operation
至于第二行,我们是replace:
state[0] = -1*state[0] # in-place operation
with:
# out-of-place operations
temp = torch.ones(state.shape).type(state.type()).to(state.device)
temp[1] = -1*temp[1]
state = state*temp
这似乎很管用!