關於torch.nn.Linear的筆記


關於該類:

torch.nn.Linear(in_features, out_features, bias=True)

可以對輸入數據進行線性變換:

$y  = x A^T + b$

in_features: 輸入數據的大小。

out_features: 輸出數據的大小。

bias: 是否添加一個可學習的 bias,即上式中的 $b$。

 

該線性變換,只對輸入的 tensor 的最后一維進行:

例如我們有一個Linear層如下:

m = nn.Linear(20, 30)

 

示例1:

input = torch.randn(2, 5, 8, 20)
output = m(input)
print(output.size())

結果:

torch.Size([2, 5, 8, 30])

 

示例2:

input = torch.randn(20)
output = m(input)
print(output.size())

結果:

torch.Size([30])

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM