PyTorch tensor 翻转
June 18, 2019, 5:33 p.m.
read: 2886
转自https://discuss.pytorch.org/t/how-to-reverse-a-torch-tensor/382
形状为(10)的tensor翻转
tensor = torch.rand(10) # your tensor
# create inverted indices
idx = [i for i in range(tensor.size(0)-1, -1, -1)]
idx = torch.LongTensor(idx)
inverted_tensor = tensor.index_select(0, idx)
形状为(1,10,1)的tensor翻转
tensor = torch.rand(1, 10, 1) # your tensor
# create inverted indices
idx = [i for i in range(tensor.size(1)-1, -1, -1)]
idx = torch.LongTensor(idx)
inverted_tensor = tensor.index_select(1, idx)