From aae617fbb9b5039591736d4b6ce248714e05bd2e Mon Sep 17 00:00:00 2001 From: tangdezhi_123 Date: Mon, 27 Feb 2023 16:45:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9ops.dropout2d=E5=92=8Cops.dropout3d?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=8F=82=E6=95=B0training,?= =?UTF-8?q?=20training=E4=B8=BATrue,=20=E6=89=A7=E8=A1=8Cdropout=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=EF=BC=8Ctraining=E4=B8=BAFalse,=20=E4=B8=8D=E6=89=A7?= =?UTF-8?q?=E8=A1=8Cdropout=E6=93=8D=E4=BD=9C;=20=E5=B0=86dropout2d?= =?UTF-8?q?=E5=92=8Cdropout3d=E7=9A=84=E5=8F=82=E6=95=B0x=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E5=91=BD=E5=90=8D=E4=B8=BAinput,=20=E4=BB=A5=E5=92=8C?= =?UTF-8?q?pytorch=E5=AF=B9=E6=A0=87=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ops/mindspore.ops.func_dropout2d.rst | 15 +++--- .../ops/mindspore.ops.func_dropout3d.rst | 15 +++--- .../python/mindspore/ops/function/nn_func.py | 48 ++++++++++--------- 3 files changed, 42 insertions(+), 36 deletions(-) diff --git a/docs/api/api_python/ops/mindspore.ops.func_dropout2d.rst b/docs/api/api_python/ops/mindspore.ops.func_dropout2d.rst index 35eb42e867f..e0e98ceb6b7 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_dropout2d.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_dropout2d.rst @@ -1,7 +1,7 @@ mindspore.ops.dropout2d ======================= -.. py:function:: mindspore.ops.dropout2d(x, p=0.5) +.. py:function:: mindspore.ops.dropout2d(input, p=0.5, training=True) 在训练期间,以服从伯努利分布的概率 `p` 随机将输入Tensor的某些通道归零(对于形状为 `NCHW` 的四维Tensor,其通道特征图指的是后两维 `HW` 形状的二维特征图)。 例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `2D` Tensor 是一个待处理数据。 @@ -11,17 +11,18 @@ mindspore.ops.dropout2d `dropout2d` 可以提高通道特征映射之间的独立性。 参数: - - **x** (Tensor) - 一个形状为 :math:`(N, C, H, W)` 的 `4D` Tensor,其中N是批处理大小,`C` 是通道数,`H` 是特征高度,`W` 是特征宽度。其数据类型应为int8、int16、int32、int64、float16、float32或float64。 + - **input** (Tensor) - 一个形状为 :math:`(N, C, H, W)` 的 `4D` Tensor,其中N是批处理大小,`C` 是通道数,`H` 是特征高度,`W` 是特征宽度。其数据类型应为int8、int16、int32、int64、float16、float32或float64。 - **p** (float) - 通道的丢弃概率,介于 0 和 1 之间,例如 `p` = 0.8,意味着80%的清零概率。默认值:0.5。 + - **training** (bool) - 如果training为True, 则执行对`input`的某些通道概率清零的操作,否则,不执行。默认值:True。 返回: - - Tensor,输出,具有与输入 `x` 相同的形状和数据类型。 - - Tensor,掩码,形状与 `x` 相同,数据类型为bool。 + - Tensor,输出,具有与输入 `input` 相同的形状和数据类型。 + - Tensor,掩码,形状与 `input` 相同,数据类型为bool。 异常: - - **TypeError** - `x` 不是Tensor。 - - **TypeError** - `x` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 + - **TypeError** - `input` 不是Tensor。 + - **TypeError** - `input` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 - **TypeError** - `p` 的数据类型不是float。 - **ValueError** - `p` 值不在 `[0.0,1.0]` 之间。 - - **ValueError** - `x` 的维度不等于4。 + - **ValueError** - `input` 的维度不等于4。 diff --git a/docs/api/api_python/ops/mindspore.ops.func_dropout3d.rst b/docs/api/api_python/ops/mindspore.ops.func_dropout3d.rst index f89c97eb17f..4ed5792e5d2 100644 --- a/docs/api/api_python/ops/mindspore.ops.func_dropout3d.rst +++ b/docs/api/api_python/ops/mindspore.ops.func_dropout3d.rst @@ -1,7 +1,7 @@ mindspore.ops.dropout3d ======================= -.. py:function:: mindspore.ops.dropout3d(x, p=0.5) +.. py:function:: mindspore.ops.dropout3d(input, p=0.5, training=True) 在训练期间,以服从伯努利分布的概率 `p` 随机将输入Tensor的某些通道归零(对于形状为 `NCDHW` 的 `5D` Tensor,其通道特征图指的是后三维 `DHW` 形状的三维特征图)。 例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `3D` Tensor 是一个待处理数据。 @@ -10,17 +10,18 @@ mindspore.ops.dropout3d `dropout3d` 可以提高通道特征映射之间的独立性。 参数: - - **x** (Tensor) - 一个形状为 :math:`(N, C, D, H, W)` 的 `5D` Tensor,其中N是批处理大小,`C` 是通道数,`D` 是特征深度, `H` 是特征高度,`W` 是特征宽度。其数据类型应为int8、int16、int32、int64、float16、float32或float64。 + - **input** (Tensor) - 一个形状为 :math:`(N, C, D, H, W)` 的 `5D` Tensor,其中N是批处理大小,`C` 是通道数,`D` 是特征深度, `H` 是特征高度,`W` 是特征宽度。其数据类型应为int8、int16、int32、int64、float16、float32或float64。 - **p** (float) - 通道的丢弃概率,介于 0 和 1 之间,例如 `p` = 0.8,意味着80%的清零概率。默认值:0.5。 + - **training** (bool) - 如果training为True, 则执行对`input`的某些通道概率清零的操作,否则,不执行。默认值:True。 返回: - - Tensor,输出,具有与输入 `x` 相同的形状和数据类型。 - - Tensor,掩码,形状与 `x` 相同,数据类型为bool。 + - Tensor,输出,具有与输入 `input` 相同的形状和数据类型。 + - Tensor,掩码,形状与 `input` 相同,数据类型为bool。 异常: - - **TypeError** - `x` 不是Tensor。 - - **TypeError** - `x` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 + - **TypeError** - `input` 不是Tensor。 + - **TypeError** - `input` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 - **TypeError** - `p` 的数据类型不是float。 - **ValueError** - `p` 值不在 `[0.0,1.0]` 之间。 - - **ValueError** - `x` 的维度不等于5。 + - **ValueError** - `input` 的维度不等于5。 diff --git a/mindspore/python/mindspore/ops/function/nn_func.py b/mindspore/python/mindspore/ops/function/nn_func.py index 7bd987adfe8..f9afcb4c9e4 100644 --- a/mindspore/python/mindspore/ops/function/nn_func.py +++ b/mindspore/python/mindspore/ops/function/nn_func.py @@ -1305,7 +1305,7 @@ def dropout1d(x, p=0.5, training=True): return out -def dropout2d(x, p=0.5): +def dropout2d(input, p=0.5, training=True): r""" During training, randomly zeroes some channels of the input tensor with probability `p` from a Bernoulli distribution(For a 4-dimensional tensor with a shape of :math:`NCHW`, @@ -1324,38 +1324,40 @@ def dropout2d(x, p=0.5): `dropout2d` can improve the independence between channel feature maps. Args: - x (Tensor): A `4D` tensor with shape :math:`(N, C, H, W)`, where `N` is the batch size, `C` is the number + input (Tensor): A `4D` tensor with shape :math:`(N, C, H, W)`, where `N` is the batch size, `C` is the number of channels, `H` is the feature height, and `W` is the feature width. The data type must be int8, int16, int32, int64, float16, float32 or float64. p (float): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, which means dropping out 80% of channels. Default: 0.5. + training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: True. Returns: - Tensor, output, with the same shape and data type as `x`. - - Tensor, mask, with the same shape as `x` and the data type is bool. + Tensor, output, with the same shape and data type as `input`. Raises: - TypeError: If `x` is not a Tensor. - TypeError: If dtype of `x` is not int8, int16, int32, int64, float16, float32 or float64. + TypeError: If `input` is not a Tensor. + TypeError: If dtype of `input` is not int8, int16, int32, int64, float16, float32 or float64. TypeError: If the data type of `p` is not float. ValueError: If `p` is out of the range `[0.0, 1.0]`. - ValueError: If `x` shape is not `4D`. + ValueError: If `input` shape is not `4D`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: - >>> input_x = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32) - >>> output, mask = ops.dropout2d(input_x, 0.5) + >>> input = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32) + >>> output = ops.dropout2d(input, 0.5) >>> print(output.shape) (2, 1, 2, 3) """ + if training is False: + return input dropout_2d_op = NN_OPS.Dropout2D(1.0 - p) - return dropout_2d_op(x) + out, _ = dropout_2d_op(input) + return out -def dropout3d(x, p=0.5): +def dropout3d(input, p=0.5, training=True): r""" During training, randomly zeroes some channels of the input tensor with probability `p` from a Bernoulli distribution(For a 5-dimensional tensor @@ -1370,35 +1372,37 @@ def dropout3d(x, p=0.5): `dropout3d` can improve the independence between channel feature maps. Args: - x (Tensor): A `5D` tensor with shape :math:`(N, C, D, H, W)`, where `N` is the batch size, `C` is the number + input (Tensor): A `5D` tensor with shape :math:`(N, C, D, H, W)`, where `N` is the batch size, `C` is the number of channels, `D` is the feature depth, `H` is the feature height, and `W` is the feature width. The data type must be int8, int16, int32, int64, float16, float32 or float64. p (float): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, which means dropping out 80% of channels. Default: 0.5. + training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: True. Returns: - Tensor, output, with the same shape and data type as `x`. - - Tensor, mask, with the same shape as `x` and the data type is bool. + Tensor, output, with the same shape and data type as `input`. Raises: - TypeError: If `x` is not a Tensor. - TypeError: If dtype of `x` is not int8, int16, int32, int64, float16, float32 or float64. + TypeError: If `input` is not a Tensor. + TypeError: If dtype of `input` is not int8, int16, int32, int64, float16, float32 or float64. TypeError: If the data type of `p` is not float. ValueError: If `p` is out of the range `[0.0, 1.0]`. - ValueError: If `x` shape is not 5D. + ValueError: If `input` shape is not 5D. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: - >>> input_x = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32) - >>> output, mask = ops.dropout3d(input_x, 0.5) + >>> input = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32) + >>> output = ops.dropout3d(input, 0.5) >>> print(output.shape) (2, 1, 2, 1, 2) """ + if training is False: + return input dropout_3d_op = NN_OPS.Dropout3D(1.0 - p) - return dropout_3d_op(x) + out, _ = dropout_3d_op(input) + return out def fast_gelu(x):