From 052e7b079c591fcb84a709c5330ecf735a4eaf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Mon, 14 Aug 2023 10:56:55 +0800 Subject: [PATCH] [xdoctest] reformat example code with google style in No 101 (#55968) * weight_norm_hook * Update weight_norm_hook.py * Update weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py * Update python/paddle/nn/utils/weight_norm_hook.py Co-authored-by: Nyakku Shigure --------- Co-authored-by: Nyakku Shigure --- python/paddle/nn/utils/weight_norm_hook.py | 49 +++++++++++----------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/python/paddle/nn/utils/weight_norm_hook.py b/python/paddle/nn/utils/weight_norm_hook.py index 0c0851cdf31..3747a14cc11 100644 --- a/python/paddle/nn/utils/weight_norm_hook.py +++ b/python/paddle/nn/utils/weight_norm_hook.py @@ -191,15 +191,15 @@ def weight_norm(layer, name='weight', dim=0): 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] + >>> 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 @@ -219,22 +219,21 @@ def remove_weight_norm(layer, name='weight'): Examples: .. code-block:: python - 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) - # The following is the effect after removing the weight norm: - # print(conv.weight_g) - # AttributeError: 'Conv2D' object has no attribute 'weight_g' + >>> import paddle + >>> from paddle.nn import Conv2D + >>> from paddle.nn.utils import weight_norm, remove_weight_norm + >>> paddle.seed(2023) + + >>> conv = Conv2D(3, 5, 3) + >>> wn = weight_norm(conv) + >>> print(conv.weight_g) + Parameter containing: + Tensor(shape=[5], dtype=float32, place=Place(cpu), stop_gradient=False, + [1.35883713, 1.32126212, 1.56303072, 1.20874095, 1.22893476]) + >>> remove_weight_norm(conv) + >>> # The following is the effect after removing the weight norm: + >>> # print(conv.weight_g) + >>> # AttributeError: 'Conv2D' object has no attribute 'weight_g' """ for k, hook in layer._forward_pre_hooks.items(): if isinstance(hook, WeightNorm) and hook.name == name: -- GitLab