Advertisement

pytorch: 对list中的tensor进行concat

阅读量:

list中的tensor维度相同

当各维度保持一致时,可直接采用stack操作进行处理,需注意该操作会导致维度数量增加,因此应通过squeeze函数去除冗余的维度信息。

复制代码
    a=torch.rand([1,2])
    b=torch.rand([1,2])
    z=torch.rand([1,2])
    c=[]
    c.append(a)
    c.append(b)
    c.append(z)
    ret=torch.stack(c).squeeze()
    print(a,b,z,ret)
    print(ret.size())
    >>>tensor([[0.7365, 0.8111]]) tensor([[0.2135, 0.1070]]) tensor([[0.4466, 0.6659]]) tensor([[0.7365, 0.8111],
        [0.2135, 0.1070],
        [0.4466, 0.6659]])
    >>>torch.Size([3, 2])
    
    
    AI写代码python

全部评论 (0)

还没有任何评论哟~