\qquad 张量的基本运算方式,一种为逐元素之间的运算,如加减乘除四则运算、幂运算、平方根、对数、数据裁剪等;
\qquad 另一种为矩阵之间的运算,如矩阵相乘、矩阵的转置、矩阵的迹等。
PyTorch | 常用数学运算
一、基础四则运算
1.1 add 加法
torch.add(input, value, out=None)
:对输入张量 i n p u t input input 逐元素加上标量值/张量
v a l u e value value,并返回一个新的张量 t e n s o r tensor tensor。需要满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。可以使用符号+
完成相同运算。- input(Tensor):输入张量。
- value(Number):添加到输入每个元素的数。
- out(Tensor, optional):可选参数。要输出的目标张量。
1.2 sub 减法
torch.sub(input, value, out=None)
:对输入张量 i n p u t input input 逐元素减去标量值/张量
v a l u e value value,并返回一个新的张量 t e n s o r tensor tensor。需要满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。可以使用符号-
完成相同运算。
1.3 mul 乘法
torch.mul(input, value, out=None)
:对输入张量 i n p u t input input 逐元素乘以标量值/张量
v a l u e value value,并返回一个新的张量 t e n s o r tensor tensor。需要满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。可以使用符号*
完成相同运算。
1.4 div 除法
torch.div(input, value, out=None)
:对输入张量 i n p u t input input 逐元素除以标量值/张量
v a l u e value value,并返回一个新的张量 t e n s o r tensor tensor。需要满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。可以使用符号/
完成相同运算,使用符号//
完成整除运算。
1.5 fmod 函数
torch.fmod(input, div, out=None)
:对输入张量 i n p u t input input 逐元素除以标量值/张量
d i v div div,取余数,并返回一个新的张量 t e n s o r tensor tensor。需要满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。
二、平方与开平方
2.1 pow 函数
torch.pow(input,size,out=None)
:对输入张量 i n p u t input input 逐元素求幂次 s i z e size size 运算。
2.2 sqrt 函数(平方)
torch.sqrt(input, out=None)
:返回一个新张量 t e n s o r tensor tensor,包含输入 i n p u t input input 张量每个元素的平方根。
- 如果要开更高次根号,可以使用 p o w pow pow 函数,输入的参数为分数,示例:
2.3 rsqrt 函数
torch.rsqrt(input, out=None)
:返回一个新张量,包含输入 i n p u t input input 张量每个元素的平方根倒数。
三、对数函数
3.1 exp 函数
torch.exp(input, out=None)
:对 i n p u t input input 张量每个元素求自然指数, y i = e x i \pmb{y_i=e^{x_i}} yi=exiyi=exiyi=exi。
3.2 log 函数
torch.log(input, out=None)
:求 l o g log log,是以自然数 e \pmb{e} eee 为底的对数函数。
torch.log2(input, out=None)
:求 l o g log log,是以 2 \pmb{2} 222 为底的对数函数。
torch.log10(input, out=None)
:求 l o g log log,是以 10 \pmb{10} 101010 为底的对数函数。
torch.log1p(input, out=None)
:计算以自然数 e \pmb{e} eee 为底的自然对数 y = l o g e ( i n p u t + 1 ) \pmb{y = log_e(input + 1)} y=loge(input+1)y=loge(input+1)y=loge(input+1)。
四、绝对值 & 负值
4.1 abs 函数
torch.abs(input, out=None)
:计算输入的 i n p u t input input 张量的每个元素的绝对值。
4.2 neg 函数
torch.neg(input, out=None)
:计算输入的 i n p u t input input 张量的每个元素的负值。
五、近似函数
5.1 floor 函数
torch.floor(input, out=None)
:向下取整,返回一个新张量,包含输入 i n p u t input input 张量每个元素的 f l o o r floor floor,即取不大于元素的最大整数。
5.2 ceil 函数
torch.ceil(input, out=None)
:向上取整,对输入 i n p u t input input 张量每个元素向上取整,即取不小于元素的最小整数,并返回结果到输出。
5.3 trunc 函数
torch.trunc(input, out=None)
:取整数部分,将 i n p u t input input 张量中每个元素的小数部分截断。
5.4 frac 函数
torch.frac(tensor, out=None)
:返回 i n p u t input input 张量每个元素的小数部分。
5.5 round 函数
torch.round(input, out=None)
:将输入 i n p u t input input 张量的每个元素四舍五入到最近的整数。
六、数据裁剪函数
6.1 clamp_max 函数
torch.clamp_max(input, max, out=None)
:将输入 i n p u t input input 张量的逐个元素值与 m a x \pmb{max} maxmaxmax 进行比较,大于 m a x \pmb{max} maxmaxmax 的元素将被替换为 m a x \pmb{max} maxmaxmax 值,并返回结果到一个新张量 t e n s o r tensor tensor。
6.2 clamp_min 函数
torch.clamp_min(input, min, out=None)
:将输入 i n p u t input input 张量的逐个元素值与 m i n \pmb{min} minminmin 进行比较,小于 m i n \pmb{min} minminmin 的元素将被替换为 m i n \pmb{min} minminmin 值,并返回结果到一个新张量 t e n s o r tensor tensor。
6.3 clamp 函数
torch.clamp(input, min, max, out=None)
:将输入 i n p u t input input 张量每个元素值约束到区间 [ m i n , m a x ] \pmb{[min, max]} [min,max][min,max][min,max],并返回结果到一个新张量 t e n s o r tensor tensor,也可以只设定 m i n min min 或只设定 m a x max max。
七、矩阵相乘
7.1 mm 函数(二维矩阵乘法)
torch.mm(mat1, mat2, out=None)
:对矩阵 m a t 1 mat1 mat1 和 m a t 2 mat2 mat2 进行相乘,仅针对二维矩阵,矩阵要满足相乘条件。其中 m a t 1 ∈ R m × n mat1\in R^{m\times n} mat1∈Rm×n, m a t 2 ∈ R n × d mat2\in R^{n\times d} mat2∈Rn×d,输出 R m × d R^{m\times d} Rm×d。
7.2 bmm 函数(三维带batch的矩阵乘法)
torch.bmm(bmat1, bmat2, out=None)
:由于神经网络训练一般采用 m i n i − b a t c h mini-batch mini−batch,经常输入的是三维带 b a t c h batch batch 的矩阵。其中 b m a t 1 ∈ R b × m × n bmat1\in R^{b\times m\times n} bmat1∈Rb×m×n, b m a t 2 ∈ R b × n × d bmat2\in R^{b\times n\times d} bmat2∈Rb×n×d,输出 R b × m × d R^{b\times m\times d} Rb×m×d。该函数的两个输入必须是三维矩阵并且第一维相同(表示 b a t c h batch batch 维度)
7.3 matmul 函数
torch.matmul(mat1, mat2, out=None)
:对矩阵 m a t 1 mat1 mat1 和 m a t 2 mat2 mat2 进行相乘,可以对高阶矩阵相乘。注:@ 符号与 m a t m u l matmul matmul 效果相同。- 当输入矩阵都是二维时,就是普通的矩阵乘法,与
tensor.mm()
函数用法相同。
- 当输入矩阵是多维时,要求最后两个维度满足矩阵相乘条件,高维度满足广播机制:https://blog.csdn.net/m0_52650517/article/details/119913625。
- 当输入矩阵都是二维时,就是普通的矩阵乘法,与
八、矩阵转置 & 取逆 & 迹
8.1 t 函数
torch.t(input)
:求矩阵转置的函数,但是它要求输入 i n p u t input input 的 t e n s o r tensor tensor 结构维度 d i m ≤ 2 d dim\leq2d dim≤2d。 当 i n p u t input input 维度为 0 d 0d 0d 或 1 d 1d 1d 时,不做改变输出 i n p u t input input 本身,维度为 2 d 2d 2d 时,输出 i n p u t input input 的转置。
8.2 inverse 函数
torch.inverse(input, out=None)
:对方阵 i n p u t input input 求逆。如果 x × y = I x\times y=\pmb{I} x×y=III, I \pmb{I} III 为单位矩阵(主对角线上的元素都为 1 1 1 的方阵),则可称 x x x 和 y y y 互为逆矩阵。
8.3 trace 函数
torch.trace(input)
:返回二维矩阵 i n p u t input input 对角元素的和(迹)。