dropout2d¶
- paddle.nn.functional. dropout2d ( x, p=0.5, training=True, data_format='NCHW', name=None ) [source]
-
Randomly zero out entire channels (in the batched input 4d tensor with the shape NCHW , a channel is a 2D feature map with the shape HW ). Each channel will be zeroed out independently on every forward call with probability p using samples from a Bernoulli distribution.
See dropout for more details.
- Parameters
-
x (Tensor) – The input is 4-D Tensor with shape [N, C, H, W] or [N, H, W, C]. The data type is float16, float32 or float64.
p (float, optional) – Probability of setting units to zero. Default: 0.5.
training (bool, optional) – A flag indicating whether it is in train phrase or not. Default: True.
data_format (str, optional) – Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from NCHW or NHWC . When it is NCHW , the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. Default: NCHW .
name (str, optional) – Name for the operation, Default: None. For more information, please refer to Name.
- Returns
-
A Tensor representing the dropout2d, has same shape and data type as x .
Examples
>>> import paddle >>> paddle.seed(1) >>> x = paddle.randn(shape=(2, 3, 4, 5)).astype(paddle.float32) >>> y_train = paddle.nn.functional.dropout2d(x) #train >>> y_test = paddle.nn.functional.dropout2d(x, training=False) #test >>> for i in range(2): ... for j in range(3): ... print(x[i,j,:,:]) ... print(y_train[i,j,:,:]) # may all 0 ... print(y_test[i,j,:,:]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.30557564, 0.11855337, 0.41220093, -0.09968963, 1.50014710], [ 1.24004936, -0.92485696, 0.08612321, 1.15149164, -0.09276631], [ 1.22873247, -1.46587241, -1.30802727, 0.19496460, 1.73776841], [ 0.40092674, 0.67630458, 0.72265440, 1.31720388, -1.41899264]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.61115128, 0.23710674, 0.82440186, -0.19937925, 3.00029421], [ 2.48009872, -1.84971392, 0.17224643, 2.30298328, -0.18553263], [ 2.45746493, -2.93174481, -2.61605453, 0.38992921, 3.47553682], [ 0.80185348, 1.35260916, 1.44530880, 2.63440776, -2.83798528]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.30557564, 0.11855337, 0.41220093, -0.09968963, 1.50014710], [ 1.24004936, -0.92485696, 0.08612321, 1.15149164, -0.09276631], [ 1.22873247, -1.46587241, -1.30802727, 0.19496460, 1.73776841], [ 0.40092674, 0.67630458, 0.72265440, 1.31720388, -1.41899264]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.88350385, -1.14767575, 0.51043051, -0.10051888, -0.61305630], [-0.12084112, 0.48506257, -1.13189507, 0.62806708, -0.80003673], [ 0.51513153, -0.08890446, 0.22753835, 0.11557858, 0.78117645], [ 1.47505593, 0.84618902, -0.38528305, -1.05887091, 0.16592593]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 1.76700771, -2.29535151, 1.02086103, -0.20103776, -1.22611260], [-0.24168225, 0.97012514, -2.26379013, 1.25613415, -1.60007346], [ 1.03026307, -0.17780893, 0.45507669, 0.23115715, 1.56235290], [ 2.95011187, 1.69237804, -0.77056611, -2.11774182, 0.33185187]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.88350385, -1.14767575, 0.51043051, -0.10051888, -0.61305630], [-0.12084112, 0.48506257, -1.13189507, 0.62806708, -0.80003673], [ 0.51513153, -0.08890446, 0.22753835, 0.11557858, 0.78117645], [ 1.47505593, 0.84618902, -0.38528305, -1.05887091, 0.16592593]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-1.46668839, -0.38117948, 1.18678427, 0.38740095, 0.29117522], [-0.13538910, -0.14527084, -0.04912176, -0.26063353, 0.23640174], [ 0.45643106, 0.60587281, -1.03242552, -0.45319262, -1.57911122], [-0.08732958, -0.75898546, 0.14563090, -1.73751652, -0.89109969]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0., -0., 0. , 0. , 0. ], [-0., -0., -0., -0., 0. ], [0. , 0. , -0., -0., -0.], [-0., -0., 0. , -0., -0.]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-1.46668839, -0.38117948, 1.18678427, 0.38740095, 0.29117522], [-0.13538910, -0.14527084, -0.04912176, -0.26063353, 0.23640174], [ 0.45643106, 0.60587281, -1.03242552, -0.45319262, -1.57911122], [-0.08732958, -0.75898546, 0.14563090, -1.73751652, -0.89109969]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.32110816, -0.76044011, 0.34456784, -0.39410326, 0.37896338], [ 0.52747023, 0.72711533, 0.29204839, 0.72493637, 0.31128070], [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693], [ 0.96669912, 0.43670532, -0.38109761, 0.78405094, -2.17882323]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0., -0., 0. , -0., 0. ], [0. , 0. , 0. , 0. , 0. ], [0. , -0., -0., -0., -0.], [0. , 0. , -0., 0. , -0.]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[-0.32110816, -0.76044011, 0.34456784, -0.39410326, 0.37896338], [ 0.52747023, 0.72711533, 0.29204839, 0.72493637, 0.31128070], [ 0.58046782, -1.78499067, -1.67504823, -0.38590902, -0.26243693], [ 0.96669912, 0.43670532, -0.38109761, 0.78405094, -2.17882323]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.17168395, 0.45112833, 0.63307828, 2.38763475, -1.27247131], [ 0.56171960, -1.09584677, 0.38300961, -0.57512099, 0.31011426], [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209], [ 2.22819090, 1.12403083, -0.04198794, -1.51167727, -0.42699185]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[0. , 0. , 0. , 0. , -0.], [0. , -0., 0. , -0., 0. ], [-0., -0., -0., -0., -0.], [0. , 0. , -0., -0., -0.]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.17168395, 0.45112833, 0.63307828, 2.38763475, -1.27247131], [ 0.56171960, -1.09584677, 0.38300961, -0.57512099, 0.31011426], [-0.95336407, -1.04852903, -0.21312937, -0.53549880, -0.00074209], [ 2.22819090, 1.12403083, -0.04198794, -1.51167727, -0.42699185]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704], [ 1.04561794, 1.08428383, -0.52219963, -1.56003857, 0.89213932], [-0.16578521, 0.14524542, -0.45563069, 0.48180851, 1.35843253], [ 1.07669640, -0.84535235, -1.18651557, 0.79144061, -0.45565742]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[0. , -0., -0., -0., -0.], [0. , 0. , -0., -0., 0. ], [-0., 0. , -0., 0. , 0. ], [0. , -0., -0., 0. , -0.]]) Tensor(shape=[4, 5], dtype=float32, place=Place(cpu), stop_gradient=True, [[ 0.62503546, -0.20989063, -0.22046235, -0.38679042, -1.02590704], [ 1.04561794, 1.08428383, -0.52219963, -1.56003857, 0.89213932], [-0.16578521, 0.14524542, -0.45563069, 0.48180851, 1.35843253], [ 1.07669640, -0.84535235, -1.18651557, 0.79144061, -0.45565742]])