scatter_(input, dim, index, src)将src中数据根据index中的索引按照dim的方向填进input中.
1 >>> x = torch.rand(2, 5) 2 >>> x 3 4 0.4319 0.6500 0.4080 0.8760 0.2355 5 0.2609 0.4711 0.8486 0.8573 0.1029 6 [torch.FloatTensor of size 2x5]
1) dim = 0,分别对每列填充:
>>> torch.zeros(3, 5).scatter_(0, torch.LongTensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x) 0.4319 0.4711 0.8486 0.8760 0.2355 0.0000 0.6500 0.0000 0.8573 0.0000 0.2609 0.0000 0.4080 0.0000 0.1029 [torch.FloatTensor of size 3x5]
实现原理:
对于LoneTensor内的矩阵,暂且称为 tmp = [[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]];将最终的 3*5的矩阵,暂且称为result。result初始为全0,需要经过scatter_处理。
举例:
对于tmp[0][0] = 0 -> 取x中x[0][0] = 0.4319,将其插入到result第0列的第0个位置,result[0][0] = 0.4319;
对于tmp[0][1] = 1 -> 取x中x[0][1] = 0.6500,将其插入到result第1列的第1个位置,result[1][1] = 0.6500;
对于tmp[0][2] = 2 -> 取x中x[0][1] = 0.4080,将其插入到result第2列的第2个位置,result[2][2] = 0.4080;
......
对于tmp[1][0] = 2 -> 取x中x[1][0] = 0.2609,将其插入到result第0列的第2个位置,result[2][0] = 0.2609;
对于tmp[1][1] = 0 -> 取x中x[1][1] = 0.4711,将其插入到result第1列的第0个位置,result[0][1] = 0.4711。
......
2) dim = 1,分别对每行填充
1 >>> z = torch.zeros(2, 4).scatter_(1, torch.LongTensor([[2], [3]]), 1.23) 2 >>> z 3 4 0.0000 0.0000 1.2300 0.0000 5 0.0000 0.0000 0.0000 1.2300 6 [torch.FloatTensor of size 2x4]
tmp = [[2], [3]]
tmp[0][0] = 2 -> 取x中x[0][0] = 0.4319,将其插入到result第0行的第2个位置,result[0][2] = 0.4319;
......
scatter() 和 scatter_() 的作用是一样的,只不过 scatter() 不会直接修改原来的 Tensor,而 scatter_() 会
PyTorch 中,一般函数加下划线代表直接在原来的 Tensor 上修改
scatter(dim, index, src) 的参数有 3 个
- dim:沿着哪个维度进行索引
- index:用来 scatter 的元素索引
- src:用来 scatter 的源元素,可以是一个标量或一个张量
这个 scatter 可以理解成放置元素或者修改元素
简单说就是通过一个张量 src 来修改另一个张量,哪个元素需要修改、用 src 中的哪个元素来修改由 dim 和 index 决定
官方文档给出了 3维张量 的具体操作说明,如下所示
self[index[i][j][k]][j][k] = src[i][j][k] # if dim == 0
self[i][index[i][j][k]][k] = src[i][j][k] # if dim == 1
self[i][j][index[i][j][k]] = src[i][j][k] # if dim == 2
exmaple:
x = torch.rand(2, 5)
#tensor([[0.1940, 0.3340, 0.8184, 0.4269, 0.5945],
# [0.2078, 0.5978, 0.0074, 0.0943, 0.0266]])
torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), x)
#tensor([[0.1940, 0.5978, 0.0074, 0.4269, 0.5945],
# [0.0000, 0.3340, 0.0000, 0.0943, 0.0000],
# [0.2078, 0.0000, 0.8184, 0.0000, 0.0266]])
具体地说,我们的 index 是 torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]),一个二维张量,下面用图简单说明
我们是 2维 张量,一开始进行 self[index[0][0]][0]self[index[0][0]][0],其中 index[0][0]index[0][0] 的值是0,所以执行 self[0][0]=x[0][0]=0.1940self[0][0]=x[0][0]=0.1940
self[index[i][j]][j]=src[i][j]self[index[i][j]][j]=src[i][j]
再比如self[index[1][0]][0]self[index[1][0]][0],其中 index[1][0]index[1][0] 的值是2,所以执行 self[2][0]=x[1][0]=0.2078self[2][0]=x[1][0]=0.2078
src 除了可以是张量外,也可以是一个标量
example:
torch.zeros(3, 5).scatter_(0, torch.tensor([[0, 1, 2, 0, 0], [2, 0, 0, 1, 2]]), 7)
#tensor([[7., 7., 7., 7., 7.],
# [0., 7., 0., 7., 0.],
# [7., 0., 7., 0., 7.]]
scatter() 一般可以用来对标签进行 one-hot 编码,这就是一个典型的用标量来修改张量的一个例子
example:
class_num = 10
batch_size = 4
label = torch.LongTensor(batch_size, 1).random_() % class_num
#tensor([[6],
# [0],
# [3],
# [2]])
torch.zeros(batch_size, class_num).scatter_(1, label, 1)
#tensor([[0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],
# [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
# [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
# [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.]])
参考链接:
https://www.cnblogs.com/shiyublog/p/10924287.html
https://www.cnblogs.com/dogecheng/p/11938009.html