函数torch.gather()的实例记载学习

记录一下

因为总是使用这个函数,而且贼恶心,总忘记。

二维案例

1.1 二维简单案例
# 1.
x = torch.randint(0,20,(4,5)) 
x, x.shape
'''
(tensor([[ 6, 19,  3,  4,  5],
         [12,  2,  9, 13,  2],
         [ 8, 18, 16, 14,  6],
         [ 8,  8, 10, 12,  8]]),
 torch.Size([4, 5]))
'''
# 2.
index = torch.tensor([[1,1,1,1],[2,2,2,2],[3,3,3,3],[0,0,0,0]])
index,index.shape
'''
(tensor([[1, 1, 1, 1],
         [2, 2, 2, 2],
         [3, 3, 3, 3],
         [0, 0, 0, 0]]),
----------------------------------------------------
----> 注意index的 size() 是 torch.Size([4, 4])) <----
----------------------------------------------------
'''
# 3.
torch.gather(x, 1, index)
'''
tensor([[19, 19, 19, 19],
        [ 9,  9,  9,  9],
        [14, 14, 14, 14],
        [ 8,  8,  8,  8]])
'''
# 4.很明显
----------------------------------------------------
'返回 ---> size=(4,4) <--- 的矩阵': 和上面index的尺寸一样
----------------------------------------------------
[[ x[0,1]=19, x[0,1]=19, x[0,1]=19, x[0,1]=19],
 [ x[1,2]=9, x[1,2]=9, x[1,2]=9, x[1,2]=9],
 [...],
 [...]
]

1.2 二维复杂案例
# 1.
'''
(tensor([[ 6, 19,  3,  4,  5],
         [12,  2,  9, 13,  2],
         [ 8, 18, 16, 14,  6],
         [ 8,  8, 10, 12,  8]]),
 torch.Size([4, 5]))
'''
# 2.
index = torch.tensor([[1,2,3,0],[2,1,3,0],[3,3,3,3],[0,0,0,0]])
index,index.shape
'''
(tensor([[1, 2, 3, 0],
         [2, 1, 3, 0],
         [3, 3, 3, 3],
         [0, 0, 0, 0]]),
----------------------------------------------------
----> 注意index的 size() 是 torch.Size([4, 4])) <----
----------------------------------------------------
'''
# 3.
torch.gather(x, 1, index)
'''
tensor([[19,  3,  4,  6],
        [ 9,  2, 13, 12],
        [14, 14, 14, 14],
        [ 8,  8,  8,  8]])
'''
# 4.很明显
----------------------------------------------------
'返回 ---> size=(4,4) <--- 的矩阵': 和上面index的尺寸一样,这个绝对不会变
----------------------------------------------------
[[ x[0,1]=19, x[0,2]=3, x[0,3]=4, x[0,0]=6],
 [ ... ],
 [ ... ],
 [ ... ]
]
# 5. 那么当dim=0呢
'RuntimeError: Size does not match at dimension 1 get 5 vs 4'
会报错
这是因为 index.size()[1]4, 而 x.size()[1]5
但是选定的·dim=0·,也就是 index.size()[dim] 和 x.size()[0]是可以不同,也可以相同。大一点小一点都行
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
class _PointnetSAModuleBase(nn.Module): def init(self): super().init() self.npoint = None self.groupers = None self.mlps = None self.pool_method = 'max_pool' def forward(self, xyz: torch.Tensor, features: torch.Tensor = None, new_xyz=None) -> (torch.Tensor, torch.Tensor): """ :param xyz: (B, N, 3) tensor of the xyz coordinates of the features :param features: (B, N, C) tensor of the descriptors of the the features :param new_xyz: :return: new_xyz: (B, npoint, 3) tensor of the new features' xyz new_features: (B, npoint, \sum_k(mlps[k][-1])) tensor of the new_features descriptors """ new_features_list = [] xyz_flipped = xyz.transpose(1, 2).contiguous() if new_xyz is None: new_xyz = pointnet2_utils.gather_operation( xyz_flipped, pointnet2_utils.furthest_point_sample(xyz, self.npoint) ).transpose(1, 2).contiguous() if self.npoint is not None else None for i in range(len(self.groupers)): new_features = self.groupers[i](xyz, new_xyz, features) # (B, C, npoint, nsample) new_features = self.mlpsi # (B, mlp[-1], npoint, nsample) if self.pool_method == 'max_pool': new_features = F.max_pool2d( new_features, kernel_size=[1, new_features.size(3)] ) # (B, mlp[-1], npoint, 1) elif self.pool_method == 'avg_pool': new_features = F.avg_pool2d( new_features, kernel_size=[1, new_features.size(3)] ) # (B, mlp[-1], npoint, 1) else: raise NotImplementedError new_features = new_features.squeeze(-1) # (B, mlp[-1], npoint) new_features_list.append(new_features) return new_xyz, torch.cat(new_features_list, dim=1)你可以给我详细讲解一下这个模块吗,一个语句一个语句的来讲解
05-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值