fused_dropout_add¶
- paddle.incubate.nn.functional. fused_dropout_add ( x, y, p=0.5, training=True, mode='upscale_in_train', name=None ) [源代码] ¶
实现了 Dropout 和 Add 的融合
参数¶
x (Tensor): 输入张量。数据类型为 bfloat16, float16, float32 或 float64.
y (Tensor): 输入张量,数据类型为 bfloat16, float16, float32 或 float64.
p (float|int, 可选): 将单位设置为零的概率。默认值: 0.5。
training (bool, 可选): 标记是否为训练阶段。默认值: True。
mode (str, 可选): ['upscale_in_train'(默认) | 'downscale_in_infer'].
upscale_in_train (默认), 在训练时放大输出
训练: \(out = x \times \frac{mask}{(1.0 - dropout\_prob)} + y\)
推理: \(out = x + y\)
downscale_in_infer, 在推理时缩小输出
训练: \(out = input \times mask + y\)
推理: \(out = input \times (1.0 - dropout\_prob) + y\)
name (str, 可选): 操作的名称, 默认值: None。有关更多信息, 请参阅 Name.
返回¶
融合 dropout 和 add 的张量具有与 x 相同的形状和数据类型。
代码示例¶
>>> import paddle
>>> from paddle.incubate.nn.functional import fused_dropout_add
>>> paddle.set_device('gpu')
>>> paddle.seed(2023)
>>> x = paddle.randn([4, 10], dtype="float32")
>>> y = paddle.randn([4, 10], dtype="float32")
>>> out = fused_dropout_add(x, y, p=0.5)
>>> print(out)
Tensor(shape=[4, 10], dtype=float32, place=Place(gpu:0), stop_gradient=True,
[[-0.49133155, 0.53819323, -2.58393312, 0.06336236, -1.09908366,
0.22085167, 2.19751787, 0.05034769, 0.53417486, 0.84864247],
[ 0.78248203, -1.59652555, -0.14399840, -0.77985179, -0.17006736,
-0.30991879, -0.36593807, -0.51025450, 1.46401680, 0.61627960],
[ 4.50472546, -0.48472026, 0.60729283, 0.33509624, -0.25593102,
-1.45173049, 1.06727099, 0.00440830, -0.77340341, 0.67393088],
[ 1.29453969, 0.07568165, 0.71947742, -0.71768606, -2.57172823,
1.89179027, 3.26482797, 1.10493207, -1.04569530, -1.04862499]])