weight_norm_hook.py 8.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
Z
zhiboniu 已提交
14
import paddle
15 16
from paddle import _C_ops

17 18
from ...fluid import layers as F
from ...fluid.data_feeder import check_variable_and_dtype
19
from ...fluid.layer_helper import LayerHelper
20
from ...framework import in_dygraph_mode
21

22 23
__all__ = []

24 25 26 27

def l2_norm(x, axis, epsilon=1e-12, name=None):
    if len(x.shape) == 1:
        axis = 0
28 29

    if in_dygraph_mode():
30
        out, norm = _C_ops.norm(x, 1 if axis is None else axis, epsilon, False)
31 32
        return paddle.squeeze(norm, axis=[axis])

33 34 35 36 37
    check_variable_and_dtype(x, "X", ("float32", "float64"), "norm")

    helper = LayerHelper("l2_normalize", **locals())
    out = helper.create_variable_for_type_inference(dtype=x.dtype)
    norm = helper.create_variable_for_type_inference(dtype=x.dtype)
38 39 40 41 42 43 44 45 46
    helper.append_op(
        type="norm",
        inputs={"X": x},
        outputs={"Out": out, "Norm": norm},
        attrs={
            "axis": 1 if axis is None else axis,
            "epsilon": epsilon,
        },
    )
Z
zhiboniu 已提交
47
    return paddle.squeeze(norm, axis=[axis])
48 49 50 51 52 53


def norm_except_dim(p, dim):
    shape = p.shape
    ndims = len(shape)
    if dim == -1:
Z
zhiboniu 已提交
54
        return paddle.sqrt(paddle.sum(paddle.square(p)) + 1e-12)
55
    elif dim == 0:
Z
zhiboniu 已提交
56
        p_matrix = paddle.reshape(p, (shape[0], -1))
57 58
        return l2_norm(p_matrix, axis=1)
    elif dim == ndims - 1:
Z
zhiboniu 已提交
59
        p_matrix = paddle.reshape(p, (-1, shape[-1]))
60 61 62 63 64
        return l2_norm(p_matrix, axis=0)
    else:
        perm = list(range(ndims))
        perm[0] = dim
        perm[dim] = 0
Z
zhiboniu 已提交
65
        p_transposed = paddle.transpose(p, perm)
66 67 68 69 70 71 72 73
        return norm_except_dim(p_transposed, 0)


def _weight_norm(v, g, dim):
    shape = v.shape
    ndims = len(shape)

    if dim == -1:
Z
zhiboniu 已提交
74
        v_normalized = v / (paddle.sqrt(paddle.sum(paddle.square(v))) + 1e-12)
75
    elif dim == 0:
Z
zhiboniu 已提交
76
        p_matrix = paddle.reshape(v, (shape[0], -1))
77
        v_normalized = F.l2_normalize(p_matrix, axis=1)
Z
zhiboniu 已提交
78
        v_normalized = paddle.reshape(v_normalized, shape)
79
    elif dim == ndims - 1:
Z
zhiboniu 已提交
80
        p_matrix = paddle.reshape(v, (-1, shape[-1]))
81
        v_normalized = F.l2_normalize(p_matrix, axis=0)
Z
zhiboniu 已提交
82
        v_normalized = paddle.reshape(v_normalized, shape)
83 84 85 86
    else:
        perm = list(range(ndims))
        perm[0] = dim
        perm[dim] = 0
Z
zhiboniu 已提交
87
        p_transposed = paddle.transpose(v, perm)
88
        transposed_shape = p_transposed.shape
Z
zhiboniu 已提交
89
        p_matrix = paddle.reshape(p_transposed, (p_transposed.shape[0], -1))
90
        v_normalized = F.l2_normalize(p_matrix, axis=1)
Z
zhiboniu 已提交
91 92
        v_normalized = paddle.reshape(v_normalized, transposed_shape)
        v_normalized = paddle.transpose(v_normalized, perm)
93
    weight = paddle.tensor.math._multiply_with_axis(
94 95
        v_normalized, g, axis=dim if dim is not None else -1
    )
96 97 98
    return weight


99
class WeightNorm:
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    def __init__(self, name, dim):
        if dim is None:
            dim = -1
        self.name = name
        self.dim = dim

    def compute_weight(self, layer):
        g = getattr(layer, self.name + '_g')
        v = getattr(layer, self.name + '_v')
        return _weight_norm(v, g, self.dim)

    @staticmethod
    def apply(layer, name, dim):
        for k, hook in layer._forward_pre_hooks.items():
            if isinstance(hook, WeightNorm) and hook.name == name:
115 116 117 118
                raise RuntimeError(
                    "Cannot register two weight_norm hooks on "
                    "the same parameter {}".format(name)
                )
119 120 121 122

        if dim is None:
            dim = -1

