PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)
生活随笔
收集整理的這篇文章主要介紹了
PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. Tensor 歸并運算函數
此類操作會使輸出形狀小于輸入形狀,并可以沿著某一維度進行指定操作,如加法, 既可以計算整個 tensor 的和,也可以計算 tensor 每一行或者 每一列的和,
常用歸并操作如下表所示:
2. 使用示例
2.1 torch.mean
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [13]: t.mean(a)
Out[13]: tensor(2.5000)In [14]: t.mean(a,dim=0)
Out[14]: tensor([1.5000, 3.5000])In [15]: t.mean(a,dim=1)
Out[15]: tensor([3., 2.])
2.2 torch.sum
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [16]: a.sum()
Out[16]: tensor(10.)In [17]: a.sum(dim=0)
Out[17]: tensor([3., 7.])In [18]: a.sum(dim=1)
Out[18]: tensor([6., 4.])In [19]:
2.3 torch.median
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [19]: a.median()
Out[19]: tensor(2.)In [20]: a.median(dim=0)
Out[20]:
torch.return_types.median(
values=tensor([1., 3.]),
indices=tensor([1, 1]))In [21]: a.median(dim=1)
Out[21]:
torch.return_types.median(
values=tensor([2., 1.]),
indices=tensor([0, 0]))
2.4 torch.mode
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [29]: a.mode()
Out[29]:
torch.return_types.mode(
values=tensor([2., 1.]),
indices=tensor([0, 0]))In [30]: a.mode(dim=0)
Out[30]:
torch.return_types.mode(
values=tensor([1., 3.]),
indices=tensor([1, 1]))In [31]: a.mode(dim=1)
Out[31]:
torch.return_types.mode(
values=tensor([2., 1.]),
indices=tensor([0, 0]))
2.5 torch.norm
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [22]: a.norm()
Out[22]: tensor(5.4772)In [23]: a.norm(dim=0)
Out[23]: tensor([2.2361, 5.0000])In [24]: a.norm(dim=1)
Out[24]: tensor([4.4721, 3.1623])
2.6 torch.dist
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [28]: a.dist(t.Tensor([1,2]))
Out[28]: tensor(2.4495)
2.7 torch.std
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [32]: a.std()
Out[32]: tensor(1.2910)In [33]: a.std(dim=0)
Out[33]: tensor([0.7071, 0.7071])In [34]: a.std(dim=1)
Out[34]: tensor([1.4142, 1.4142])
2.8 torch.var
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [35]: a.var()
Out[35]: tensor(1.6667)In [36]: a.var(dim=0)
Out[36]: tensor([0.5000, 0.5000])In [37]: a.var(dim=1)
Out[37]: tensor([2., 2.])
2.9 torch.cumsum
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [39]: a.cumsum(dim=0)
Out[39]:
tensor([[2., 4.],[3., 7.]])In [40]: a.cumsum(dim=1)
Out[40]:
tensor([[2., 6.],[1., 4.]])
2.10 torch.cumprod
In [1]: import torch as tIn [11]: a = t.Tensor([[2,4], [1, 3]])In [12]: a
Out[12]:
tensor([[2., 4.],[1., 3.]])In [41]: a.cumprod(dim=0)
Out[41]:
tensor([[ 2., 4.],[ 2., 12.]])In [42]: a.cumprod(dim=1)
Out[42]:
tensor([[2., 8.],[1., 3.]])
總結
以上是生活随笔為你收集整理的PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个一个人伤感的微信网名
- 下一篇: 试管婴儿降调期间感冒了怎么办