kaiming.py 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#   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.

# TODO: define the initializers of Kaiming functions in neural network
from ...fluid.initializer import MSRAInitializer

18 19
__all__ = []

20 21

class KaimingNormal(MSRAInitializer):
22
    r"""Implements the Kaiming Normal initializer
23 24 25 26 27 28 29 30 31 32 33 34 35

    This class implements the weight initialization from the paper
    `Delving Deep into Rectifiers: Surpassing Human-Level Performance on
    ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
    by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
    robust initialization method that particularly considers the rectifier
    nonlinearities.

    In case of Normal distribution, the mean is 0 and the standard deviation
    is

    .. math::

36
        \frac{gain}{\sqrt{{fan\_in}}}
37 38

    Args:
39 40 41 42 43
        fan_in (float32|None): fan_in (in_features) of trainable Tensor,\
        If None, it will be infered automaticly. If you don't want to use in_features of the Tensor,\
        you can set the value of 'fan_in' smartly by yourself. default is None.
        negative_slope (float, optional): negative_slope (only used with leaky_relu). default is 0.0.
        nonlinearity(str, optional): the non-linear function. default is relu.
44 45 46 47 48 49

    Note:
        It is recommended to set fan_in to None for most cases.

    Examples:
        .. code-block:: python
50
          :name: code-example1
51 52 53 54 55 56 57 58 59 60 61
            import paddle
            import paddle.nn as nn

            linear = nn.Linear(2,
                               4,
                               weight_attr=nn.initializer.KaimingNormal())
            data = paddle.rand([30, 10, 2], dtype='float32')
            res = linear(data)

    """

62
    def __init__(self, fan_in=None, negative_slope=0.0, nonlinearity='relu'):
63 64
        super(KaimingNormal, self).__init__(uniform=False,
                                            fan_in=fan_in,
65 66 67
                                            seed=0,
                                            negative_slope=negative_slope,
                                            nonlinearity=nonlinearity)
68 69 70


class KaimingUniform(MSRAInitializer):
71
    r"""Implements the Kaiming Uniform initializer
72 73 74 75 76 77 78 79 80 81 82 83

    This class implements the weight initialization from the paper
    `Delving Deep into Rectifiers: Surpassing Human-Level Performance on
    ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
    by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
    robust initialization method that particularly considers the rectifier
    nonlinearities.
    
    In case of Uniform distribution, the range is [-x, x], where

    .. math::

84
        x = gain \times \sqrt{\frac{3}{fan\_in}}
85 86

    Args:
87 88 89 90 91
        fan_in (float32|None): fan_in (in_features) of trainable Tensor,\
        If None, it will be infered automaticly. If you don't want to use in_features of the Tensor,\
        you can set the value of 'fan_in' smartly by yourself. default is None.
        negative_slope (float, optional): negative_slope (only used with leaky_relu). default is 0.0.
        nonlinearity(str, optional): the non-linear function. default is relu.
92 93 94 95 96 97

    Note:
        It is recommended to set fan_in to None for most cases.

    Examples:
        .. code-block:: python
98
          :name: code-example1
99 100 101 102 103 104 105 106 107 108 109
            import paddle
            import paddle.nn as nn

            linear = nn.Linear(2,
                               4,
                               weight_attr=nn.initializer.KaimingUniform())
            data = paddle.rand([30, 10, 2], dtype='float32')
            res = linear(data)

    """

110
    def __init__(self, fan_in=None, negative_slope=0.0, nonlinearity='relu'):
111 112
        super(KaimingUniform, self).__init__(uniform=True,
                                             fan_in=fan_in,
113 114 115
                                             seed=0,
                                             negative_slope=negative_slope,
                                             nonlinearity=nonlinearity)