pytorch函数

一。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])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值