PyTorch | 常用统计属性与计算

\qquad 在PyTorch中包含了一些基础的统计计算功能,可以很方便地获取张量中的均值标准差最大值最小值位置等。

一、默认类型

  • torch.set_default_tensor_type(t):设置 p y t o r c h pytorch pytorch 中默认的浮点类型。
数据类型 CPU Tensor GPU Tensor
16 bit 单精度浮点 torch.cuda.HalfTensor
32 bit 浮点 torch.FloatTensor torch.cuda.FloatTensor
64 bit 浮点 torch.DoubleTensor torch.cuda.DoubleTensor

二、范数

  • 0 − 范 数 0-范数 0:非零元素的个数。
  • 1 − 范 数 1-范数 1 ∣ ∣ x ∣ ∣ 1 = ∣ x 1 ∣ + ∣ x 2 ∣ + ∣ x 3 ∣ + ⋯ + ∣ x n ∣ ||x||_1=|x_1|+|x_2|+|x_3|+\cdots+|x_n| x1=x1+x2+x3++xn(向量元素绝对值之和), x x x 到原点的曼哈顿距离
  • 向 量   2 − 范 数 向量\ 2-范数  2 ∣ ∣ x ∣ ∣ 2 = ∣ x 1 ∣ 2 + ∣ x 2 ∣ 2 + ∣ x 3 ∣ 2 + ⋯ + ∣ x n ∣ 2 2 ||x||_2=\sqrt[2]{|x_1|^2+|x_2|^2+|x_3|^2+\cdots+|x_n|^2} x2=2x12+x22+x32++xn2 2 2 2-范数也称为 * E u c l i d Euclid Euclid 范数(欧几里得范数,常用计算向量长度),即向量元素绝对值的平方和再开方,表示 x x x 到零点的欧式距离。
  • 矩 阵   F − 范 数 ( 参 数   f r o ) 矩阵\ F-范数(参数\ fro)  F fro ∣ ∣ A ∣ ∣ F = ( ∑ i = 1 m ∑ j = 1 m a i , j 2 ) 1 2 \displaystyle ||A||_F=(\sum^{m}_{i=1}\sum^{m}_{j=1}{a^2_{i,j}})^{\frac{1}{2}} AF=(i=1mj=1mai,j2)21,称为Frobenius范数,即矩阵元素绝对值的平方和再开平方。
  • p − 范 数 p-范数 p ∣ ∣ x ∣ ∣ p = ∣ x 1 ∣ p + ∣ x 2 ∣ p + ∣ x 3 ∣ p + ⋯ + ∣ x n ∣ p p ||x||_p=\sqrt[p]{|x_1|^p+|x_2|^p+|x_3|^p+\cdots+|x_n|^p} xp=px1p+x2p+x3p++xnp p p p 指的是求 p p p 范数的 p p p 值)。
  • ∞ − 范 数 ( 参 数   i n f ) \infty-范数(参数\ inf)  inf ∣ ∣ x ∣ ∣ ∞ = m a x ∣ x i ∣ ||x||_\infty=max|x_i| x=maxxi,即所有向量元素绝对值中的最大值。
  • − ∞ − 范 数 ( 参 数   − i n f ) -\infty-范数(参数\ -inf)  inf ∣ ∣ x ∣ ∣ − ∞ = m i n ∣ x i ∣ ||x||_{-\infty}=min|x_i| x=minxi,即所有向量元素绝对值中的最小值。

torch.norm(input, p='fro', dim=None, keepdim=False, out=None, dtype=None)

  1. input:输入 t e n s o r tensor tensor
  2. p(int, float, inf, -inf, 'fro', 'nuc', optional):范数计算中的幂指数值,默认为’fro’。
  3. dim (int,2-tuple,2-list, optional):指定计算的维度。
    • 如果是一个整数值,向量范数将被计算;
    • 如果是一个大小为 2 2 2 的元组,矩阵范数将被计算;
    • 如果为 N o n e None None,当输入 t e n s o r tensor tensor 只有二维时计算矩阵范数;当输入只有一维时则计算向量范数。如果输入 t e n s o r tensor tensor 超过 2 2 2 维,向量范数将被应用在最后一维。
      在这里插入图片描述

三、sort, std

3.1 sort 函数

  • torch.sort(input, dim=None, descending=False, out=None):对输入张量 i n p u t input input 沿着指定维度 d i m dim dim升序排序,如果不给定 d i m dim dim,默认为输入的最后一维 d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n。如果指定参数 d e s c e n d i n g descending descending T r u e True True,则按降序排序。 s o r t sort sort 函数返回两项:重排后的张量,和重排后元素在原张量的索引。
    在这里插入图片描述

3.2 std 函数

  • torch.std(input, dim, out=None):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上的标准差, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则返回所有元素的标准差。
    在这里插入图片描述

四、max, min, argmax, argmin

