torch.tensor拼接与list(tensors)

Construct list(tensors)

创建一个包含张量的列表,以及2个张量如下:

import toroch

a = [torch.tensor([[0.7, 0.3], [0.2, 0.8]]), 
     torch.tensor([[0.5, 0.9], [0.5, 0.5]])]
b = torch.tensor([[0.1, 0.9], [0.3, 0.7]])
c = torch.tensor([[0.1, 0.9, 0.5], [0.3, 0.7, 0.0]])

To stack list(tensors)

堆叠之前对stack函数做一点说明。Stack操作,先升维,再扩增。参考stack与cat。对张量进行堆叠操作,要求张量的shape一致:

stack1 = torch.stack(a)  # default: dim=0, [2, 2, 2]
print(stack1)
stack2 = torch.stack((stack1, b), 0)
print(stack2)

output:

 tensor([[[0.7000, 0.3000],
     [0.2000, 0.8000]],
    [[0.5000, 0.9000],
     [0.5000, 0.5000]]])

RuntimeError: 
stack expects each tensor to be equal size, but got [2, 2, 2] at entry 0 and [2, 2] at entry 1

To concatenate list(tensors)

c = torch.cat([torch.stack(a), b[None]], 0)
# (2, 2, 2), (1, 2, 2) ⇒ (3, 2, 2)
print(c)

Output:

   tensor([
   [[0.7000, 0.3000],
     [0.2000, 0.8000]],

   [[0.5000, 0.9000],
     [0.5000, 0.5000]],

   [[0.1000, 0.9000],
     [0.3000, 0.7000]]])

但是,如果要使用stack替代上述cat操作,则会报错,因为stack要求两个输入的shape完全相同,而cat允许非拼接部分不同。除了torch.cat以外,也可以使用list.append完成以上操作。

a.append(b)
print(a)
a.append(c)
print(a)
[tensor([[0.7000, 0.3000],[0.2000, 0.8000]]),
tensor([[0.5000, 0.9000], [0.5000, 0.5000]]), 
tensor([[0.1000, 0.9000], [0.3000, 0.7000]])]

[tensor([[0.7000, 0.3000], [0.2000, 0.8000]]), 
tensor([[0.5000, 0.9000], [0.5000, 0.5000]]), 
tensor([[0.1000, 0.9000], [0.3000, 0.7000]]), 
tensor([[0.1000, 0.9000, 0.5000],
       [0.3000, 0.7000, 0.0000]])]

注意到,list.append()不仅可以堆叠同形状的tensors,而且可以容纳不同shape的tensor,是一个tensor容器!但是本人在使用过程中出现了以下 low-level 错误,请读者谨防:

    d = []
    d.append(a), d.append(b)
    print(d)

    e = a.append(b)
    print(e)  # 空的!

Output:

[[tensor([[0.7000, 0.3000],
        [0.2000, 0.8000]]), tensor([[0.5000, 0.9000],
        [0.5000, 0.5000]])], tensor([[0.1000, 0.9000],
        [0.3000, 0.7000]])]
None

list_x.append 过程中不会返回新的东西,只能从list_x中获取。
完结,撒花。

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
class PointnetFPModule(nn.Module): r"""Propigates the features of one set to another""" def __init__(self, *, mlp: List[int], bn: bool = True): """ :param mlp: list of int :param bn: whether to use batchnorm """ super().__init__() self.mlp = pt_utils.SharedMLP(mlp, bn=bn) def forward( self, unknown: torch.Tensor, known: torch.Tensor, unknow_feats: torch.Tensor, known_feats: torch.Tensor ) -> torch.Tensor: """ :param unknown: (B, n, 3) tensor of the xyz positions of the unknown features :param known: (B, m, 3) tensor of the xyz positions of the known features :param unknow_feats: (B, C1, n) tensor of the features to be propigated to :param known_feats: (B, C2, m) tensor of features to be propigated :return: new_features: (B, mlp[-1], n) tensor of the features of the unknown features """ if known is not None: dist, idx = pointnet2_utils.three_nn(unknown, known) dist_recip = 1.0 / (dist + 1e-8) norm = torch.sum(dist_recip, dim=2, keepdim=True) weight = dist_recip / norm interpolated_feats = pointnet2_utils.three_interpolate(known_feats, idx, weight) else: interpolated_feats = known_feats.expand(*known_feats.size()[0:2], unknown.size(1)) if unknow_feats is not None: new_features = torch.cat([interpolated_feats, unknow_feats], dim=1) # (B, C2 + C1, n) else: new_features = interpolated_feats new_features = new_features.unsqueeze(-1) new_features = self.mlp(new_features) return new_features.squeeze(-1)运行时报错: File "/root/autodl-tmp/project/tools/../pointnet2_lib/pointnet2/pointnet2_modules.py", line 165, in forward new_features = torch.cat([interpolated_feats, unknow_feats], dim=1) # (B, C2 + C1, n) RuntimeError: Sizes of tensors must match except in dimension 2. Got 64 and 256 (The offending index is 0)
05-24
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值