torch.unsqueeze()函数理解

torch.unsqueeze()函数理解

torch.unsqueeze(input, dim)  使用时等同于  input.unsqueeze(dim)

torch.unsqueeze()函数起到升维的作用,dim等于几表示在第几维度加一,比如原来x的size=([4]),x.unsqueeze(0)之后就变成了size=([1, 4]),而x.unsqueeze(1)之后就变成了size=([4, 1]),注意dim∈[-input.dim() - 1, input.dim() + 1]
例如

  1. 输入一维张量,即input.dim()=1
# 输入:
x = torch.tensor([1, 2, 3, 4])  # x.dim()=1
print(x)
print(x.shape)
y = x.unsqueeze(0)
print(y)
print(y.shape)  # 此时y.dim()=2
z = x.unsqueeze(1)
print(z)
print(z.shape)  # 此时z.dim()=2
# 输出:
tensor([1, 2, 3, 4])
torch.Size([4])
tensor([[1, 2, 3, 4]])
torch.Size([1, 4])
tensor([[1],
        [2],
        [3],
        [4]])
torch.Size([4, 1])
  1. 输入二维张量,即input.dim()=2
# 输入:
x = torch.tensor([[1, 2, 3], [4, 5, 6]])  # x.dim()=2
print(x)
print(x.shape)
y = x.unsqueeze(0)
print(y)
print(y.shape)  # 此时y.dim()=3
z = x.unsqueeze(1)
print(z)
print(z.shape)  # 此时z.dim()=3
# 输出:
tensor([[1, 2, 3],
        [4, 5, 6]])
torch.Size([2, 3])
tensor([[[1, 2, 3],
         [4, 5, 6]]])
torch.Size([1, 2, 3])
tensor([[[1, 2, 3]],

        [[4, 5, 6]]])
torch.Size([2, 1, 3])
  1. 输入四维张量,即input.dim()=4
# 输入:
x = torch.tensor([[[[1, 2, 3], 
                    [4, 5, 6]],
                [[0, 2, 1], 
                 [1, 5, 2]]],
                  
                [[[1, 2, 3], 
                  [4, 5, 6]],
                [[0, 2, 1], 
                 [1, 5, 2]]]])
print(x)
print(x.shape)

y2 = x.unsqueeze(2)
print(y2)
print(y2.shape)

y3 = x.unsqueeze(3)
print(y3)
print(y3.shape)
# 输出:
tensor([[[[1, 2, 3],
          [4, 5, 6]],

         [[0, 2, 1],
          [1, 5, 2]]],


        [[[1, 2, 3],
          [4, 5, 6]],

         [[0, 2, 1],
          [1, 5, 2]]]])
torch.Size([2, 2, 2, 3])
tensor([[[[[1, 2, 3],
           [4, 5, 6]]],


         [[[0, 2, 1],
           [1, 5, 2]]]],



        [[[[1, 2, 3],
           [4, 5, 6]]],


         [[[0, 2, 1],
           [1, 5, 2]]]]])
torch.Size([2, 2, 1, 2, 3])
tensor([[[[[1, 2, 3]],

          [[4, 5, 6]]],


         [[[0, 2, 1]],

          [[1, 5, 2]]]],



        [[[[1, 2, 3]],

          [[4, 5, 6]]],


         [[[0, 2, 1]],

          [[1, 5, 2]]]]])
torch.Size([2, 2, 2, 1, 3])
  • 5
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
torch.unsqueeze()函数是一个PyTorch中的函数,用于在指定维度上增加一个维度。它的作用是将一个张量的维度增加到指定的尺寸。 该函数的使用格式为torch.unsqueeze(input, dim),其中input是要进行维度扩充的张量,dim是要插入的维度的索引。使用时,也可以直接写成input.unsqueeze(dim)。 该函数在多种情况下都有用途,例如在进行卷积操作时,需要将输入的维度扩充到适当的大小。通过增加维度,可以满足不同操作的输入要求。 举个例子,如果我们有一个形状为(3, 5)的张量,我们可以使用unsqueeze函数在维度1上增加一个维度,从而得到一个形状为(3, 1, 5)的新张量。这样做可以方便在某些操作中使用。 总结来说,torch.unsqueeze()函数可以在指定维度上增加一个维度,使得张量的形状适应不同的操作需求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [torch.unsqueeze()函数理解](https://blog.csdn.net/weixin_53142585/article/details/128997156)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [pyTorch.randn()、rand()、cat()、pow()、scatter_()、.squeeze() 、.unsqueeze()](https://download.csdn.net/download/weixin_38606656/14884199)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [torch.unsqueeze() 和 torch.squeeze()](https://blog.csdn.net/qq_36387683/article/details/108093536)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值