Pytorch学习笔记4:合并切割与基本运算
#添加到学习笔记2末尾,直接运行。代码意义可以看注释。
print('——————————合并与切割——————————')
cat1=torch.rand(4,32,8)#在0维度上进行合并
cat2=torch.rand(5,32,8)
cat3=torch.cat([cat1,cat2],dim=0)
print('tensor shape:',cat3.shape)
stack1=torch.rand(32,8)#在0维度前增加一个维度进行合并
stack2=torch.rand(32,8)
stack3=torch.stack([stack1,stack2],dim=0)
print('tensor shape:',stack3.shape)
split1=torch.rand(6,32,8)#根据长度列表或者长度数值拆分
split2,split3,split4=split1.split([1,2,3],dim=0)#根据长度列表拆分,在0维度,拆分成3个tensor,每个tensor分别有1,2,3个元素
print('tensor shape:',split2.shape)
print('tensor shape:',split3.shape)
print('tensor shape:',split4.shape)
split5,split6=split1.split(3,dim=0)#根据长度拆分,在0维度,拆分成2个tensor,每个tensor有3个元素
print('tensor shape:',split5.shape)
print('tensor shape:',split6.shape)
chunk1=torch.rand(6,32,8)#按照数量来拆分
chunk2,chunk3,chunk4=chunk1.chunk(3,dim=0)#在0维度,拆分成3个tensor
print('tensor shape:',chunk2.shape)
print('tensor shape:',chunk3.shape)
print('tensor shape:',chunk4.shape)
print('——————————合并与切割——————————')
print('——————————基本运算——————————')
#推荐使用重载运算符+(加法)-(减法)*(对应位置的元素相乘)/(除法)@(矩阵乘法)
calc1=torch.rand(2,2,2)
calc2=torch.rand(2,2,2)
calc3=calc1+calc2
print('tensor shape:',calc1)
print('tensor shape:',calc2)
print('tensor shape:',calc3)
calc3=calc1-calc2
print('tensor shape:',calc1)
print('tensor shape:',calc2)
print('tensor shape:',calc3)
calc3=calc1*calc2
print('tensor shape:',calc1)
print('tensor shape:',calc2)
print('tensor shape:',calc3)
calc3=calc1/calc2
print('tensor shape:',calc1)
print('tensor shape:',calc2)
print('tensor shape:',calc3)
calc3=calc1@calc2#矩阵乘法
print('tensor shape:',calc1)
print('tensor shape:',calc2)
print('tensor shape:',calc3)
#线性层降维例子
#aaa=torch.rand(4,784)
xxx=torch.rand(4,784)
www=torch.rand(512,784)#pytorch习惯的参数顺序ch-out,ch-in
ooo=xxx@www.t()#如果是3维或以上tensor就要使用transpose来转置
www2=torch.rand(64,512)
ooo2=ooo@www2.t()
www3=torch.rand(10,64)
ooo3=ooo2@www3.t()
print('tensor shape:',xxx.shape)
print('tensor shape:',ooo.shape)
print('tensor shape:',ooo2.shape)
print('tensor shape:',ooo3.shape)
#4维tensor的矩阵乘法
aaaa=torch.rand(4,3,28,64)
bbbb=torch.rand(4,3,64,32)
cccc=aaaa@bbbb#pytorch只会在最后两维进行矩阵乘法
print('tensor shape:',cccc.shape)
bbbb2=torch.rand(4,1,64,32)#这里会自动boardcast,第1维会由1扩展成3再进行矩阵乘法
cccc=aaaa@bbbb2#pytorch只会在最后两维进行矩阵乘法
print('tensor shape:',cccc.shape)
aaaaa=torch.full([2,2],3.0)
bbbbb=aaaaa**2#平方
print('tensor:',bbbbb)
bbbbb=aaaaa.sqrt()#开根号
print('tensor:',bbbbb)
bbbbb=aaaaa.rsqrt()#平方根的倒数
print('tensor:',bbbbb)
exp1=torch.exp(torch.ones(2,2))#自然对数
print('tensor:',exp1)
log1=torch.log(exp1)#log
print('tensor:',log1)
appx=torch.rand(2,2)#近似值
floor=appx.floor()#向下取整
ceil=appx.ceil()#向上取整
trunc=appx.trunc()#整数部分
frac=appx.frac()#小数部分
round=appx.round()#四舍五入
print('tensor:',appx)
print('tensor:',floor)
print('tensor:',ceil)
print('tensor:',trunc)
print('tensor:',frac)
print('tensor:',round)
#幅值限制
grad=torch.rand(2,3)*15
print('max:',grad.max())
print('median:',grad.median())
print('min:',grad.min())
print('clamp 10-:',grad.clamp(10))#最小值限制在10
print('clamp 0-10:',grad.clamp(0,10))#数值范围限制在0-10
print('——————————基本运算——————————')