一。torch.mean() 均值 (1+2+3)/3=2
a=torch.tensor([1,2,3],dtype=torch.float)
print(torch.mean(a))
结果:
tensor(2.)
二。torch.sum() 求和 1+2+3=6
print(torch.sum(a))
结果:
tensor(6.)
三。torch.prod() 求a所有元素的积 1*2*3=6
print(torch.prod(a))
结果:
tensor(6.)
四。torch.max() 取最大值 3
print(torch.max(a))
结果:
tensor(3.)
五。torch.min() 取最小值 1
print(torch.min(a))
结果:
tensor(1.)
六。 torch.argmax() 取最大值的索引 2
print(torch.argmax(a))
结果:
tensor(2)
七。torch.argmin() 取最小值的索引 0
print(torch.argmin(a))
结果:
tensor(0)
八。torch.std() 求标准差
b=torch.tensor([4,5,7],dtype=torch.float)
print(torch.std(b))
结果:
tensor(1.5275)
九。torch.var() 求方差
print(torch.var(b))
结果:
tensor(2.3333)
十。torch.median() 求中位数
c=torch.tensor([2,1,3,23,4,7],dtype=float)
print(torch.median(c))
结果:
tensor(3., dtype=torch.float64)
十一。torch.manual_seed(222) 定义随机种子
【PyTorch】一文详细介绍 随机数种子 的原理、作用和使用场景_pytorch generatorimpl随机数生成器工作原理-CSDN博客
十二。torch.cat([x1,x2],dim=0/1) 组合/拼接 dim=[-2,1]
x1=torch.tensor([[1,2,3],[2,3,4]],dtype=torch.float)
x2=torch.tensor([[4,5,6],[5,6,7]],dtype=torch.float)
torch.cat([x1,x2],dim=0)
结果:
tensor([[1., 2., 3.], [2., 3., 4.], [4., 5., 6.], [5., 6., 7.]])
x1=torch.tensor([[1,2,3],[2,3,4]],dtype=torch.float)
x2=torch.tensor([[4,5,6],[5,6,7]],dtype=torch.float)
torch.cat([x1,x2],dim=1)
结果:
tensor([[1., 2., 3., 4., 5., 6.], [2., 3., 4., 5., 6., 7.]])
十三。torch.stack([x1,x2],dim=0/1) 组合/拼接 dim=[-3,2]
torch.stack([x1,x2],dim=0)
结果:
tensor([[[1., 2., 3.], [2., 3., 4.]], [[4., 5., 6.], [5., 6., 7.]]])
torch.stack([x1,x2],dim=1)
结果:
tensor([[[1., 2., 3.], [4., 5., 6.]], [[2., 3., 4.], [5., 6., 7.]]])
十四。torch.where()
x3=torch.stack([x1,x2],dim=0)
torch.where(x3>6)
结果:
(tensor([1]), tensor([1]), tensor([2]))
why:
tensor([[[1., 2., 3.], 0[0[0,1,2], [2., 3., 4.]], 1[0,1,2]], [[4., 5., 6.], 1[0[0,1,2], [5., 6., 7.]]]) 1[0,1,2]] [1[1][2]]=7
十五。tensor与Tensor
a=torch.Tensor([1,2])
b=torch.tensor([3,4])
print(a)
print(a.type())
print(b)
print(b.type())
结果:
tensor([1., 2.]) torch.FloatTensor tensor([3, 4]) torch.LongTensor
十六。torch.masked_select(input,mask,out) 按照mask输出tensor
mask=x3.ge(5) ge >=
mask
torch.masked_select(x3,mask)
结果:
tensor([5., 6., 5., 6., 7.])
十七:torch.take(input,indices) 按照indices取值
x1=torch.tensor([[1,2,3],
[4,5,6]])
torch.take(x1,torch.tensor([1,4,5]))
结果:
tensor([2, 5, 6])