dropout2d

paddle.nn.functional. dropout2d ( x, p=0.5, training=True, data_format='NCHW', name=None ) [源代码]

根据丢弃概率 p,在训练过程中随机将某些通道特征图置 0 (对一个形状为 NCHW 的 4 维 Tensor,通道特征图指的是其中的形状为 HW 的 2 维特征图)。

基于 paddle.nn.functional.dropout 实现,如您想了解更多,请参见 dropout

参数

  • x (Tensor) - 形状为 [N, C, H, W] 或 [N, H, W, C] 的 4D Tensor。数据类型为 float16、float32 或 float64。

  • p (float,可选) - 将输入通道置 0 的概率,即丢弃概率。默认值为 0.5。

  • training (bool,可选) - 标记是否为训练阶段。默认值为 True。

  • data_format (str,可选) - 指定输入的数据格式,输出的数据格式将与输入保持一致,可以是 NCHWNHWC。其中 N 是批尺寸,C 是通道数,H 是特征高度,W 是特征宽度。默认值为 NCHW

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

经过 dropout2d 之后的结果,与输入 x 形状相同的 Tensor

代码示例

>>> 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]])