Linear

class paddle.nn. Linear ( in_features, out_features, weight_attr=None, bias_attr=None, name=None ) [源代码]

线性变换层。对于每个输入 Tensor \(X\),计算公式为:

\[Out = XW + b\]

其中,\(W\)\(b\) 分别为权重和偏置。

Linear 层只接受一个 Tensor 作为输入,形状为 \([batch\_size, *, in\_features]\),其中 \(*\) 表示可以为任意个额外的维度。 该层可以计算输入 Tensor 与权重矩阵 \(W\) 的乘积,然后生成形状为 \([batch\_size, *, out\_features]\) 的输出 Tensor。 如果 \(bias\_attr\) 不是 False,则将创建一个偏置参数并将其添加到输出中。

参数

  • in_features (int) – 线性变换层输入单元的数目。

  • out_features (int) – 线性变换层输出单元的数目。

  • weight_attr (ParamAttr,可选) – 指定权重参数的属性。默认值为 None,表示使用默认的权重参数属性,将权重参数初始化为 0。具体用法请参见 ParamAttr

  • bias_attr (ParamAttr|bool,可选) – 指定偏置参数的属性。\(bias\_attr\) 为 bool 类型且设置为 False 时,表示不会为该层添加偏置。\(bias\_attr\) 如果设置为 True 或者 None,则表示使用默认的偏置参数属性,将偏置参数初始化为 0。具体用法请参见 ParamAttr。默认值为 None。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

属性

weight

本层的可学习参数,类型为 Parameter

bias

本层的可学习偏置,类型为 Parameter

形状

  • 输入:形状为 \([batch\_size, *, in\_features]\) 的多维 Tensor。

  • 输出:形状为 \([batch\_size, *, out\_features]\) 的多维 Tensor。

代码示例

import paddle

# Define the linear layer.
weight_attr = paddle.ParamAttr(
    name="weight",
    initializer=paddle.nn.initializer.Constant(value=0.5))
bias_attr = paddle.ParamAttr(
    name="bias",
    initializer=paddle.nn.initializer.Constant(value=1.0))
linear = paddle.nn.Linear(2, 4, weight_attr=weight_attr, bias_attr=bias_attr)
# linear.weight: [[0.5 0.5 0.5 0.5]
#                 [0.5 0.5 0.5 0.5]]
# linear.bias: [1. 1. 1. 1.]

x = paddle.randn((3, 2), dtype="float32")
# x: [[-0.32342386 -1.200079  ]
#     [ 0.7979031  -0.90978354]
#     [ 0.40597573  1.8095392 ]]
y = linear(x)
# y: [[0.23824859 0.23824859 0.23824859 0.23824859]
#     [0.9440598  0.9440598  0.9440598  0.9440598 ]
#     [2.1077576  2.1077576  2.1077576  2.1077576 ]]