首页 > 其他分享 >torch 的 unsqueeze用法

torch 的 unsqueeze用法

时间:2023-01-13 10:08:25浏览次数:37  
标签:3.0000 unsqueeze tensor torch 用法 shape print True

import torch

a=torch.tensor([[3.0000, 3.0000],
        [3.0000, 4.0000],
        [3.6000, 3.0000],
        [3.5000, 3.0000]])


个人以为这个unsqueeze方法有点废话,完全可以用reshape 实现,鸡肋!

a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)

a.reshape([1,a.shape[0],1,a.shape[1]])==a.unsqueeze(0)

a.reshape([a.shape[0],a.shape[1] ,1])==a.unsqueeze(2)
a.reshape([a.shape[0],1,a.shape[1]])==a.unsqueeze(1)
'''
      0
m ,n------> 1,m,n

      1
m ,n------> m,1,n

      2
m ,n------> m,n,2

      k
m ,.....,n------> m,...,k,k+1,n
'''
print('a=',a)
print('a.shape=',a.shape)
print('-'*30+'分割线'+'-'*30)

print('a.unsqueeze(0).shape=',a.unsqueeze(0).shape)

print('a.unsqueeze(0)=',a.unsqueeze(0))


print('-'*30+'分割线'+'-'*30)

print('a.unsqueeze(1).shape=',a.unsqueeze(1).shape)

print('a.unsqueeze(1)=',a.unsqueeze(1))



print('-'*30+'分割线'+'-'*30)

print('a.unsqueeze(2).shape=',a.unsqueeze(2).shape)

print('a.unsqueeze(2)=',a.unsqueeze(2))
在这里是一个二位矩阵 可以在[0,1,2]三个维度上
a= tensor([[3.0000, 3.0000],
        [3.0000, 4.0000],
        [3.6000, 3.0000],
        [3.5000, 3.0000]])
a.shape= torch.Size([4, 2])
------------------------------分割线------------------------------
a.unsqueeze(0).shape= torch.Size([1, 4, 2])
a.unsqueeze(0)= tensor([[[3.0000, 3.0000],
         [3.0000, 4.0000],
         [3.6000, 3.0000],
         [3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(1).shape= torch.Size([4, 1, 2])
a.unsqueeze(1)= tensor([[[3.0000, 3.0000]],

        [[3.0000, 4.0000]],

        [[3.6000, 3.0000]],

        [[3.5000, 3.0000]]])
------------------------------分割线------------------------------
a.unsqueeze(2).shape= torch.Size([4, 2, 1])
a.unsqueeze(2)= tensor([[[3.0000],
         [3.0000]],

        [[3.0000],
         [4.0000]],

        [[3.6000],
         [3.0000]],

        [[3.5000],
         [3.0000]]])
一个是顺序,一个逆序,形成一个闭环
'''
-3 -2 -1 
0   1  2
'''
print('a.unsqueeze(-3)==a.unsqueeze(0)',a.unsqueeze(-3)==a.unsqueeze(0))

print('a.unsqueeze(-2)==a.unsqueeze(1)',a.unsqueeze(-2)==a.unsqueeze(1))

print('a.unsqueeze(-1)==a.unsqueeze(2)',a.unsqueeze(-1)==a.unsqueeze(2))

a.unsqueeze(-3)==a.unsqueeze(0) tensor([[[True, True],
         [True, True],
         [True, True],
         [True, True]]])
a.unsqueeze(-2)==a.unsqueeze(1) tensor([[[True, True]],

        [[True, True]],

        [[True, True]],

        [[True, True]]])
a.unsqueeze(-1)==a.unsqueeze(2) tensor([[[True],
         [True]],

        [[True],
         [True]],

        [[True],
         [True]],

        [[True],
         [True]]])

标签:3.0000,unsqueeze,tensor,torch,用法,shape,print,True
From: https://blog.51cto.com/u_15202985/6005763

相关文章

  • torch expand
    expand就是对那个维度为一的进行扩张importtorcha=torch.tensor([[3.0000,3.0000],[3.0000,4.0000],[3.6000,3.0000],[3.5000,3.0000......
  • 【Pytorch】torch.Tensor.view()
    目录​​简介​​​​torch.Tensor.view()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢......
  • 【Pytorch】torch. matmul()
    目录​​简介​​​​torch.matmul()​​​​语法​​​​作用​​​​举例​​​​情形1:一维*一维​​​​情形2:二维*二维​​​​情形3:一维*二维​​​​情形......
  • 【Pytorch】torch. bmm()
    目录​​简介​​​​torch.bmm()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~......
  • 【Pytorch】torch.nn.init.xavier_uniform_()
    目录​​简介​​​​torch.nn.init.xavier_uniform_()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有......
  • 【Pytorch】torch.nn.Dropout()
    目录​​简介​​​​torch.nn.Dropout()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎......
  • 【Pytorch】torch.nn. Softmax()
    目录​​简介​​​​torch.nn.Softmax()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎......
  • 【Pytorch】torch.nn.Linear()
    目录​​简介​​​​torch.nn.Linear()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎......
  • 【Pytorch】torch.nn.LeakyReLU()
    目录​​简介​​​​torch.nn.LeakyReLU()​​​​语法​​​​作用​​​​举例​​​​参考​​​​结语​​简介Hello!非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢......
  • pytorch 把权重参数从一个模型复制到另外一个模型
    参考:在pytorch模型中获取权重和偏差并将其复制到另一个模型中的类似层的正确方法是什么? ......