scale

paddle. scale ( x, scale=1.0, bias=0.0, bias_after_scale=True, act=None, name=None ) [源代码]

对输入 Tensor 进行缩放和偏置,其公式如下:

bias_after_scale 为 True:

\[Out=scale*X+bias\]

bias_after_scale 为 False:

\[Out=scale*(X+bias)\]

参数

  • x (Tensor) - 要进行缩放的多维 Tensor,数据类型可以为 float32,float64,int8,int16,int32,int64,uint8。

  • scale (float|Tensor) - 缩放的比例,是一个 float 类型或者一个 shape 为[],数据类型为 float32 的 0-D Tensor 类型。

  • bias (float) - 缩放的偏置。

  • bias_after_scale (bool) - 判断在缩放之前或之后添加偏置。为 True 时,先缩放再偏置;为 False 时,先偏置再缩放。该参数在某些情况下,对数值稳定性很有用。

  • act (str,可选) - 应用于输出的激活函数,如 tanh、softmax、sigmoid、relu 等。

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

返回

Tensor,缩放后的计算结果。

代码示例

>>> # scale as a float32 number
>>> import paddle

>>> data = paddle.arange(6).astype("float32").reshape([2, 3])
>>> print(data)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 2.],
 [3., 4., 5.]])
>>> res = paddle.scale(data, scale=2.0, bias=1.0)
>>> print(res)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1. , 3. , 5. ],
 [7. , 9. , 11.]])