Skip to content

Commit

Permalink
[xdoctest] reformat example code with google style in No 101 (#55968)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

---------

Co-authored-by: Nyakku Shigure <[email protected]>
  • Loading branch information
Liyulingyue and SigureMo authored Aug 14, 2023
1 parent 2ac6a7e commit 052e7b0
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions python/paddle/nn/utils/weight_norm_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 052e7b0

Please sign in to comment.