pytorch 基本操作(三)——运算和统计函数

运算函数

最基本的加减乘除就是每个向量的对应位置进行运算,如果两个tensor的维度不同,pytorch会运用broadcasting机制令两者的维度相同在进行运算。

matrix multiply

具体讲一下矩阵的乘法,在tensor里和numpy的乘法不太一致,运用的函数是mm,或者@。

a = 2* torch.ones(2,2)
b = 3*torch.ones(2,1)
torch.mm(a,b)
'''
out:
tensor([[12.],
        [12.]])
'''

a@b
'''
out:
tensor([[12.],
        [12.]])
'''

如果tensor不是二维的,那么乘法操作会运用在最后两维度的位置上。

a = 2* torch.ones(2,3,2,2)
b = 3*torch.ones(2,3,2,1)
(a@b).shape

# out:
# torch.Size([2, 3, 2, 1])

pow

pow函数就是幂, 也可以用**。

a = torch.full([2,2], 3)
a.pow(2)
'''
out:
tensor([[9., 9.],
        [9., 9.]])
'''

a**2
'''
out:
tensor([[9., 9.],
        [9., 9.]])
'''

sqrt

sqrt就是开方的意思了。

a.sqrt()
'''
tensor([[1.7321, 1.7321],
        [1.7321, 1.7321]])
'''

exp & log

exp和log分别是指数和对数的运算。

torch.exp(a)
'''
tensor([[20.0855, 20.0855],
        [20.0855, 20.0855]])
        '''

torch.log2(a)
'''
tensor([[1.5850, 1.5850],
        [1.5850, 1.5850]])
'''

clamp

clamp函数就是去范围内的数据,如果存在范围外的数据,则会优化成设置的取值范围的上下界。

grad = torch.rand(2,3)*15
grad
'''
tensor([[ 8.8869, 12.1274, 10.7745],
        [ 6.9670, 14.5700,  4.9279]])
'''
grad.clamp(0,10)
'''
tensor([[ 8.8869, 10.0000, 10.0000],
        [ 6.9670, 10.0000,  4.9279]])
'''

min, max, median

取最小值,最大值,中间值。

grad.median()
# tensor(8.8869)

grad.max()
# tensor(14.5700)

grad.min()
# tensor(14.5700)

统计函数

norm

norm其实是取范数的意思,范数给一个公式。
∣ ∣ x ∣ ∣ p = ∑ x i p p ||\pmb {x}||_p = \sqrt[p]{\sum x_i^p} xxxp=pxip

import torch
a = torch.full([8],2)
b = a.reshape(2,4)
b
'''
tensor([[2., 2., 2., 2.],
        [2., 2., 2., 2.]])
'''
a.norm(2)
# tensor(5.6569)

你可以选择按照什么维度进行范数的计算,比如按行计算,也可以选择你想要的维度,用dim来控制。

b.norm(2,dim = 0)
# tensor([2.8284, 2.8284, 2.8284, 2.8284])

argmin、argmax

这两个函数是用来获得最大最小的索引值的。

a = torch.arange(8).reshape(2,4)
a
'''
tensor([[0, 1, 2, 3],
        [4, 5, 6, 7]])
'''
a.argmax(dim = 1)
# tensor([3, 3])

我们就可以得到在列上寻找最大值的索引。

topk

topk函数用来获得排序过后前几个值。比如我要获得在列上算,前三个最大值。

a = torch.randn(4,10)
a.topk(3,dim=1)
'''
(tensor([[1.2134, 0.9104, 0.6717],
         [3.0268, 1.0519, 0.8832],
         [1.3503, 0.8439, 0.7871],
         [0.6061, 0.5712, 0.2885]]),
 tensor([[3, 1, 2],
         [7, 3, 4],
         [4, 9, 2],
         [3, 6, 8]]))
'''

此外,可以用largest参数来改变获得最小值

a.topk(3,dim=1,largest = False)
'''
(tensor([[-2.5969, -0.6951, -0.5944],
         [-1.7995, -0.9872, -0.6307],
         [-0.9267, -0.6088, -0.0487],
         [-1.4414, -0.6768, -0.3093]]),
 tensor([[5, 0, 6],
         [0, 2, 5],
         [3, 6, 0],
         [0, 4, 9]]))
'''

randn

randn用来获得一个标准正态分布。比如,生成一个4x10维的矩阵,每个元素服从正态分布。

a = torch.randn(4,10)
a
'''
tensor([[ 0.8017,  0.2075,  0.7297, -0.4558,  0.5534, -0.2399, -0.8278, -1.0563,
          0.9368,  1.9269],
        [-0.0197, -1.7427,  2.3900,  1.6809,  0.2796, -0.5736,  0.6188,  0.0685,
          2.5092, -0.9390],
        [-1.2848,  0.0313, -2.2174, -0.6386,  0.7001,  0.8683, -0.4074,  0.3764,
         -2.3123,  0.0442],
        [-2.2270,  2.3214, -0.9695, -0.1810, -0.5536,  1.0346,  0.6068, -0.7674,
         -0.6230,  1.0022]])
'''

如果调整平均值和标准差。

a.normal_(mean = 0,std = 10)
a
'''
tensor([[-7.7657e+00,  3.0024e+00, -2.1063e+01, -3.2085e+00,  2.5228e+00,
          7.8223e-02, -1.0486e+01,  4.9094e-01, -8.3381e+00, -1.9087e+00],
        [-1.3216e+01, -3.5146e+00,  9.8041e+00, -2.3957e+00, -1.0590e+01,
          6.6000e+00,  1.7035e+00, -1.4882e+01, -8.3720e+00,  1.4738e+01],
        [ 3.2365e+00, -3.3753e+01, -1.2601e+01,  2.8864e+00,  1.8325e-03,
          4.3199e+00, -1.2004e+00,  3.1132e+00,  1.3352e+00,  1.7702e+01],
        [ 1.6580e+00,  1.0280e+00,  2.9821e+00,  5.2143e+00,  4.0229e+00,
         -1.5143e+01, -7.0302e+00,  1.5830e+00,  8.7714e+00, -1.1065e+01]])
'''

Ref

[1] https://github.com/irobbwu/pytorch-intro/blob/main/02.basic.ipynb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值