norm.py 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15 16
import numbers

17
# TODO: define normalization api
18 19
import paddle
import paddle.fluid as fluid
20
from paddle import _C_ops, _legacy_C_ops, in_dynamic_mode
21
from paddle.fluid.framework import _in_legacy_dygraph, in_dygraph_mode
22

23 24 25 26
from ...fluid import dygraph_utils
from ...fluid.data_feeder import check_type, check_variable_and_dtype
from ...fluid.layer_helper import LayerHelper

27 28
__all__ = []

29 30

def normalize(x, p=2, axis=1, epsilon=1e-12, name=None):
31
    r"""
32
    Normalize ``x`` along dimension ``axis`` using :math:`L_p` norm. This layer computes
33 34 35

    .. math::

36
        y = \frac{x}{ \max\left( \lvert \lvert x \rvert \rvert_p, epsilon\right) }
37

38
    .. math::
39
        \lvert \lvert x \rvert \rvert_p = \left( \sum_i {\lvert x_i \rvert^p}  \right)^{1/p}
40

41
    where, :math:`\sum_i{\lvert x_i \rvert^p}` is calculated along the ``axis`` dimension.
42 43


N
Noel 已提交
44
    Parameters:
45
        x (Tensor): The input tensor could be N-D tensor, and the input data type could be float32 or float64.
46
        p (float|int, optional): The exponent value in the norm formulation. Default: 2.
47
        axis (int, optional): The axis on which to apply normalization. If `axis < 0`, the dimension to normalization is `x.ndim + axis`. -1 is the last dimension.
48 49 50 51 52 53 54 55 56 57 58 59 60 61
        epsilon (float, optional): Small float added to denominator to avoid dividing by zero. Default is 1e-12.
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor, the output has the same shape and data type with ``x``.

    Examples:

        .. code-block:: python

            import paddle
            import paddle.nn.functional as F

            paddle.disable_static()
62
            x = paddle.arange(6, dtype="float32").reshape([2,3])
63
            y = F.normalize(x)
64 65 66 67
            print(y)
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[0.        , 0.44721359, 0.89442718],
            #         [0.42426404, 0.56568539, 0.70710671]])
68 69

            y = F.normalize(x, p=1.5)
70 71 72 73
            print(y)
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[0.        , 0.40862012, 0.81724024],
            #         [0.35684016, 0.47578689, 0.59473360]])
74 75

            y = F.normalize(x, axis=0)
76 77 78 79
            print(y)
            # Tensor(shape=[2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[0.        , 0.24253564, 0.37139067],
            #         [1.        , 0.97014254, 0.92847669]])
80
    """
81 82
    if in_dygraph_mode():
        eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype)
83 84
        out = _C_ops.p_norm(x, float(p), axis, epsilon, True, False)
        return x / _C_ops.maximum(out, eps)
85 86

    if _in_legacy_dygraph():
87
        eps = fluid.dygraph.base.to_variable([epsilon], dtype=x.dtype)
88 89 90 91 92 93 94 95 96 97 98
        out = _legacy_C_ops.p_norm(
            x,
            'axis',
            axis,
            'porder',
            float(p),
            'keepdim',
            True,
            'epsilon',
            epsilon,
        )
99
        return x / _legacy_C_ops.elementwise_max(out, eps)
100 101 102

    check_type(p, 'p', (float, int), 'normalize')
    check_type(axis, 'axis', (int), 'normalize')
103 104 105
    check_variable_and_dtype(
        x, 'x', ['float16', 'float32', 'float64'], 'normalize'
    )
106 107
    if len(x.shape) == 1 and axis != 0 and axis != -1:
        raise ValueError(
108 109 110 111
            "Axis must be 0 or -1 when x is a 1-D tensor, but received axis = {}".format(
                axis
            )
        )
112 113 114 115 116 117 118 119 120

    attrs = {
        'axis': axis,
        'porder': float(p),
        'keepdim': True,
        'epsilon': epsilon,
    }
    helper = LayerHelper('p_norm', **locals())
    out = helper.create_variable_for_type_inference(dtype=x.dtype)