4.1 max 函数

  • torch.max(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上的最大值,并同时返回每个最大值的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则返回所有元素的最大值。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

4.2 min 函数

  • torch.min(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上的最小值,并同时返回每个最小值的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则返回所有元素的最小值。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

4.3 argmax 函数

  • torch.argmax(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上最大值的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则返回所有元素的最大值的位置索引。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

4.4 argmin 函数

  • torch.argmin(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上最小值的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则返回所有元素的最小值的位置索引。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

五. mean, median, sum, cumsum

5.1 mean 函数

  • torch.mean(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上的平均值,并同时返回每个平均值的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则计算所有元素的平均值。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

5.2 median 函数

  • torch.median(input, dim=-1, values=None, indices=None):返回输入张量 i n p u t input input 在指定维度 d i m dim dim 上的中位数,同时返回一个 L o n g T e n s o r LongTensor LongTensor 类型,包含中位数索引的 i n d i c e s indices indices。对于具有偶数个元素的 i n p u t input input 张量,中位数不是唯一的,在这种情况下,返回两个中值中较低的一个。
    在这里插入图片描述

5.3 sum 函数

  • torch.sum(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上的总和,并同时返回每个总和的位置索引, d i m dim dim 从左至右为 0 , 1 , ⋯   , n 0,1,\cdots,n 0,1,,n,不填写 d i m dim dim 则计算所有元素的和。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

5.4 cumsum 函数

  • torch.cumsum(input, dim, out=None):返回 i n p u t input input 沿指定维度 d i m dim dim累积和,即指定维度 d i m dim dim 的第一个数字不变,后面的数字依次累加。 d i m dim dim 参数为必选项。
    在这里插入图片描述

六. prod, cumprod

6.1 prod 函数

  • torch.prod(input, dim, keepdim):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 上所有元素的乘积,并同时返回每个乘积的位置索引。 k e e p d i m keepdim keepdim 表示是否需要保持输出的维度与输入一样, k e e p d i m = T r u e keepdim=True keepdim=True 表示输出和输入的维度一样, k e e p d i m = F a l s e keepdim=False keepdim=False默认)表示输出的维度被压缩了,也就是输出会比输入低一个维度。
    在这里插入图片描述

6.2 cumprod 函数

  • torch.cumprod(input, dim, out=None):返回输入张量 i n p u t input input 在给定维度 d i m dim dim 的累乘积,,即指定维度 d i m dim dim 的第一个数字不变,后面的数字依次累乘。 d i m dim dim 参数为必选项。
    在这里插入图片描述

七. kthvalue, topk

7.1 kthvalue 函数

  • torch.kthvalue(input, k, dim=None, out=None):取输入张量 i n p u t input input 指定维度上第 k k k 个最小值,若不指定 d i m dim dim,则默认为 i n p u t input input 的最后一维。返回一个元组,其中 i n d i c e s indices indices 是原始输入张量 i n p u t input input 中沿 d i m dim dim 维的第 k k k 个最小值下标。
    在这里插入图片描述

7.2 topk 函数

  • torch.topk(input, k, dim=None, largest=True, sorted=True, out=None):沿给定 d i m dim dim 维度返回输入张量 i n p u t input input k k k 个最大值,不指定 d i m dim dim,则默认为最后一维,如果 l a r g e s t largest largest F a l s e False False,则返回最小的 k k k 个值。
    在这里插入图片描述

八. 比较大小

8.1 allclose 函数

  • torch.allclose(self, other, rtol=1e-05, atol=1e-08, equal_nan=False):比较 s e l f self self o t h e r other other 两个元素是否接近,比较 s e l f self self o t h e r other other 是否接近的公式为 ∣ s e l f − o t h e r ∣ ≤ a t o l + r t o l × ∣ o t h e r ∣ | self - other |\leq atol+rtol\times |other| selfotheratol+rtol×otherequal_nan如果为 T r u e True True,则将两个 N a N NaN NaN 视为相等。
    在这里插入图片描述

8.2 eq & equal 函数

  • torch.eq(input, other, out=None):比较 i n p u t input input o t h e r other other 两个元素的相等性, o t h e r other other 参数可为一个数,或可广播的张量。
  • torch.equal(tensor1, tensor2):若 t e n s o r 1 tensor1 tensor1 t e n s o r 2 tensor2 tensor2 两个张量有相同的形状和元素值,则返回 T r u e True True, 否则 F a l s e False False
    在这里插入图片描述

8.3 ge & gt 函数

  • torch.ge(input, other, out=None):逐元素比较 i n p u t input input o t h e r other other,即是否 i n p u t ≥ o t h e r input \geq other inputother o t h e r other other 参数可为一个数,或可广播的张量。
  • torch.gt(input, other, out=None):逐元素比较 i n p u t input input o t h e r other other,即是否 i n p u t > o t h e r input > other input>other o t h e r other other 参数可为一个数,或可广播的张量。
    在这里插入图片描述

8.4 le & lt 函数

  • torch.le(input, other, out=None):逐元素比较 i n p u t input input o t h e r other other,即是否 i n p u t ≤ o t h e r input \leq other inputother o t h e r other other 参数可为一个数,或可广播的张量。
  • torch.gt(input, other, out=None):逐元素比较 i n p u t input input o t h e r other other,即是否 i n p u t < o t h e r input < other input<other o t h e r other other 参数可为一个数,或可广播的张量。
    在这里插入图片描述

8.5 ne 函数

  • torch.ne(input, other, out=None):逐元素比较 i n p u t input input o t h e r other other,即是否 i n p u t ≠ o t h e r input\neq other input=other o t h e r other other 参数可为一个数,或可广播的张量。
    在这里插入图片描述

8.6 isfinite & isnan 函数

  • torch.isnan(input):判断 i n p u t input input 张量是否为缺失值
  • torch.isnan(input):判断 i n p u t input input 张量是否为有限值
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_52650517/article/details/120095550
今日推荐