五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網 會員登陸 & 注冊

Pytorch學習筆記4:合并切割與基本運算

2020-10-10 10:11 作者:車科技2020  | 我要投稿

#添加到學習筆記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('——————————基本運算——————————')

Pytorch學習筆記4:合并切割與基本運算的評論 (共 條)

分享到微博請遵守國家法律
中卫市| 临澧县| 通城县| 霍山县| 台南县| 云霄县| 邓州市| 宁都县| 河南省| 托里县| 上虞市| 天祝| 雅江县| 卫辉市| 泽州县| 偏关县| 顺平县| 利川市| 甘孜县| 岚皋县| 施秉县| 阿拉善盟| 麻江县| 香格里拉县| 增城市| 馆陶县| 大关县| 鸡泽县| 岳西县| 丰都县| 松桃| 永平县| 桐乡市| 阳新县| 嘉义县| 和静县| 绥化市| 聂荣县| 哈密市| 通辽市| 开远市|