关于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]) >>>- 接下來看下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好方便奧
總結
以上是生活随笔為你收集整理的关于squeeze unsqueeze 以及expand的学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 选民投票
- 下一篇: 高会评审职称英语计算机,2017年高级会