Linear |
您所在的位置:网站首页 › paddle200 › Linear |
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,表示使用默认的权重参数属性。如果 \(ParamAttr\) 的初始值未设置,则使用 \(Xavier\) 初始化参数,具体用法请参见 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。其数据类型为 float16, float32, float64, 默认为 float32。 输出:形状为 \([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 ]] 使用本API的教程文档¶ 附录: 飞桨框架 2.x 升级指南 快速上手 迁移经验汇总 NLP - 迁移经验汇总 CV - 快速上手 CV - 迁移经验汇总 Pytorch 写法 升级指南 模型性能分析(Profiler) 自动混合精度训练(AMP) 使用 paddle.nn.Layer 自定义网络 梯度裁剪方式介绍 转换原理 案例解析 报错调试 使用样例 Limitations 快速开始-数据并行 张量模型并行 前向重计算 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |