关于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])
>>> 
  • 接下来看下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好方便奥

上一篇:numpy.squeeze()的用法


下一篇:逻辑回归判断cat的Tips