網(wǎng)站首頁 編程語言 正文
問題: 由于 在輸入lstm 層 每個(gè)batch 做了根據(jù)輸入序列最大長度做了padding,導(dǎo)致每個(gè) batch 的 length 不同。 導(dǎo)致輸出 長度不同 。如:(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的代碼。
原文鏈接:https://blog.csdn.net/u013996948/article/details/126406694
相關(guān)推薦
- 2022-11-23 一文教會(huì)你用正則表達(dá)式校驗(yàn)日期時(shí)間格式_正則表達(dá)式
- 2022-07-21 React樣式?jīng)_突問題
- 2022-11-15 python運(yùn)行cmd命令行的3種方法總結(jié)_python
- 2022-03-30 SQL基礎(chǔ)查詢和LINQ集成化查詢_MsSql
- 2023-02-27 pandas中concatenate和combine_first的用法詳解_python
- 2022-07-15 go語言實(shí)現(xiàn)銀行卡號(hào)Luhn校驗(yàn)_Golang
- 2022-05-20 詳解在SQLPlus中實(shí)現(xiàn)上下鍵翻查歷史命令的功能_MsSql
- 2022-12-06 React自定義視頻全屏按鈕實(shí)現(xiàn)全屏功能_React
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支