a = tp.arange(0, 16).view(4, 4)
print(a)
index = tp.LongTensor([[0, 1, 2, 3]])
print(index)
print(a.gather(0, index))
index = tp.LongTensor([[3, 2, 1, 0]]).t()
print(a.gather(1, index))
index = tp.LongTensor([[3, 2, 1, 0]])
print(a.gather(0, index))
index = tp.LongTensor([[0, 1, 2, 3], [3, 2, 1, 0]]).t()
b = a.gather(1, index)
print(b)
c = tp.zeros([4, 4], dtype=tp.int64)
print©
c.scatter_(1, index, b)
print©
scatter要求参数为int64,而tp.zero()生成的张量为float32,self.dtype 和 src.dtype类型不一致,所以加入dtype=tp.int64问题解决
附上张量数据类型查询网址:https://pytorch.org/docs/stable/tensors.html