ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

关于squeeze unsqueeze 以及expand的学习

2020-12-02 16:58:36  阅读:477  来源: 互联网

标签:10 unsqueeze tensor torch squeeze expand size


参考链接

因为自己之前对于squeeze 以及unsqueeze应用较多,这里不再赘述,只给一个简单的例子

>>> import torch
>>> a=torch.randn(2,1,10)
>>> a
tensor([[[ 2.0138,  0.5330,  0.1697, -2.1840,  1.1781, -0.2538, -1.9618,
           2.5919, -0.1698,  0.7177]],

        [[ 1.2393,  0.8537, -0.1364,  0.2114, -0.4427,  0.7169, -0.0189,
          -2.8338,  1.0929,  0.5666]]])
>>> print(a.shape)
torch.Size([2, 1, 10])
>>> a.squeeze()###这里就是默认压缩维数是1的维度
tensor([[ 2.0138,  0.5330,  0.1697, -2.1840,  1.1781, -0.2538, -1.9618,  2.5919,
         -0.1698,  0.7177],
        [ 1.2393,  0.8537, -0.1364,  0.2114, -0.4427,  0.7169, -0.0189, -2.8338,
          1.0929,  0.5666]])
>>> b = a.squeeze()
>>> b
tensor([[ 2.0138,  0.5330,  0.1697, -2.1840,  1.1781, -0.2538, -1.9618,  2.5919,
         -0.1698,  0.7177],
        [ 1.2393,  0.8537, -0.1364,  0.2114, -0.4427,  0.7169, -0.0189, -2.8338,
          1.0929,  0.5666]])
>>> b.shape
torch.Size([2, 10])
>>> b.unsqueeze()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsqueeze() missing 1 required positional arguments: "dim"
>>> b.unsqueeze(0)###在第0维增加维数1
tensor([[[ 2.0138,  0.5330,  0.1697, -2.1840,  1.1781, -0.2538, -1.9618,
           2.5919, -0.1698,  0.7177],
         [ 1.2393,  0.8537, -0.1364,  0.2114, -0.4427,  0.7169, -0.0189,
          -2.8338,  1.0929,  0.5666]]])
>>> b1 = b.unsqueeze()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsqueeze() missing 1 required positional arguments: "dim"
>>> b1 = b.unsqueeze(0)
>>> b1.size
<built-in method size of Tensor object at 0x7fda5d4e03a8>
>>> b1.shape
torch.Size([1, 2, 10])
>>> b2 = b.unsqueeze(1)
>>> b2.shape
torch.Size([2, 1, 10])
>>> b2 = b.unsqueeze(2)
>>> b2.shape
torch.Size([2, 10, 1])
>>> 
  • 接下来看下expand~的用法
    expand()
    这个函数的作用就是对指定的维度进行数值大小的改变。只能改变维大小为1的维,否则就会报错。不改变的维可以传入-1或者原来的数值。

torch.Tensor.expand(*sizes) → Tensor

pytorch官方文档

expand(*sizes) → Tensor

Returns a new view of the self tensor with singleton dimensions expanded to a larger size.

**Passing -1 as the size for a dimension means not changing the size of that dimension.**

Tensor can be also expanded to a larger number of dimensions, and the new ones will be appended at the front. For the new dimensions, the size cannot be set to -1.

Expanding a tensor does not allocate new memory, but only creates a new view on the existing tensor where a dimension of size one is expanded to a larger size by setting the stride to 0. Any dimension of size 1 can be expanded to an arbitrary value without allocating new memory.

Parameters

    *sizes (torch.Size or int...) – the desired expanded size

下面给出例子

>>> x = torch.tensor([[1], [2], [3]])
>>> x
tensor([[1],
        [2],
        [3]])
>>> x.shape
torch.Size([3, 1])
>>> x.expand(3,3)
tensor([[1, 1, 1],
        [2, 2, 2],
        [3, 3, 3]])
>>> x.expand(3,10)
tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]])
>>> x.expand(-1,5)
tensor([[1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3]])
>>> x.expand(6,5)##注意奥,expand只可以在维数是1 的维数扩展,不可以在其他不是1 的维度上扩展
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: The expanded size of the tensor (6) must match the existing size (3) at non-singleton dimension 0.  Target sizes: [6, 5].  Tensor sizes: [3, 1]
>>> 

ok
完毕,发现学习代码的时候,直接test好方便奥

标签:10,unsqueeze,tensor,torch,squeeze,expand,size
来源: https://blog.csdn.net/qq_38376205/article/details/110489713

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有