121 122 123
    helper.append_op(
        type='p_norm', inputs={'X': x}, outputs={'Out': out}, attrs=attrs
    )
124
    eps = out.block.create_var(dtype=out.dtype)
Z
zhiboniu 已提交
125
    eps = paddle.full(shape=[1], fill_value=epsilon, dtype=out.dtype)
126
    return paddle.divide(x, paddle.maximum(out, eps), name=name)
127 128


129 130 131 132 133 134 135 136 137 138 139 140 141
def batch_norm(
    x,
    running_mean,
    running_var,
    weight,
    bias,
    training=False,
    momentum=0.9,
    epsilon=1e-05,
    data_format="NCHW",
    use_global_stats=None,
    name=None,
):
142 143 144
    """
    Applies Batch Normalization as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .

C
cnn 已提交
145
    nn.functional.batch_norm is uesd for nn.BatchNorm1D, nn.BatchNorm2D, nn.BatchNorm3D. Please use above API for BatchNorm.
146

147 148 149 150 151
    Parameters:
        x(Tesnor): input value. It's data type should be float32, float64.
        running_mean(Tensor): running mean.
        running_var(Tensor): running variance.
        weight(Tensor): The weight tensor of batch_norm, can not be None.
152
        bias(Tensor): The bias tensor of batch_norm can not be None.
153 154
        epsilon(float, optional): The small value added to the variance to prevent division by zero. Default: 1e-5.
        momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
155 156
        training(bool, optional): True means train mode which compute by batch data and track global mean and var during train period. False means inference mode which compute by global mean and var which calculated by train period. Default False.
        data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Default "NCHW".
C
ceci3 已提交
157
        use_global_stats(bool|None, optional): Whether to use global mean and variance. If set to False, use the statistics of one mini-batch, if set to True, use the global statistics, if set to None, use global statistics in the test phase and use the statistics of one mini-batch in the training phase. Default: None.
158 159 160 161 162 163 164 165
        name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        None

    Examples:
        .. code-block:: python

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            import paddle

            x = paddle.arange(12, dtype="float32").reshape([2, 1, 2, 3])
            print(x)
            # Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[[[0. , 1. , 2. ],
            #           [3. , 4. , 5. ]]],

            #         [[[6. , 7. , 8. ],
            #           [9. , 10., 11.]]]])

            running_mean = paddle.to_tensor([0], dtype="float32")
            running_variance = paddle.to_tensor([1], dtype="float32")
            weight = paddle.to_tensor([2], dtype="float32")
            bias = paddle.to_tensor([1], dtype="float32")

            batch_norm_out = paddle.nn.functional.batch_norm(x, running_mean,
                                                        running_variance, weight, bias)
            print(batch_norm_out)
            # Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [[[[1.         , 2.99998999 , 4.99997997 ],
            #           [6.99996948 , 8.99995995 , 10.99994946]]],

            #         [[[12.99993896, 14.99992943, 16.99991989],
            #           [18.99990845, 20.99989891, 22.99988937]]]])
191 192 193 194 195 196 197
    """
    assert len(x.shape) >= 2, "input dim must be larger than 1"

    # input ad out must share the memory
    mean_out = running_mean
    variance_out = running_var

F
Feiyu Chan 已提交
198
    true_data_format = ['NC', 'NCL', 'NCHW', 'NCDHW', 'NLC', 'NHWC', 'NDHWC']
199 200
    if data_format not in true_data_format:
        raise ValueError(
F
Feiyu Chan 已提交
201
            "data_format must be one of 'NC', 'NCL', 'NCHW', 'NCDHW', "
202 203
            "'NLC', 'NHWC', 'NDHWC' but receive {}".format(data_format)
        )
204

F
Feiyu Chan 已提交
205
    data_format = 'NCHW' if data_format[1] == 'C' else 'NHWC'
206

207
    if use_global_stats is None:
C
ceci3 已提交
208 209 210 211 212
        use_global_stats = not training
        trainable_statistics = False
    else:
        trainable_statistics = not use_global_stats

213
    if in_dygraph_mode():
