Pytorch scatter_ 理解轴的含义 冷不防 2021-06-24 15:59 350阅读 0赞 scatter\_(input, dim, index, src)将src中数据根据index中的索引按照dim的方向填进input中。 >>> x = torch.rand(2, 5) >>> x 0.4319 0.6500 0.4080 0.8760 0.2355 0.2609 0.4711 0.8486 0.8573 0.1029 [torch.FloatTensor of size 2x5] LongTensor的shape刚好与x的shape对应,也就是LongTensor每个index指定x中一个数据的填充位置。dim=0,表示按行填充,主要理解按行填充。举例LongTensor中的第0行第2列index=2,表示在第2行(从0开始)进行填充填充,对应到zeros(3, 5)中就是位置(2,2)。所以此处要求zeros(3, 5)的列数要与x列数相同,而LongTensor中的index最大值应与zeros(3, 5)行数相一致。 >>> 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] 同上理,可以把1.23看成\[\[1.23\], \[1.23\]\]。此处按列填充,LongTensor中的index=2对应zeros(2, 4)的(0,2)位置。 >>> z = torch.zeros(2, 4).scatter_(1, torch.LongTensor([[2], [3]]), 1.23) >>> z 0.0000 0.0000 1.2300 0.0000 0.0000 0.0000 0.0000 1.2300 [torch.FloatTensor of size 2x4]
还没有评论,来说两句吧...