Pytorch?linear?多維輸入的參數(shù)問題
問題: 由于 在輸入lstm 層 每個(gè)batch 做了根據(jù)輸入序列最大長(zhǎng)度做了padding,導(dǎo)致每個(gè) batch 的 length 不同。 導(dǎo)致輸出 長(zhǎng)度不同 。如:(batch, length, output_dim): (12,128,10),(12,111,10). 但是輸入 linear 層的時(shí)候沒有出現(xiàn)問題。
網(wǎng)站解釋:
官網(wǎng) pytorch linear:
- Input:(*, H_{in})(∗,Hin?)where*∗means any number of dimensions including none andH_{in} = \text{in\_features}Hin?=in_features. 任意維度 number 理解有歧義 (a)number. k可以理解三維,四維。。。 (b) 可以理解 為某一維度的數(shù) 。
- Output:(*, H_{out})(∗,Hout?)where all but the last dimension are the same shape as the input andH_{out} = \text{out\_features}Hout?=out_features.
代碼解釋:
分別 用三維 和二維輸入數(shù)組,查看他們參數(shù)數(shù)目是否一樣。
import torch x = torch.randn(128, 20) # 輸入的維度是(128,20) m = torch.nn.Linear(20, 30) # 20,30是指維度 output = m(x) print('m.weight.shape:\n ', m.weight.shape) print('m.bias.shape:\n', m.bias.shape) print('output.shape:\n', output.shape) # ans = torch.mm(input,torch.t(m.weight))+m.bias 等價(jià)于下面的 ans = torch.mm(x, m.weight.t()) + m.bias print('ans.shape:\n', ans.shape) print(torch.equal(ans, output))
output:
m.weight.shape: torch.Size([30, 20]) m.bias.shape: torch.Size([30]) output.shape: torch.Size([128, 30]) ans.shape: torch.Size([128, 30]) True
x = torch.randn(128, 30,20) # 輸入的維度是(128,30,20) m = torch.nn.Linear(20, 30) # 20,30是指維度 output = m(x) print('m.weight.shape:\n ', m.weight.shape) print('m.bias.shape:\n', m.bias.shape) print('output.shape:\n', output.shape)
ouput: m.weight.shape: torch.Size([30, 20]) m.bias.shape: torch.Size([30]) output.shape: torch.Size([128, 30, 30])
結(jié)果:
(128,30,20),和 (128,20) 分別是如 nn.linear(30,20) 層。
weight.shape 均為: (30,20)
linear() 參數(shù)數(shù)目只和 input_dim ,output_dim 有關(guān)。
weight 在源碼的定義, 沒找到如何計(jì)算多維input的代碼。
到此這篇關(guān)于Pytorch linear 多維 輸入的參數(shù)的文章就介紹到這了,更多相關(guān)Pytorch多維 輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python分享一款地理數(shù)據(jù)可視化神器keplergl
這篇文章主要介紹了分享一款地理數(shù)據(jù)可視化神器keplergl,keplergl是由Uber開源的一款地理數(shù)據(jù)可視化工具,通過keplergl我們可以在Jupyter?notebook中使用,下文分享需要的小伙伴可以參考一下2022-02-02Python Unittest根據(jù)不同測(cè)試環(huán)境跳過用例的方法
這篇文章主要給大家介紹了關(guān)于Python Unittest如何根據(jù)不同測(cè)試環(huán)境跳過用例的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧2018-12-12python實(shí)現(xiàn)音樂播放和下載小程序功能
這篇文章主要介紹了python實(shí)現(xiàn)音樂播放和下載小程序功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Django框架創(chuàng)建mysql連接與使用示例
這篇文章主要介紹了Django框架創(chuàng)建mysql連接與使用,簡(jiǎn)單介紹了Linux環(huán)境下mysql的安裝,并結(jié)合實(shí)例形式分析了Django框架基于第三方庫pymysql連接mysql數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-07-07Python爬蟲實(shí)戰(zhàn)之網(wǎng)易云音樂加密解析附源碼
讀萬卷書不如行萬里路,學(xué)的扎不扎實(shí)要通過實(shí)戰(zhàn)才能看出來,本篇文章手把手帶你解析網(wǎng)易云音樂數(shù)據(jù),大家可以在實(shí)戰(zhàn)過程中更有效的掌握python2021-10-10Python閉包執(zhí)行時(shí)值的傳遞方式實(shí)例分析
這篇文章主要介紹了Python閉包執(zhí)行時(shí)值的傳遞方式,結(jié)合實(shí)例形式分析了Python閉包執(zhí)行時(shí)的傳值原理與實(shí)現(xiàn)方式,代碼中包含了較為詳盡的注釋便于理解,需要的朋友可以參考下2018-06-06編寫Python腳本來實(shí)現(xiàn)最簡(jiǎn)單的FTP下載的教程
這篇文章主要介紹了編寫Python腳本來實(shí)現(xiàn)最簡(jiǎn)單的FTP下載的教程,主要用到了ftplib模塊,無圖形界面...需要的朋友可以參考下2015-05-05python matplotlib 畫dataframe的時(shí)間序列圖實(shí)例
今天小編就為大家分享一篇python matplotlib 畫dataframe的時(shí)間序列圖實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11