PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)

1. Tensor 归并运算函数

此类操作会使输出形状小于输入形状,并可以沿着某一维度进行指定操作,如加法, 既可以计算整个 tensor 的和,也可以计算 tensor 每一行或者 每一列的和,

常用归并操作如下表所示:
PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)_第1张图片
PyTorch 笔记(07)— Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)_第2张图片

2. 使用示例

2.1 torch.mean

In [1]: import torch as t

In [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 t

In [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 t

In [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 t

In [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 t

In [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 t

In [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 t

In [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 t

In [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 t

In [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 t

In [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)