快捷方式

線性層

class torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)[原始碼][原始碼]

對輸入資料應用仿射線性變換:y=xAT+by = xA^T + b

此模組支援 TensorFloat32

在某些 ROCm 裝置上,當使用 float16 輸入時,此模組在反向傳播時將使用不同的精度

引數
  • in_features (int) – 每個輸入樣本的大小

  • out_features (int) – 每個輸出樣本的大小

  • bias (bool) – 如果設定為 False,該層將不學習加性偏置。預設值: True

形狀
  • 輸入: (,Hin)(*, H_\text{in}) 其中 * 表示任意數量的維度(包括零個),且 Hin=in_featuresH_\text{in} = \text{in\_features}

  • 輸出: (,Hout)(*, H_\text{out}) 其中除最後一個維度外,所有維度的形狀與輸入相同,且 Hout=out_featuresH_\text{out} = \text{out\_features}

變數
  • weight (torch.Tensor) – 模組的可學習權重,形狀為 (out_features,in_features)(\text{out\_features}, \text{in\_features})。其值從 U(k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k}),其中 k=1in_featuresk = \frac{1}{\text{in\_features}}

  • bias – 模組的可學習偏置,形狀為 (out_features)(\text{out\_features})。如果 biasTrue,其值從 U(k,k)\mathcal{U}(-\sqrt{k}, \sqrt{k}) 初始化,其中 k=1in_featuresk = \frac{1}{\text{in\_features}}

示例

>>> m = nn.Linear(20, 30)
>>> input = torch.randn(128, 20)
>>> output = m(input)
>>> print(output.size())
torch.Size([128, 30])

文件

獲取 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得解答

檢視資源