import torch
||x||p = (||x1||p + ||x2||p + … + ||x3||p)1/p
a = torch.randint(-10,11,[2,3,2])
a = a.to(torch.float64)
a
tensor([[[ -7., -5.],
[ 6., -5.],
[ -1., 10.]],
[[-10., -9.],
[ -5., -4.],
[ -8., 2.]]], dtype=torch.float64)
a.norm(1)
tensor(72., dtype=torch.float64)
a.norm(2)
tensor(22.9347, dtype=torch.float64)
a.norm(1,dim=0) #17=abs(-7)+abs(-10) 14=abs(-5)+abs(-9) ...
tensor([[17., 14.],
[11., 9.],
[ 9., 12.]], dtype=torch.float64)
a.norm(1,dim=1) #14=abs(-7)+abs(6)+abs(-1) 20=abs(-5)+abs(-5)+abs(10) ...
tensor([[14., 20.],
[23., 15.]], dtype=torch.float64)
a.norm(1,dim=2) #12=abs(-7)+abs(-5) 11=abs(6)+abs(-5)
tensor([[12., 11., 11.],
[19., 9., 10.]], dtype=torch.float64)
a.norm(1,dim=[0,1])
tensor([37., 35.], dtype=torch.float64)
a.norm(1,dim=[0,2])
tensor([31., 20., 21.], dtype=torch.float64)
a = torch.randint(-20,21,[2,2,3,2]).to(torch.float64)
a
tensor([[[[-17., -17.],
[ -3., 17.],
[ 7., 0.]],
[[ 7., -20.],
[-16., -18.],
[ 18., -5.]]],
[[[ 14., -20.],
[ 5., -12.],
[ 5., -13.]],
[[-16., -20.],
[ 16., -9.],
[ -1., 15.]]]], dtype=torch.float64)
print(a.argmax()) #把a拉平成一维vector 然后再找最大值索引
print(a.argmin()) #把a拉平成一维vector 然后再找最小值索引
tensor(10)
tensor(19)
print(a.argmax(dim=0))
print(a.argmin(dim=0))
tensor([[[1, 0],
[1, 0],
[0, 0]],
[[0, 1],
[1, 1],
[0, 1]]])
tensor([[[0, 1],
[0, 1],
[1, 1]],
[[1, 1],
[0, 0],
[1, 0]]])
print(a.argmax(dim=1))
print(a.argmax(dim=2))
tensor([[[1, 0],
[0, 0],
[1, 0]],
[[0, 1],
[1, 1],
[0, 1]]])
tensor([[[2, 1],
[2, 2]],
[[0, 1],
[1, 2]]])
print('索引:\n',a.max(dim=2)[1])
print('值:\n',a.max(dim=2)[0])
索引:
tensor([[[2, 1],
[2, 2]],
[[0, 1],
[1, 2]]])
值:
tensor([[[ 7., 17.],
[ 18., -5.]],
[[ 14., -12.],
[ 16., 15.]]], dtype=torch.float64)
b = torch.rand(2,3,10) #假设2个batch,每个batch3张照片,10表示每张照片依次是0~9的概率
b
tensor([[[0.5304, 0.1505, 0.5322, 0.0247, 0.1890, 0.8630, 0.6212, 0.4308,
0.3447, 0.9590],
[0.7632, 0.1420, 0.7258, 0.8698, 0.6531, 0.2155, 0.8730, 0.3963,
0.1711, 0.4374],
[0.7572, 0.4117, 0.2699, 0.4153, 0.9025, 0.7338, 0.4403, 0.6043,
0.6109, 0.9631]],
[[0.5738, 0.9288, 0.2986, 0.8859, 0.2687, 0.9388, 0.4515, 0.9561,
0.9277, 0.6534],
[0.4825, 0.9142, 0.4260, 0.4814, 0.8739, 0.9296, 0.8225, 0.5581,
0.8214, 0.2455],
[0.5460, 0.6100, 0.5846, 0.7510, 0.5964, 0.4243, 0.8347, 0.7734,
0.5279, 0.5943]]])
b.topk(1,dim=2)
# 0.9590表示第一个batch的第一张照片的10个概率值中,最大的是0.9590 并且我们可以看到它对应的indice是9 表示这张照片是“9”的概率为0.9590
# 0.9296表示第2个batch的第2张照片的10个概率值中,最大的是0.9296 并且我们可以看到它对应的indice是5 表示这张照片是“5”的概率为0.9590
torch.return_types.topk(
values=tensor([[[0.9590],
[0.8730],
[0.9631]],
[[0.9561],
[0.9296],
[0.8347]]]),
indices=tensor([[[9],
[6],
[9]],
[[7],
[5],
[6]]]))
b.topk(1)
torch.return_types.topk(
values=tensor([[[0.9590],
[0.8730],
[0.9631]],
[[0.9561],
[0.9296],
[0.8347]]]),
indices=tensor([[[9],
[6],
[9]],
[[7],
[5],
[6]]]))
print(b.shape,'\n')
print(b)
torch.Size([2, 3, 10])
tensor([[[0.5304, 0.1505, 0.5322, 0.0247, 0.1890, 0.8630, 0.6212, 0.4308,
0.3447, 0.9590],
[0.7632, 0.1420, 0.7258, 0.8698, 0.6531, 0.2155, 0.8730, 0.3963,
0.1711, 0.4374],
[0.7572, 0.4117, 0.2699, 0.4153, 0.9025, 0.7338, 0.4403, 0.6043,
0.6109, 0.9631]],
[[0.5738, 0.9288, 0.2986, 0.8859, 0.2687, 0.9388, 0.4515, 0.9561,
0.9277, 0.6534],
[0.4825, 0.9142, 0.4260, 0.4814, 0.8739, 0.9296, 0.8225, 0.5581,
0.8214, 0.2455],
[0.5460, 0.6100, 0.5846, 0.7510, 0.5964, 0.4243, 0.8347, 0.7734,
0.5279, 0.5943]]])
print(b.kthvalue(2,dim=2)[0].shape,'\n')
print(b.kthvalue(2,dim=2))
torch.Size([2, 3])
torch.return_types.kthvalue(
values=tensor([[0.1505, 0.1711, 0.4117],
[0.2986, 0.4260, 0.5279]]),
indices=tensor([[1, 8, 1],
[2, 2, 8]]))
torch.multinomial(input, num_samples,replacement=False, out=None)
作用是对input的每一行做n_samples次取值,输出的张量是每一次取值时input张量对应行的下标。
对input的要求是:包含的元素是非负的。
对num_samples的要求:小于等于input.size(-1)
a=torch.rand(2,8)
print(a)
idx = torch.multinomial(a,5)
print(idx)
tensor([[0.4200, 0.2601, 0.6574, 0.3479, 0.5186, 0.6497, 0.0095, 0.1039],
[0.3494, 0.8898, 0.6566, 0.8612, 0.0581, 0.3294, 0.4782, 0.6344]])
tensor([[5, 0, 2, 4, 1],
[0, 2, 1, 6, 3]])
因此取出来的分别是0.6497,0.4200,0.6574,0.5186,0.2601
以及0.3494,0.6566,0.8898,0.4782,0.8612.
可以看出,基本上是把值较大的取出来,值较小的被取出来的概率较小。
a = torch.tensor([[1,2],[7,8]])
b = torch.tensor([[1,4],[5,6]])
a>b
tensor([[False, False],
[ True, True]])
a == 0
tensor([[False, False],
[False, False]])
torch.ge(a,b) #a>=b? element-wise 如果dim不合适,就broadcast
tensor([[ True, False],
[ True, True]])
torch.gt(a,b) #a>b?
tensor([[False, False],
[ True, True]])
torch.le(a,b) #a<=b
tensor([[ True, True],
[False, False]])
torch.lt(a,b) #a
tensor([[False, True],
[False, False]])
torch.eq(a,b)
tensor([[ True, False],
[False, False]])
torch.equal(a,b)
False