对ops.dropout2d和ops.dropout3d增加一个参数training, training为True, 执行dropout操作,training为False, 不执行dropout操作; 将dropout2d和dropout3d的参数x重新命名为input, 以和pytorch对标。

This commit is contained in:
tangdezhi_123 2023-02-27 16:45:41 +08:00
parent dda93b6ca7
commit aae617fbb9
3 changed files with 42 additions and 36 deletions

View File

@ -1,7 +1,7 @@
mindspore.ops.dropout2d 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` 形状的二维特征图)。 在训练期间,以服从伯努利分布的概率 `p` 随机将输入Tensor的某些通道归零对于形状为 `NCHW` 的四维Tensor其通道特征图指的是后两维 `HW` 形状的二维特征图)。
例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `2D` Tensor 是一个待处理数据。 例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `2D` Tensor 是一个待处理数据。
@ -11,17 +11,18 @@ mindspore.ops.dropout2d
`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。 - **p** (float) - 通道的丢弃概率,介于 0 和 1 之间,例如 `p` = 0.8意味着80%的清零概率。默认值0.5。
- **training** (bool) - 如果training为True, 则执行对`input`的某些通道概率清零的操作否则不执行。默认值True。
返回: 返回:
- Tensor输出具有与输入 `x` 相同的形状和数据类型。 - Tensor输出具有与输入 `input` 相同的形状和数据类型。
- Tensor掩码形状与 `x` 相同数据类型为bool。 - Tensor掩码形状与 `input` 相同数据类型为bool。
异常: 异常:
- **TypeError** - `x` 不是Tensor。 - **TypeError** - `input` 不是Tensor。
- **TypeError** - `x` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 - **TypeError** - `input` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。
- **TypeError** - `p` 的数据类型不是float。 - **TypeError** - `p` 的数据类型不是float。
- **ValueError** - `p` 值不在 `[0.01.0]` 之间。 - **ValueError** - `p` 值不在 `[0.01.0]` 之间。
- **ValueError** - `x` 的维度不等于4。 - **ValueError** - `input` 的维度不等于4。

View File

@ -1,7 +1,7 @@
mindspore.ops.dropout3d 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` 形状的三维特征图)。 在训练期间,以服从伯努利分布的概率 `p` 随机将输入Tensor的某些通道归零对于形状为 `NCDHW``5D` Tensor其通道特征图指的是后三维 `DHW` 形状的三维特征图)。
例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `3D` Tensor 是一个待处理数据。 例如,在批处理输入中 :math:`i\_th` 批, :math:`j\_th` 通道的 `input[i, j]` `3D` Tensor 是一个待处理数据。
@ -10,17 +10,18 @@ mindspore.ops.dropout3d
`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。 - **p** (float) - 通道的丢弃概率,介于 0 和 1 之间,例如 `p` = 0.8意味着80%的清零概率。默认值0.5。
- **training** (bool) - 如果training为True, 则执行对`input`的某些通道概率清零的操作否则不执行。默认值True。
返回: 返回:
- Tensor输出具有与输入 `x` 相同的形状和数据类型。 - Tensor输出具有与输入 `input` 相同的形状和数据类型。
- Tensor掩码形状与 `x` 相同数据类型为bool。 - Tensor掩码形状与 `input` 相同数据类型为bool。
异常: 异常:
- **TypeError** - `x` 不是Tensor。 - **TypeError** - `input` 不是Tensor。
- **TypeError** - `x` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。 - **TypeError** - `input` 的数据类型不是int8、int16、int32、int64、float16、float32或float64。
- **TypeError** - `p` 的数据类型不是float。 - **TypeError** - `p` 的数据类型不是float。
- **ValueError** - `p` 值不在 `[0.0,1.0]` 之间。 - **ValueError** - `p` 值不在 `[0.0,1.0]` 之间。
- **ValueError** - `x` 的维度不等于5。 - **ValueError** - `input` 的维度不等于5。

View File

@ -1305,7 +1305,7 @@ def dropout1d(x, p=0.5, training=True):
return out return out
def dropout2d(x, p=0.5): def dropout2d(input, p=0.5, training=True):
r""" r"""
During training, randomly zeroes some channels of the input tensor with probability `p` 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`, 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. `dropout2d` can improve the independence between channel feature maps.
Args: 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, 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. int16, int32, int64, float16, float32 or float64.
p (float): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, 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. which means dropping out 80% of channels. Default: 0.5.
training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: True.
Returns: Returns:
Tensor, output, with the same shape and data type as `x`. Tensor, output, with the same shape and data type as `input`.
Tensor, mask, with the same shape as `x` and the data type is bool.
Raises: Raises:
TypeError: If `x` is not a Tensor. TypeError: If `input` is not a Tensor.
TypeError: If dtype of `x` is not int8, int16, int32, int64, float16, float32 or float64. 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. TypeError: If the data type of `p` is not float.
ValueError: If `p` is out of the range `[0.0, 1.0]`. 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: Supported Platforms:
``Ascend`` ``GPU`` ``CPU`` ``Ascend`` ``GPU`` ``CPU``
Examples: Examples:
>>> input_x = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32) >>> input = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32)
>>> output, mask = ops.dropout2d(input_x, 0.5) >>> output = ops.dropout2d(input, 0.5)
>>> print(output.shape) >>> print(output.shape)
(2, 1, 2, 3) (2, 1, 2, 3)
""" """
if training is False:
return input
dropout_2d_op = NN_OPS.Dropout2D(1.0 - p) 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""" r"""
During training, randomly zeroes some channels of the input tensor During training, randomly zeroes some channels of the input tensor
with probability `p` from a Bernoulli distribution(For a 5-dimensional 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. `dropout3d` can improve the independence between channel feature maps.
Args: 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. 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. 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, 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. which means dropping out 80% of channels. Default: 0.5.
training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: True.
Returns: Returns:
Tensor, output, with the same shape and data type as `x`. Tensor, output, with the same shape and data type as `input`.
Tensor, mask, with the same shape as `x` and the data type is bool.
Raises: Raises:
TypeError: If `x` is not a Tensor. TypeError: If `input` is not a Tensor.
TypeError: If dtype of `x` is not int8, int16, int32, int64, float16, float32 or float64. 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. TypeError: If the data type of `p` is not float.
ValueError: If `p` is out of the range `[0.0, 1.0]`. 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: Supported Platforms:
``Ascend`` ``GPU`` ``CPU`` ``Ascend`` ``GPU`` ``CPU``
Examples: Examples:
>>> input_x = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32) >>> input = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32)
>>> output, mask = ops.dropout3d(input_x, 0.5) >>> output = ops.dropout3d(input, 0.5)
>>> print(output.shape) >>> print(output.shape)
(2, 1, 2, 1, 2) (2, 1, 2, 1, 2)
""" """
if training is False:
return input
dropout_3d_op = NN_OPS.Dropout3D(1.0 - p) 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): def fast_gelu(x):