123 124 125 126 127 128 129 130
        # support dim is negative numeber, (dim = -1) == (dim = None)
        weight_dim = len(layer._parameters[name].shape)
        assert (
            dim < weight_dim and dim >= -1 * weight_dim
        ), "dim must set between [-R, R), R means the dimension of weight."
        if dim != -1:
            dim = (dim + weight_dim) % weight_dim

131 132 133 134 135 136 137 138 139 140
        fn = WeightNorm(name, dim)

        w = getattr(layer, name)
        del layer._parameters[name]

        g_var = norm_except_dim(w, dim)
        v = layer.create_parameter(w.shape, dtype=w.dtype)
        layer.add_parameter(name + "_v", v)
        g = layer.create_parameter(g_var.shape, dtype=g_var.dtype)
        layer.add_parameter(name + '_g', g)
Z
zhiboniu 已提交
141 142 143
        with paddle.no_grad():
            paddle.assign(w, v)
            paddle.assign(g_var, g)
144 145 146 147 148 149 150 151 152 153 154 155
        setattr(layer, name, fn.compute_weight(layer))

        layer.register_forward_pre_hook(fn)
        return fn

    def remove(self, layer):
        w_var = self.compute_weight(layer)
        delattr(layer, self.name)
        del layer._parameters[self.name + '_g']
        del layer._parameters[self.name + '_v']
        w = layer.create_parameter(w_var.shape, dtype=w_var.dtype)
        layer.add_parameter(self.name, w)
Z
zhiboniu 已提交
156 157
        with paddle.no_grad():
            paddle.assign(w_var, w)
158 159 160 161 162 163

    def __call__(self, layer, inputs):
        setattr(layer, self.name, self.compute_weight(layer))


def weight_norm(layer, name='weight', dim=0):
164
    r"""
165
    Applies weight normalization to a parameter according to the
166 167 168 169 170 171
    following formula:

    .. math::

        \mathbf{w} = g \dfrac{v}{\|v\|}

172 173 174 175 176
    Weight normalization is a reparameterization of the weight vectors in a neural network that
    decouples the magnitude of those weight vectors from their direction. Weight normalization
    replaces the parameter specified by `name`(eg: 'weight') with two parameters: one parameter
    specifying the magnitude (eg: 'weight_g') and one parameter specifying the direction
    (eg: 'weight_v'). Weight normalization has been implemented as discussed in this paper:
177 178 179 180 181 182
    `Weight Normalization: A Simple Reparameterization to Accelerate Training of Deep Neural Networks
    <https://arxiv.org/pdf/1602.07868.pdf>`_.

    Parameters:
        layer(Layer): Layer of paddle, which has weight.
        name(str, optional): Name of the weight parameter. Default: 'weight'.
183 184 185
        dim(int, optional): Dimension over which to compute the norm. Dim is a non-negative number
              which is less than the rank of weight Tensor. For Example, dim can be chosen from 0,
              1, 2, 3 for convolution whose weight shape is [cout, cin, kh, kw] and rank is 4.
186
              If dim is set to None, meaning that all elements will be normalized. Default: 0.
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    Returns:
        Origin layer with weight norm hook.

    Examples:
        .. code-block:: python

          from paddle.nn import Conv2D
          from paddle.nn.utils import weight_norm

          conv = Conv2D(3, 5, 3)
          wn = weight_norm(conv)
          print(conv.weight_g.shape)
          # [5]
          print(conv.weight_v.shape)
          # [5, 3, 3, 3]
    """
    WeightNorm.apply(layer, name, dim)
    return layer


def remove_weight_norm(layer, name='weight'):
    """
    remove weight normalization from layer.

    Parameters:
        layer(Layer): Layer of paddle, which has weight.
        name(str, optional): Name of the weight parameter. Default: 'weight'.

    Returns:
217
        Layer, the origin layer without weight norm
218 219 220

    Examples:
        .. code-block:: python
221

C
Chen Long 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
            import paddle
            from paddle.nn import Conv2D
            from paddle.nn.utils import weight_norm, remove_weight_norm

            conv = Conv2D(3, 5, 3)
            wn = weight_norm(conv)
            print(conv.weight_g)
            # Parameter containing:
            # Tensor(shape=[5], dtype=float32, place=Place(gpu:0), stop_gradient=False,
            #        [0., 0., 0., 0., 0.])
            # Conv2D(3, 5, kernel_size=[3, 3], data_format=NCHW)

            remove_weight_norm(conv)
            # print(conv.weight_g)
            # AttributeError: 'Conv2D' object has no attribute 'weight_g'
237 238 239 240 241 242 243 244
    """
    for k, hook in layer._forward_pre_hooks.items():
        if isinstance(hook, WeightNorm) and hook.name == name:
            hook.remove(layer)
            del layer._forward_pre_hooks[k]
            return layer

    raise ValueError("weight_norm of '{}' not found in {}".format(name, layer))