Tensor的统计操作
范数
import torch
a = torch.full([8], 1)
b = a.reshape([2, 4])
c = a.reshape([2, 2, 2])
# 求L1范数(所有元素绝对值求和)
print(a.norm(1), b.norm(1), c.norm(1))
# 求L2范数(所有元素的平方和再开根号)
print(a.norm(2), b.norm(2), c.norm(2))
# 在b的1号维度上求L1范数
print(b.norm(1, dim=1))
# 在b的1号维度上求L2范数
print(b.norm(2, dim=1))
# 在c的0号维度上求L1范数
print(c.norm(1, dim=0))
# 在c的0号维度上求L2范数
print(c.norm(2, dim=0))
运行结果:
tensor(8.) tensor(8.) tensor(8.)
tensor(2.8284) tensor(2.8284) tensor(2.8284)
tensor([4., 4.])
tensor([2., 2.])
tensor([[2., 2.],
[2., 2.]])
tensor([[1.4142, 1.4142],
[1.4142, 1.4142]])
一些常用操作
b = torch.arange(8).reshape(2, 4).float()
# 均值,累加,最小,最大,累积
print(b.mean(), b.sum(), b.min(), b.max(), b.prod())
# 打平后的最小最大值索引
print(b.argmax(), b.argmin())
运行结果:
tensor(3.5000) tensor(28.) tensor(0.) tensor(7.) tensor(0.)
tensor(7) tensor(0)
在指定维度上求最值索引
例如有4个样本,10个类别,下面的Tensor里记录了每个样本属于每个类别的可能性,这里要得到每个样本所属的可能性最大的那个类别,那就是在"10个类别"这个维度上来求argmax
了。
c = torch.randn(4, 10)
print(c.argmax(dim=1))
运行结果:
tensor([2, 2, 5, 7])
直接使用max
和min
配合dim参数也可以获得最值索引,同时得到最值的具体值(在上面的这种语境下就是置信度了)。
print(c.max(dim=1))
运行结果:
(tensor([0.9589, 1.7394, 1.3448, 2.2079]), tensor([2, 2, 5, 7]))
使用keepdim=True
可以保持应有的dim,即仅仅是将求最值的那个dim的size变成了1,返回的结果是符合原Tensor语义的。
print(c.argmax(dim=1, keepdim=True))
print(c.max(dim=1, keepdim=True))
运行结果:
tensor([[2],
[2],
[5],
[7]])
(tensor([[0.9589],
[1.7394],
[1.3448],
[2.2079]]), tensor([[2],
[2],
[5],
[7]]))
前k大/前k小/第k小
使用topk
代替max
可以完成更灵活的需求,有时候不是仅仅要概率最大的那一个,而是概率最大的k个。如果不是求最大的k个,而是求最小的k个,只要使用参数largest=False
。
d = torch.randn(2, 10) # 2个样本,分为10个类别的置信度
print(d.topk(3, dim=1)) # 最大的3个类别
print(d.topk(3, dim=1, largest=False)) # 最小的3个类别
print(d.kthvalue(8, dim=1)) # 求第8小(一共10个那就是第3大)
运行结果:
(tensor([[2.0692, 1.6490, 0.9526],
[1.5983, 1.5737, 1.5532]]), tensor([[6, 3, 5],
[8, 1, 2]]))
(tensor([[-1.0023, -0.6423, 0.0655],
[-1.2959, -1.1504, -0.9859]]), tensor([[4, 0, 2],
[0, 5, 3]]))
(tensor([0.9526, 1.5532]), tensor([5, 2]))
比较操作
import torch
a = torch.randn(2, 3)
b = torch.randn(2, 3)
print(a > 0) # 注意得到的是ByteTensor
print(torch.gt(a, 0))
print(a != 0)
print(torch.eq(a, b)) # 比较每个位置是否相等
print(torch.equal(a, b), torch.equal(a, a)) # 全部相等时才返回True
运行结果:
tensor([[0, 1, 0],
[0, 0, 0]], dtype=torch.uint8)
tensor([[0, 1, 0],
[0, 0, 0]], dtype=torch.uint8)
tensor([[1, 1, 1],
[1, 1, 1]], dtype=torch.uint8)
tensor([[0, 0, 0],
[0, 0, 0]], dtype=torch.uint8)
False True
Tensor的高阶操作
where
使用C=torch.where(condition,A,B)
其中A,B,C,condition
是shape相同的Tensor,C
中的某些元素来自A
,某些元素来自B
,这由condition
中相应位置的元素是1还是0来决定。
import torch
cond = torch.tensor([[0.6, 0.1], [0.2, 0.7]])
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[4, 5], [6, 7]])
c = torch.where(cond > 0.5, a, b)
print(c)
运行结果:
tensor([[1, 5],
[6, 4]])
gather
使用torch.gather(input,dim,index)
对元素实现一个查表映射的操作:
这里的input往往由一个shape=[i]或者shape=[j]或者shape=[k]的Tensor扩展成shape=[i,j,k]的Tensor。也就是说往往是这样用的:
o
u
t
p
u
t
[
i
]
[
j
]
[
k
]
=
i
n
p
u
t
[
i
n
d
e
x
[
i
]
[
j
]
[
k
]
]
output[i][j][k]=input[index[i][j][k]]
output[i][j][k]=input[index[i][j][k]]
只是input的dim必须和output和input相同。
prob = torch.randn(4, 10)
_, idx = prob.topk(3, dim=1)
print(idx)
ipt = torch.arange(10) + 100
print(ipt) # 用于将idx的0~9映射到100~109
out = torch.gather(ipt.expand(4, 10), dim=1, index=idx.long())
print(out)
运行结果:
tensor([[5, 6, 1],
[1, 8, 4],
[3, 4, 2],
[6, 9, 3]])
tensor([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])
tensor([[105, 106, 101],
[101, 108, 104],
[103, 104, 102],
[106, 109, 103]])