欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pytorch?linear?多維輸入的參數(shù)問(wèn)題

 更新時(shí)間:2022年08月19日 15:07:31   作者:又是花落時(shí)  
這篇文章主要介紹了Pytorch?linear多維輸入的參數(shù)的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

問(wèn)題: 由于 在輸入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í)候沒(méi)有出現(xiàn)問(wèn)題。

網(wǎng)站解釋?zhuān)?/p>

官網(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.

代碼解釋?zhuān)?/h2>

分別 用三維 和二維輸入數(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 在源碼的定義, 沒(méi)找到如何計(jì)算多維input的代碼。

到此這篇關(guān)于Pytorch linear 多維 輸入的參數(shù)的文章就介紹到這了,更多相關(guān)Pytorch多維 輸入內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論