线性
- classtorch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)[源代码]
-
对输入数据进行仿射线性变换:$y = xA^T + b$。
此模块支持TensorFloat32。
在某些ROCm设备上,当使用float16输入时,此模块会采用不同的精度进行反向传播。
- 参数
- 形状:
-
-
输入: $(*, H_{in})$,其中 $*$ 表示任意数量的维度(包括零个),$H_{in} = \text{in\_features}$。
-
输出为 $(*, H_{out})$,其中除了最后一维外,所有维度的形状都与输入相同,并且$H_{out} = \text{out\_features}$。
-
- 变量
-
-
weight (torch.Tensor) – 模块的可学习权重,形状为$(\text{out\_features}, \text{in\_features})$。这些值从分布$\mathcal{U}(-\sqrt{k}, \sqrt{k})$初始化,其中$k = \frac{1}{\text{in\_features}}$
-
bias - 模块的可学习偏置,其形状为$(\text{out\_features})$。如果
bias
是True
,则从分布$\mathcal{U}(-\sqrt{k}, \sqrt{k})$中初始化值,其中$k = \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])