214
        batch_norm_out, _, _, _, _, _ = _C_ops.batch_norm(
215 216 217
            x,
            running_mean,
            running_var,
218 219 220
            weight,
            bias,
            not training,
221 222 223 224 225 226 227 228 229 230
            momentum,
            epsilon,
            data_format,
            use_global_stats,
            trainable_statistics,
        )

        return dygraph_utils._append_activation_in_dygraph(
            batch_norm_out, act=None
        )
231 232 233

    elif _in_legacy_dygraph():
        # for dygraph need tuple
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        attrs = (
            "momentum",
            momentum,
            "epsilon",
            epsilon,
            "is_test",
            not training,
            "data_layout",
            data_format,
            "use_mkldnn",
            False,
            "fuse_with_relu",
            False,
            "use_global_stats",
            use_global_stats,
            "trainable_statistics",
            trainable_statistics,
        )
252

253
        batch_norm_out, _, _, _, _, _ = _legacy_C_ops.batch_norm(
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
            x,
            weight,
            bias,
            running_mean,
            running_var,
            None,
            mean_out,
            variance_out,
            *attrs
        )

        return dygraph_utils._append_activation_in_dygraph(
            batch_norm_out, act=None
        )

    check_variable_and_dtype(
        x, 'input', ['float16', 'float32', 'float64'], 'BatchNorm'
    )
272 273 274 275 276

    # for static need dict
    attrs = {
        "momentum": momentum,
        "epsilon": epsilon,
277
        "is_test": not training,
278 279 280 281
        "data_layout": data_format,
        "use_mkldnn": False,
        "fuse_with_relu": False,
        "use_global_stats": use_global_stats,
C
ceci3 已提交
282
        "trainable_statistics": trainable_statistics,
283 284 285 286 287 288 289
    }

    inputs = {
        "X": [x],
        "Scale": [weight],
        "Bias": [bias],
        "Mean": [running_mean],
290
        "Variance": [running_var],
291 292 293 294
    }

    helper = LayerHelper('batch_norm', **locals())

295
    param_dtype = x.dtype if x.dtype != 'float16' else 'float32'
296 297 298
    saved_mean = helper.create_variable_for_type_inference(
        dtype=param_dtype, stop_gradient=True
    )
299
    saved_variance = helper.create_variable_for_type_inference(
300 301
        dtype=param_dtype, stop_gradient=True
    )
302
    batch_norm_out = helper.create_variable_for_type_inference(x.dtype)
303 304 305 306 307 308

    outputs = {
        "Y": [batch_norm_out],
        "MeanOut": [running_mean],
        "VarianceOut": [running_var],
        "SavedMean": [saved_mean],
309
        "SavedVariance": [saved_variance],
310 311
    }

312 313 314
    if training or trainable_statistics:
        # reserve_space is only used for training.
        reserve_space = helper.create_variable_for_type_inference(
315 316
            dtype=x.dtype, stop_gradient=True
        )
317 318
        outputs["ReserveSpace"] = [reserve_space]

319 320 321
    helper.append_op(
        type="batch_norm", inputs=inputs, outputs=outputs, attrs=attrs
    )
322 323 324 325

    return helper.append_activation(batch_norm_out)


326 327 328
def layer_norm(
    x, normalized_shape, weight=None, bias=None, epsilon=1e-05, name=None
):
329 330
    """
    see more detail in paddle.nn.LayerNorm
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    Parameters:
        x(Tensor): Input Tensor. It's data type should be float32, float64.
        normalized_shape(int|list|tuple): Input shape from an expected input of
            size :math:`[*, normalized_shape[0], normalized_shape[1], ..., normalized_shape[-1]]`.
            If it is a single integer, this module will normalize over the last dimension
            which is expected to be of that specific size.
        epsilon(float, optional): The small value added to the variance to prevent
            division by zero. Default: 1e-05.
        weight(Tensor, optional): The weight tensor of batch_norm. Default: None.
        bias(Tensor, optional): The bias tensor of batch_norm. Default: None.
        name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        None

    Examples:

        .. code-block:: python

          import paddle

353
          x = paddle.rand((2, 2, 2, 3))
C
Chen Long 已提交
354
          layer_norm_out = paddle.nn.functional.layer_norm(x, x.shape[1:])
Z
zhang wenhui 已提交
355
          print(layer_norm_out)
356 357 358
    """
    input_shape = list(x.shape)
    input_ndim = len(input_shape)
