Skip to content

Commit dc12605

Browse files
TCChenlonglinjiecccweisy11RainFrost1wangna11BD
authored
[cherry-pick] Fix code examples (#43904)
* Update api docs (#42725) * Fix max_pool3d doc, test=document_fix (#42715) * fix pooling doc * fix typo test=document_fix * fix doc typo, test=document_fix * fix adaptive_avg_pool1d doc bug (#42721) * fix adaptive_avg_pool1d doc bug * fix adaptive_avg_pool1d doc bug * fix spectral_norm en doc (#42728) * Fix example code bugs (#42739) * update readme test=document_fix * fix api docs bugs test=document_fix * fix code example bugs;test=document_fix Co-authored-by: Linjie Chen <40840292+linjieccc@users.noreply.github.com> Co-authored-by: Wei Shengyu <weisy11@163.com> Co-authored-by: Walter <dongshl1226@hotmail.com> Co-authored-by: wangna11BD <79366697+wangna11BD@users.noreply.github.com>
1 parent 63458e5 commit dc12605

File tree

6 files changed

+49
-39
lines changed

6 files changed

+49
-39
lines changed

python/paddle/nn/functional/loss.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -389,20 +389,24 @@ def hsigmoid_loss(input,
389389
390390
paddle.set_device('cpu')
391391
392-
input = paddle.uniform([2, 3])
393-
# [[-0.8018668 0.8736385 -0.9064771 ] # random
394-
# [-0.10228515 -0.87188244 -0.8783718 ]] # random
392+
input = paddle.uniform([4, 3])
393+
# [[0.45424712 -0.77296764 0.82943869] # random
394+
# [0.85062802 0.63303483 0.35312140] # random
395+
# [0.57170701 0.16627562 0.21588242] # random
396+
# [0.27610803 -0.99303514 -0.17114788]] # random
395397
label = paddle.to_tensor([0, 1, 4, 5])
396398
num_classes = 5
397399
weight=paddle.uniform([num_classes-1, 3])
398-
# [[-0.24148715 0.8449961 -0.7399121 ] # random
399-
# [-0.9800559 0.43509364 0.9091208 ] # random
400-
# [ 0.60194826 0.10430074 -0.4521166 ] # random
401-
# [-0.4469818 -0.01536179 -0.604454 ]] # random
400+
# [[-0.64477652 0.24821866 -0.17456549] # random
401+
# [-0.04635394 0.07473493 -0.25081766] # random
402+
# [ 0.05986035 -0.12185556 0.45153677] # random
403+
# [-0.66236806 0.91271877 -0.88088769]] # random
402404
403405
out=F.hsigmoid_loss(input, label, num_classes, weight)
404-
# [[3.0159328]
405-
# [2.2407534]]
406+
# [[1.96709502]
407+
# [2.40019274]
408+
# [2.11009121]
409+
# [1.92374969]]
406410
"""
407411

408412
if in_dynamic_mode():

python/paddle/nn/functional/pooling.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,22 +1160,21 @@ def max_pool3d(x,
11601160
11611161
import paddle
11621162
import paddle.nn.functional as F
1163-
import numpy as np
11641163
11651164
# max pool3d
1166-
x = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32, 32]).astype(np.float32))
1167-
output = F.max_pool2d(x,
1165+
x = paddle.uniform([1, 3, 32, 32, 32])
1166+
output = F.max_pool3d(x,
11681167
kernel_size=2,
11691168
stride=2, padding=0)
1170-
output.shape [1, 3, 16, 16, 16]
1169+
# output.shape [1, 3, 16, 16, 16]
11711170
# for return_mask=True
1172-
x = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32, 32, 32]).astype(np.float32))
1171+
x = paddle.uniform([1, 3, 32, 32, 32])
11731172
output, max_indices = paddle.nn.functional.max_pool3d(x,
11741173
kernel_size = 2,
11751174
stride = 2,
11761175
padding=0,
11771176
return_mask=True)
1178-
# output.shape [None, 3, 16, 16, 16], max_indices.shape [None, 3, 16, 16, 16],
1177+
# output.shape [1, 3, 16, 16, 16], max_indices.shape [1, 3, 16, 16, 16]
11791178
"""
11801179
kernel_size = utils.convert_to_list(kernel_size, 3, 'pool_size')
11811180
if stride is None:
@@ -1267,10 +1266,9 @@ def adaptive_avg_pool1d(x, output_size, name=None):
12671266
Returns:
12681267
Tensor: The output tensor of adaptive average pooling result. The data type is same
12691268
as input tensor.
1270-
Raises:
1271-
ValueError: 'output_size' should be an integer.
12721269
Examples:
12731270
.. code-block:: python
1271+
:name: code-example1
12741272
12751273
# average adaptive pool1d
12761274
# suppose input data in shape of [N, C, L], `output_size` is m or [m],
@@ -1286,10 +1284,9 @@ def adaptive_avg_pool1d(x, output_size, name=None):
12861284
#
12871285
import paddle
12881286
import paddle.nn.functional as F
1289-
import numpy as np
12901287
1291-
data = paddle.to_tensor(np.random.uniform(-1, 1, [1, 3, 32]).astype(np.float32))
1292-
pool_out = F.adaptive_average_pool1d(data, output_size=16)
1288+
data = paddle.uniform([1, 3, 32])
1289+
pool_out = F.adaptive_avg_pool1d(data, output_size=16)
12931290
# pool_out shape: [1, 3, 16])
12941291
"""
12951292
pool_type = 'avg'

python/paddle/nn/layer/loss.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,18 @@ class HSigmoidLoss(Layer):
465465
import paddle
466466
paddle.set_device('cpu')
467467
468-
input = paddle.uniform([2, 3])
469-
# [[-0.2820413 0.9528898 -0.81638825] # random
470-
# [-0.6733154 -0.33866507 0.25770962]] # random
468+
input = paddle.uniform([4, 3])
469+
# [[0.56194401 -0.22450298 -0.10741806] # random
470+
# [0.36136317 0.23556745 0.88748658] # random
471+
# [0.18151939 0.80947340 -0.31078976] # random
472+
# [0.68886101 -0.14239830 -0.41297770]] # random
471473
label = paddle.to_tensor([0, 1, 4, 5])
472474
m = paddle.nn.HSigmoidLoss(3, 5)
473475
out = m(input, label)
474-
# [[2.4543471]
475-
# [1.9359267]]
476+
# [[2.42524505]
477+
# [1.74917245]
478+
# [3.14571381]
479+
# [2.34564662]]
476480
"""
477481

478482
def __init__(self,

python/paddle/nn/utils/spectral_norm_hook.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def spectral_norm(layer,
178178
.. code-block:: python
179179
180180
from paddle.nn import Conv2D
181-
from paddle.nn.utils import Spectralnorm
181+
from paddle.nn.utils import spectral_norm
182182
183183
conv = Conv2D(3, 1, 3)
184184
sn_conv = spectral_norm(conv)

python/paddle/nn/utils/weight_norm_hook.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,15 +213,21 @@ def remove_weight_norm(layer, name='weight'):
213213
Examples:
214214
.. code-block:: python
215215
216-
import paddle
217-
from paddle.nn import Conv2D
218-
from paddle.nn.utils import weight_norm, remove_weight_norm
219-
220-
conv = Conv2D(3, 5, 3)
221-
wn = weight_norm(conv)
222-
remove_weight_norm(conv)
223-
print(conv.weight_g)
224-
# AttributeError: 'Conv2D' object has no attribute 'weight_g'
216+
import paddle
217+
from paddle.nn import Conv2D
218+
from paddle.nn.utils import weight_norm, remove_weight_norm
219+
220+
conv = Conv2D(3, 5, 3)
221+
wn = weight_norm(conv)
222+
print(conv.weight_g)
223+
# Parameter containing:
224+
# Tensor(shape=[5], dtype=float32, place=Place(gpu:0), stop_gradient=False,
225+
# [0., 0., 0., 0., 0.])
226+
# Conv2D(3, 5, kernel_size=[3, 3], data_format=NCHW)
227+
228+
remove_weight_norm(conv)
229+
# print(conv.weight_g)
230+
# AttributeError: 'Conv2D' object has no attribute 'weight_g'
225231
"""
226232
for k, hook in layer._forward_pre_hooks.items():
227233
if isinstance(hook, WeightNorm) and hook.name == name:

python/paddle/vision/ops.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -851,15 +851,14 @@ def read_file(filename, name=None):
851851
import cv2
852852
import paddle
853853
854-
fake_img = (np.random.random(
855-
(400, 300, 3)) * 255).astype('uint8')
856-
854+
fake_img = (paddle.rand((400, 300, 3)).numpy() * 255).astype('uint8')
855+
857856
cv2.imwrite('fake.jpg', fake_img)
858857
859858
img_bytes = paddle.vision.ops.read_file('fake.jpg')
860859
861860
print(img_bytes.shape)
862-
861+
# [142915]
863862
"""
864863

865864
if _non_static_mode():

0 commit comments

Comments
 (0)