forked from mindspore-Ecosystem/mindspore
!49464 对ops.dropout2d和ops.dropput3d增加一个参数training, 若training为True,执行dropout操作,否则不执行dropout操作
Merge pull request !49464 from tangdezhi_123/br_tdz_1
This commit is contained in:
commit
a3e38979d8
|
@ -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。
|
||||
|
||||
|
|
|
@ -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。
|
||||
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue