How to use torch.stack function

后端 未结 3 495
情深已故
情深已故 2021-02-07 06:49

I have a question about torch.stack

I have 2 tensors, a.shape=(2, 3, 4) and b.shape=(2, 3). How to stack them without in-place operation?

3条回答
  •  走了就别回头了
    2021-02-07 07:23

    Stacking requires same number of dimensions. One way would be to unsqueeze and stack. For example:

    a.size()  # 2, 3, 4
    b.size()  # 2, 3
    b = torch.unsqueeze(b, dim=2)  # 2, 3, 1
    # torch.unsqueeze(b, dim=-1) does the same thing
    
    torch.stack([a, b], dim=2)  # 2, 3, 5
    

提交回复
热议问题