359 360 361 362 363 364
    if isinstance(normalized_shape, numbers.Integral):
        normalized_shape = [normalized_shape]
    elif isinstance(normalized_shape, tuple):
        normalized_shape = list(normalized_shape)
    elif not isinstance(normalized_shape, list):
        raise ValueError(
365 366
            "`normalized_shape` should be int, list of ints or tuple of ints."
        )
367

368 369
    normalized_ndim = len(normalized_shape)
    begin_norm_axis = input_ndim - normalized_ndim
370 371 372 373
    if (
        input_ndim < normalized_ndim
        or input_shape[begin_norm_axis:] != normalized_shape
    ):
374
        str_normalized_shape = str(normalized_shape)
375 376 377 378 379 380 381 382
        raise ValueError(
            'Given normalized_shape is '
            + str_normalized_shape
            + ', expected input with shape [*, '
            + str_normalized_shape[1:]
            + ', but got input shape '
            + str(input_shape)
        )
383

H
hong 已提交
384
    if in_dygraph_mode():
385 386 387 388
        (
            pre_act,
            _,
            _,
389
        ) = _C_ops.layer_norm(x, weight, bias, epsilon, begin_norm_axis)
H
hong 已提交
390 391 392 393

        return dygraph_utils._append_activation_in_dygraph(pre_act, act=None)

    if _in_legacy_dygraph():
394 395 396 397 398 399 400 401 402
        pre_act, _, _ = _legacy_C_ops.layer_norm(
            x,
            weight,
            bias,
            'epsilon',
            epsilon,
            'begin_norm_axis',
            begin_norm_axis,
        )
403 404
        return dygraph_utils._append_activation_in_dygraph(pre_act, act=None)

405 406 407
    check_variable_and_dtype(
        x, 'input', ['float16', 'float32', 'float64'], 'LayerNorm'
    )
408 409 410 411 412 413 414 415 416 417 418

    inputs = dict()
    inputs['X'] = [x]
    if weight:
        inputs['Scale'] = [weight]
    if bias:
        inputs['Bias'] = [bias]
    attrs = {"epsilon": epsilon, "begin_norm_axis": begin_norm_axis}

    # create output
    helper = LayerHelper('layer_norm', **locals())
F
furnace 已提交
419 420

    dtype = x.dtype
421 422 423 424 425 426
    mean_out = helper.create_variable_for_type_inference(
        dtype=dtype, stop_gradient=True
    )
    variance_out = helper.create_variable_for_type_inference(
        dtype=dtype, stop_gradient=True
    )
F
furnace 已提交
427
    layer_norm_out = helper.create_variable_for_type_inference(dtype)
428

429 430 431 432 433 434 435 436 437 438
    helper.append_op(
        type="layer_norm",
        inputs=inputs,
        outputs={
            "Y": layer_norm_out,
            "Mean": mean_out,
            "Variance": variance_out,
        },
        attrs={"epsilon": epsilon, "begin_norm_axis": begin_norm_axis},
    )
439 440 441 442

    return helper.append_activation(layer_norm_out)


