pytorch--属性统计

属性统计

范式norm

1范式:元素相加

A=[a,b]
F = ∣ a + b ∣ F=|a+b| F=a+b

a=torch.full([8],1.)
b=a.view(2,4)
c=a.view(2,2,2)
a,b,c
'''
output:
(tensor([1., 1., 1., 1., 1., 1., 1., 1.]),
 tensor([[1., 1., 1., 1.],
         [1., 1., 1., 1.]]),
 tensor([[[1., 1.],
          [1., 1.]],
 
         [[1., 1.],
          [1., 1.]]]))
'''
a.norm(1),b.norm(1),c.norm(1)#1范式
'''
a=1+1+1+1+1+1+1+1=8
b=(1+1+1+1)+(1+1+1+1)
c=(1+1+1+1)+(1+1+1+1)+(1+1+1+1)+(1+1+1+1)
(tensor(8.), tensor(8.), tensor(8.))
'''

2-范式

A=[a,b]
F = a 2 + b 2 F=\sqrt{a^2+b^2} F=a2+b2

a.norm(2),b.norm(2),c.norm(2)
'''
(tensor(2.8284), tensor(2.8284), tensor(2.8284))
'''

也可以对固定维度做范式计算
b[2,4],dim=1就是在数字4对应的维度做范式计算

b.norm(1,dim=1)#第1维的第一范数
'''
tensor([4., 4.])
'''

b.norm(2,dim=1)#第1维的第二范数
'''
tensor([2., 2.])
'''

c.norm(1,dim=0)#第0维的第一范数
'''
tensor([[2., 2.],
        [2., 2.]])
        '''
c.norm(2,dim=0)#第0维的第二范数
'''
tensor([[1.4142, 1.4142],
        [1.4142, 1.4142]])
'''

最小,最大,平均值,累乘

a=torch.arange(8).view(2,4).float()
'''
tensor([[0., 1., 2., 3.],
        [4., 5., 6., 7.]])
'''

a.min(),a.max(),a.mean(),a.prod()#最小,最大,平均值,累乘
'''
(tensor(0.), tensor(7.), tensor(3.5000), tensor(0.))
'''

a.argmax(),a.argmin()#索引
'''
(tensor(7), tensor(0))
'''
#按第一维分布计算得到对应的数以及位置
a.max(dim=1)
'''
torch.return_types.max(
values=tensor([3., 7.]),
indices=tensor([3, 3]))
'''

a.max(dim=1,keepdim=True)#keepdim保持维度与原来一致
#实际上是输出与原始数据维度一致
'''
torch.return_types.max(
values=tensor([[3.],
        [7.]]),
indices=tensor([[3],
        [3]]))
        '''
        
a.argmax(dim=1,keepdim=True)#输出对应最大值的位置,输出与原来保持维度一致
'''
tensor([[3],
        [3]])
'''

猜你喜欢

转载自blog.csdn.net/qq_43894221/article/details/127113068
今日推荐