文章目录broadcasting加减乘除矩阵运算Torch.mm(Torch.matmul )/@2维: 2维powsqrt/sqrtexp/log近似值梯度剪辑最大、最小值:clamp ) ) )
广播业务
expand,withoutcopyingdata http://www.Sina.com /
insert1dimaheadexpanddimswithsize1tosamesizefeaturemaps : [ 4,32,14,14 ] bias : [ 32,1,1 ]=[ 1,32,1,1,1 ]
importtorcha=torch.rand (3,4 ) b=torch.rand(4)4) a b,torch.add(a ) a,b ) ) tensor ([ 1.1636,1.1454,] 请验证一下:
importtorcha=torch.rand (3,4 ) b=torch.rand(4)4) torch.all ) torch.eq ) ab、torch.add(a,b ) )
mul(* ) )。
div(//) ) ) ) ) ) )。
矩阵运算torch.mm ()只能用于二维矩阵的运算。
importtorcha=torch.full ([ 2,2 ],3 ) b=Torch.Ones ) 2,2 ) torch.mm(a,b ) Tensor ) [6.6.],[6
二维: importtorcha=torch.full (2,2 ),3 ) b=torch.ones (2,2 ) torch.matmul(a,b ) tensor ) )6.6 3 ) b=torch.ones (2,2 ) a@btensor([6.6.],[6.6.] )key idea
importtorcha=torch.rand (4,784 ) x=torch.rand (4,784 ) w=torch.rand (512,784 ) x@w.t ) ) ) shapetth
importtorcha=torch.rand (4,3,28,64 ) b=torch.rand ) 4,3,64,32 ) torch.matmul(a ) a,b.shape torch
通道数不同时,符合broadcast的状况:
importtorcha=torch.rand (4,3,28,64 ) b=torch.rand ) 4,1,64,32 ) torch.matmul(a ) a,b.shape torch
importtorcha=torch.rand (4,3,28,64 ) b=torch.rand (4,64,32 ) torch.matmul(a ) a, b ).shapetraceback(mmatmul ) shape trace inmoduleruntimeerror 3360 thesizeoftensora (3) must match the size of tensor b (4) )
任意乘方
importtorcha=torch.full ([ 2,2 ],6 ) a.pow(3),3 ) tensor ) [216 .216.],[216 .216.] ]仅平方
importtorcha=torch.full ([ 2,2 ],6 ) a**2tensor ) [36 .36.],[36 .36.] ) sqrt/sqrt平方根:
importtorcha=torch.full ([ 2,2 ],1024 ) a.sqrt ),a.rsqrt,a**0.5(tensor([[32 .32.],[32 .32 )
importtorcha=torch.full ([ 2,2 ],1024 ) a.rsqrt ) (tensor ) [ 0.0312,0.0312 ],[ 0.0312,0.0312 ] )导出3种:一起写exp、log、log2吧:
importtorcha=torch.exp (torch.ones (2,2 ) ) a、torch.log(a )、torch.log(a ) ) Tensor ) [ 2.7183,2.7188 1.4427]] ) ) )近似值. floor(——下近似. ceil ) ——上近似. trunc ) ) ——修整到整数部分. frac ) ——小数部分importtorcha=totor tensor )、tensor )、3.)、tensor(0.1416 ) (torch.round ) ——四舍五入importtorcha=torch.tensor ) 3.499 (b=torch.tend ) 最小值: importtorchgrad=torch.rand ) 2,3 ) *15 grad.max ),grad.min ) ) tensor ).5018 ) )。 tensor(4.8845 ).clamp ) importtorchgrad=torch.rand ) 2,3 ) *15 grad,grad.clamp ),5,110