443 444 445 446 447 448 449 450 451 452 453 454
def instance_norm(
    x,
    running_mean=None,
    running_var=None,
    weight=None,
    bias=None,
    use_input_stats=True,
    momentum=0.9,
    eps=1e-05,
    data_format="NCHW",
    name=None,
):
455
    """
C
cnn 已提交
456
    See more detail in nn.layer.InstanceNorm2D.
457 458 459

    Parameters:
        x(Tensor): Input Tensor. It's data type should be float32, float64.
D
duanboqiang 已提交
460 461
        running_mean(Tensor, optional): running mean. Default None. Obsolete (that is, no longer usable).
        running_var(Tensor, optional): running variance. Default None. Obsolete (that is, no longer usable).
462
        weight(Tensor, optional): The weight tensor of instance_norm. Default: None.
D
duanboqiang 已提交
463
            If its value is None, this parameter will be initialized by one.
464
        bias(Tensor, optional): The bias tensor of instance_norm. Default: None.
D
duanboqiang 已提交
465
            If its value is None, this parameter will be initialized by zero.
466 467
        eps(float, optional): A value added to the denominator for numerical stability. Default is 1e-5.
        momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
D
duanboqiang 已提交
468
        use_input_stats(bool, optional): Default True. Obsolete (that is, no longer usable).
469
        data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW" or "NCDHW". Defalut "NCHW".
470 471 472 473 474 475 476 477 478 479 480
        name(str, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        None.

    Examples:

        .. code-block:: python

          import paddle

481
          x = paddle.rand((2, 2, 2, 3))
C
Chen Long 已提交
482
          instance_norm_out = paddle.nn.functional.instance_norm(x)
483

Z
zhang wenhui 已提交
484
          print(instance_norm_out)
485 486

    """
487
    if in_dygraph_mode():
488
        out = _C_ops.instance_norm(x, weight, bias, eps)
489 490
        return out
    if _in_legacy_dygraph():
491 492 493 494 495 496 497 498 499 500 501
        out, _, _ = _legacy_C_ops.instance_norm(
            x,
            weight,
            bias,
            "epsilon",
            eps,
            "momentum",
            momentum,
            "data_format",
            data_format,
        )
502 503 504 505 506 507 508 509 510 511 512 513
        return out

    check_variable_and_dtype(x, 'input', ['float32', 'float64'], "InstanceNorm")

    attrs = {"epsilon": eps, "momentum": momentum, "data_format": data_format}

    if weight and bias:
        inputs = {"X": [x], "Scale": [weight], "Bias": [bias]}
    else:
        inputs = {"X": [x]}

    helper = LayerHelper('instance_norm', **locals())
514 515 516
    saved_mean = helper.create_variable_for_type_inference(
        dtype=x.dtype, stop_gradient=True
    )
517
    saved_variance = helper.create_variable_for_type_inference(
518 519
        dtype=x.dtype, stop_gradient=True
    )
520 521 522 523 524
    instance_norm_out = helper.create_variable_for_type_inference(x.dtype)

    outputs = {
        "Y": [instance_norm_out],
        "SavedMean": [saved_mean],
525
        "SavedVariance": [saved_variance],
526 527
    }

528 529 530
    helper.append_op(
        type="instance_norm", inputs=inputs, outputs=outputs, attrs=attrs
    )
531
    return instance_norm_out
532 533


534 535 536
def local_response_norm(
    x, size, alpha=1e-4, beta=0.75, k=1.0, data_format="NCHW", name=None
):
537
    r"""
538 539
    Local Response Normalization performs a type of "lateral inhibition" by normalizing over local input regions.
    For more information, please refer to `ImageNet Classification with Deep Convolutional Neural Networks <https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_
540

541
    The formula is as follows:
542

543
    .. math::
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
        Output(i, x, y) = Input(i, x, y) / \left(k + \alpha \sum\limits^{\min(C-1, i + size/2)}_{j = \max(0, i - size/2)}(Input(j, x, y))^2\right)^{\beta}

    In the above equation:

    - :math:`size` : The number of channels to sum over.
    - :math:`k` : The offset (avoid being divided by 0).
    - :math:`\\alpha` : The scaling parameter.
    - :math:`\\beta` : The exponent parameter.


    Args:
        x (Tensor): The input 3-D/4-D/5-D tensor. The data type is float32.
        size (int): The number of channels to sum over.
        alpha (float, optional): The scaling parameter, positive. Default:1e-4
        beta (float, optional): The exponent, positive. Default:0.75
        k (float, optional): An offset, positive. Default: 1.0
        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:
            If x is 3-D Tensor, the string could be `"NCL"` or `"NLC"` . When it is `"NCL"`,
            the data is stored in the order of: `[batch_size, input_channels, feature_length]`.
            If x is 4-D Tensor, the string could be  `"NCHW"`, `"NHWC"`. When it is `"NCHW"`,
            the data is stored in the order of: `[batch_size, input_channels, input_height, input_width]`.
            If x is 5-D Tensor, the string could be  `"NCDHW"`, `"NDHWC"` . When it is `"NCDHW"`,
            the data is stored in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
        name (str, optional): Name for the operation (optional, default is None). For more information,
            please refer to :ref:`api_guide_Name`.
571

572 573
    Returns:
        A tensor storing the transformation result with the same shape and data type as input.
574 575


576
    Examples:
577

578
    .. code-block:: python
579

580
        import paddle
581

582 583 584 585
        x = paddle.rand(shape=(3, 3, 112, 112), dtype="float32")
        y = paddle.nn.functional.local_response_norm(x, size=5)
        print(y.shape)  # [3, 3, 112, 112]
    """
