網站首頁 編程語言 正文
問題: 由于 在輸入lstm 層 每個batch 做了根據輸入序列最大長度做了padding,導致每個 batch 的 length 不同。 導致輸出 長度不同 。如:(batch, length, output_dim): (12,128,10),(12,111,10). 但是輸入 linear 層的時候沒有出現問題。
網站解釋:
官網 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) 可以理解 為某一維度的數 。
- 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.
代碼解釋:
分別 用三維 和二維輸入數組,查看他們參數數目是否一樣。
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 等價于下面的
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])
結果:
(128,30,20),和 (128,20) 分別是如 nn.linear(30,20) 層。
weight.shape 均為: (30,20)
linear() 參數數目只和 input_dim ,output_dim 有關。
weight 在源碼的定義, 沒找到如何計算多維input的代碼。
原文鏈接:https://blog.csdn.net/u013996948/article/details/126406694
相關推薦
- 2022-07-12 Git cherry-pick實現只復制某個commit
- 2023-12-16 IDEA中設置遠程調試服務器上的程序
- 2022-08-01 C#中把FastReport.Net報表控件的數據保存到數據庫_C#教程
- 2022-08-26 C#8.0新語法using?declaration_C#教程
- 2022-01-29 composer global require “fxp/composer-asset-plugin
- 2022-09-21 淺析C++函數模板和類模板_C 語言
- 2023-02-06 Golang泛型實現類型轉換的方法實例_Golang
- 2022-12-30 解決React報錯The?tag?is?unrecognized?in?this?browser_R
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支