【问题标题】:Convert 3D Tensor to 4D Tensor in Pytorch在 Pytorch 中将 3D 张量转换为 4D 张量
【发布时间】:2019-10-09 02:44:17
【问题描述】:

我很难在 PyTorch 中找到有关重塑的信息。 TensorFlow 非常简单。

我的张量形状为torch.Size([3, 480, 480])。 我想将其转换为形状为 [1,3,480,480] 的 4D 张量。 我该怎么做?

【问题讨论】:

    标签: pytorch


    【解决方案1】:

    您可以使用unsqueeze()

    例如:

    x = torch.zeros((4,4,4))   # Create 3D tensor 
    x = x.unsqueeze(0)         # Add dimension as the first axis (1,4,4,4)
    

    我看到一些人也使用带有None 的索引来添加单个维度。例如:

    x = torch.zeros((4,4,4))   # Create 3D tensor 
    print(x[None].shape)       #  (1,4,4,4)
    print(x[:,None,:,:].shape) #  (4,1,4,4)
    print(x[:,:,None,:].shape) #  (4,4,1,4)
    print(x[:,:,:,None].shape) #  (4,4,4,1)
    

    就我个人而言,我更喜欢unsqueeze(),但两者都熟悉就好了。

    【讨论】:

    • API 名称无论如何都会给出。
    猜你喜欢
    • 1970-01-01
    • 2023-02-15
    • 2022-10-17
    • 2020-08-05
    • 2019-07-29
    • 2021-10-11
    • 2020-02-16
    • 1970-01-01
    相关资源
    最近更新 更多