统计属性
norm-p
>>> a=torch.full([8],1.)
>>> b=a.view(2,4)
>>> c=a.view(2,2,2)
>>> b
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.]])
>>> c
tensor([[[1., 1.],
[1., 1.]],
[[1., 1.],
[1., 1.]]])
>>> a.norm(1),b.norm(1),c.norm(1)
(tensor(8.), tensor(8.), tensor(8.))
>>> a.norm(2),b.norm(2),c.norm(2)
(tensor(2.8284), tensor(2.8284), tensor(2.8284))
>>> b.norm(1,dim=1)
tensor([4., 4.])
>>> b.norm(2,dim=1)
tensor([2., 2.])
>>> c.norm(1,dim=1)
tensor([[2., 2.],
[2., 2.]])
>>> c.norm(2,dim=1)
tensor([[1.4142, 1.4142],
[1.4142, 1.4142]])
mean, sum, min, max, prod
>>> a=torch.arange(8).view(2,4).float()
>>> a
tensor([[0., 1., 2., 3.],
[4., 5., 6., 7.]])
>>> a.min(),a.max(),a.mean(),a.prod(),a.sum()
(tensor(0.), tensor(7.), tensor(3.5000), tensor(0.), tensor(28.))
>>> a.argmax(),a.argmin()
(tensor(7), tensor(0))
>>> a=torch.randn(4,10)
>>> a.argmax()
tensor(31)
>>> a.argmax(dim=1)
tensor([6, 3, 6, 1])
dim, keepdim
>>> a=torch.randn(4,10)
>>> a
tensor([[ 0.6893, -0.0819, 0.7485, 1.3642, 0.0030, 0.4745, 1.5248, 0.0414,
-1.4308, -1.7226],
[ 0.2916, -0.0455, -1.3166, 2.0126, -0.3199, 0.2111, -0.2048, -0.4260,
0.1673, 1.3173],
[ 1.0404, -0.1565, -0.2779, -0.3392, -0.5311, -0.1544, 1.5886, 0.0950,
0.9083, -0.1829],
[ 0.1790, 2.3940, -0.7230, 0.4152, -1.3105, -0.2937, -0.6306, 0.1036,
0.7949, -0.0158]])
>>> a.max(dim=1)
torch.return_types.max(
values=tensor([1.5248, 2.0126, 1.5886, 2.3940]),
indices=tensor([6, 3, 6, 1]))
>>> a.argmax(dim=1)
tensor([6, 3, 6, 1])
>>> a.max(dim=1,keepdim=True)
torch.return_types.max(
values=tensor([[1.5248],
[2.0126],
[1.5886],
[2.3940]]),
indices=tensor([[6],
[3],
[6],
[1]]))
>>> a.argmax(dim=1,keepdim=True)
tensor([[6],
[3],
[6],
[1]])
Top-k or k-th
>>> a=torch.randn(4,10)
>>> a
tensor([[ 0.6893, -0.0819, 0.7485, 1.3642, 0.0030, 0.4745, 1.5248, 0.0414,
-1.4308, -1.7226],
[ 0.2916, -0.0455, -1.3166, 2.0126, -0.3199, 0.2111, -0.2048, -0.4260,
0.1673, 1.3173],
[ 1.0404, -0.1565, -0.2779, -0.3392, -0.5311, -0.1544, 1.5886, 0.0950,
0.9083, -0.1829],
[ 0.1790, 2.3940, -0.7230, 0.4152, -1.3105, -0.2937, -0.6306, 0.1036,
0.7949, -0.0158]])
>>> a.topk(3,dim=1,largest=False)
torch.return_types.topk(
values=tensor([[-1.7226, -1.4308, -0.0819],
[-1.3166, -0.4260, -0.3199],
[-0.5311, -0.3392, -0.2779],
[-1.3105, -0.7230, -0.6306]]),
indices=tensor([[9, 8, 1],
[2, 7, 4],
[4, 3, 2],
[4, 2, 6]]))
>>> a.kthvalue(8,dim=1)
torch.return_types.kthvalue(
values=tensor([0.7485, 0.2916, 0.9083, 0.4152]),
indices=tensor([2, 0, 8, 3]))
>>> a.kthvalue(3)
torch.return_types.kthvalue(
values=tensor([-0.0819, -0.3199, -0.2779, -0.6306]),
indices=tensor([1, 4, 2, 6]))
>>> a.kthvalue(3,dim=1)
torch.return_types.kthvalue(
values=tensor([-0.0819, -0.3199, -0.2779, -0.6306]),
indices=tensor([1, 4, 2, 6]))
compare
>>> a=torch.randn(4,10)
>>> a
tensor([[ 0.6893, -0.0819, 0.7485, 1.3642, 0.0030, 0.4745, 1.5248, 0.0414,
-1.4308, -1.7226],
[ 0.2916, -0.0455, -1.3166, 2.0126, -0.3199, 0.2111, -0.2048, -0.4260,
0.1673, 1.3173],
[ 1.0404, -0.1565, -0.2779, -0.3392, -0.5311, -0.1544, 1.5886, 0.0950,
0.9083, -0.1829],
[ 0.1790, 2.3940, -0.7230, 0.4152, -1.3105, -0.2937, -0.6306, 0.1036,
0.7949, -0.0158]])
>>> a>0
tensor([[ True, False, True, True, True, True, True, True, False, False],
[ True, False, False, True, False, True, False, False, True, True],
[ True, False, False, False, False, False, True, True, True, False],
[ True, True, False, True, False, False, False, True, True, False]])
>>> torch.gt(a,0)
tensor([[ True, False, True, True, True, True, True, True, False, False],
[ True, False, False, True, False, True, False, False, True, True],
[ True, False, False, False, False, False, True, True, True, False],
[ True, True, False, True, False, False, False, True, True, False]])
>>> a!=0
tensor([[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True],
[True, True, True, True, True, True, True, True, True, True]])
>>> a=torch.ones(2,3)
>>> b=torch.randn(2,3)
>>> torch.eq(a,b)
tensor([[False, False, False],
[False, False, False]])
>>> torch.eq(a,a)
tensor([[True, True, True],
[True, True, True]])
>>> torch.equal(a,a)
True
|