其他分享
首页 > 其他分享> > 关于squeeze unsqueeze 以及expand的学习

关于squeeze unsqueeze 以及expand的学习

作者:互联网

参考链接

因为自己之前对于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])
>>> 

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