Z
zhiboniu 已提交
586
    if not in_dynamic_mode():
587 588 589
        check_variable_and_dtype(x, 'x', ['float32'], 'local_response_norm')
    if data_format not in ['NCL', 'NLC', 'NCHW', 'NHWC', 'NCDHW', 'NDHWC']:
        raise ValueError(
590 591 592
            "data_format should be in one of [NCL, NCHW, NCDHW, NLC, NHWC, NDHWC], "
            "but got {}".format(data_format)
        )
593 594 595 596 597

    sizes = x.shape
    dim = len(sizes)
    if dim < 3:
        raise ValueError(
598 599 600 601
            'Expected 3D or higher dimensionality input, but got {} dimensions'.format(
                dim
            )
        )
602

H
huangjun12 已提交
603
    for i, sz in enumerate(sizes):
H
huangjun12 已提交
604
        if not sz > 0 and i > 0:
605 606 607 608
            raise ValueError(
                "Expected every dim's size to be larger than 0, "
                "but the size of the {}-th dim is {}".format(i, sz)
            )
H
huangjun12 已提交
609

610 611
    channel_last = True if data_format[-1] == "C" else False

612
    from functools import reduce
613

614 615
    sum_sizes = reduce(lambda x, y: x * y, sizes[1:])

616 617 618 619
    div = paddle.unsqueeze(paddle.multiply(x, x), axis=1)
    if not channel_last:
        pad4d_shape = [0, 0, size // 2, (size - 1) // 2]
        pool2d_shape = (size, 1)
620
        reshape_shape = [
621 622 623 624 625
            sizes[0],
            1,
            sizes[1],
            sizes[2],
            int(sum_sizes / (sizes[1] * sizes[2])),
626
        ]
627 628 629 630 631
        pad5d_shape = [0, 0, 0, 0, size // 2, (size - 1) // 2]
        pool3d_shape = (size, 1, 1)
    else:
        pad4d_shape = [size // 2, (size - 1) // 2, 0, 0]
        pool2d_shape = (1, size)
632
        reshape_shape = [
633 634 635 636 637
            sizes[0],
            1,
            sizes[1],
            int(sum_sizes / (sizes[1] * sizes[-1])),
            sizes[-1],
638
        ]
639 640 641 642 643
        pad5d_shape = [size // 2, (size - 1) // 2, 0, 0, 0, 0]
        pool3d_shape = (1, 1, size)

    if dim == 3:
        div = paddle.nn.functional.pad(div, pad=pad4d_shape)
644 645 646
        div = paddle.nn.functional.avg_pool2d(
            div, kernel_size=pool2d_shape, stride=1
        )
647 648 649
        div = paddle.squeeze(div, axis=1)
    else:
        div = paddle.reshape(div, shape=reshape_shape)
650 651 652 653 654 655
        div = paddle.nn.functional.pad(
            div, pad=pad5d_shape, data_format='NCDHW'
        )
        div = paddle.nn.functional.avg_pool3d(
            div, kernel_size=pool3d_shape, stride=1
        )
656 657 658 659 660 661
        div = paddle.reshape(paddle.squeeze(div, axis=1), sizes)

    div = paddle.scale(div, scale=alpha, bias=k)
    div = paddle.pow(div, beta)
    res = paddle.divide(x, div, name=name)
    return res