From f6834034cc9ddd53c9395ee33c4d45b042c17c26 Mon Sep 17 00:00:00 2001 From: littletomatodonkey <2120160898@bit.edu.cn> Date: Thu, 5 Nov 2020 21:27:18 +0800 Subject: [PATCH] fix sample code (#28446) --- python/paddle/nn/functional/common.py | 24 +++++++++++------------- python/paddle/nn/layer/common.py | 11 ++++------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/python/paddle/nn/functional/common.py b/python/paddle/nn/functional/common.py index 0b18dec943d..1cf3599e846 100644 --- a/python/paddle/nn/functional/common.py +++ b/python/paddle/nn/functional/common.py @@ -1226,26 +1226,23 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): Code Examples: .. code-block:: python + import numpy as np import paddle import paddle.nn.functional as F - paddle.disable_static() - # example 1 x_shape = (1, 1, 3) - x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1 - tensor_x = paddle.to_tensor(x) - y = F.pad(tensor_x, pad=[2, 3], value=1, mode='constant') - print(y.numpy()) + x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL") + print(y) # [[[1. 1. 1. 2. 3. 1. 1. 1.]]] - + # example 2 x_shape = (1, 1, 2, 3) - x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1 - tensor_x = paddle.to_tensor(x) - y = F.pad(tensor_x, pad=[1, 2, 1, 1], value=1, mode='circular') - print(y.numpy()) + x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1 + y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular') + print(y) # [[[[6. 4. 5. 6. 4. 5.] # [3. 1. 2. 3. 1. 2.] # [6. 4. 5. 6. 4. 5.] @@ -1361,6 +1358,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): Examples: .. code-block:: text + Case 0: x1 = [[0.8024077 0.9927354 0.27238318 0.8344984 ] [0.48949873 0.5797396 0.65444374 0.66510963] @@ -1376,10 +1374,10 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): Code Examples: .. code-block:: python + import paddle import paddle.nn as nn import numpy as np - paddle.disable_static() np.random.seed(0) x1 = np.random.rand(2,3) @@ -1387,7 +1385,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): x1 = paddle.to_tensor(x1) x2 = paddle.to_tensor(x2) result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0) - print(result.numpy()) + print(result) # [0.99806249 0.9817672 0.94987036] """ diff --git a/python/paddle/nn/layer/common.py b/python/paddle/nn/layer/common.py index ad8263e4835..6e3910745e1 100644 --- a/python/paddle/nn/layer/common.py +++ b/python/paddle/nn/layer/common.py @@ -744,7 +744,6 @@ class Pad1D(layers.Layer): import paddle import paddle.nn as nn import numpy as np - paddle.disable_static() input_shape = (1, 2, 3) pad = [1, 2] @@ -752,7 +751,7 @@ class Pad1D(layers.Layer): data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad1D(padding=pad, mode=mode) result = my_pad(data) - print(result.numpy()) + print(result) # [[[0. 1. 2. 3. 0. 0.] # [0. 4. 5. 6. 0. 0.]]] """ @@ -821,14 +820,13 @@ class Pad2D(layers.Layer): import paddle import paddle.nn as nn import numpy as np - paddle.disable_static() input_shape = (1, 1, 2, 3) pad = [1, 0, 1, 2] mode = "constant" data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad2D(padding=pad, mode=mode) result = my_pad(data) - print(result.numpy()) + print(result) # [[[[0. 0. 0. 0.] # [0. 1. 2. 3.] # [0. 4. 5. 6.] @@ -906,7 +904,7 @@ class Pad3D(layers.Layer): data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 my_pad = nn.Pad3D(padding=pad, mode=mode) result = my_pad(data) - print(result.numpy()) + print(result) # [[[[[0. 0. 0. 0.] # [0. 1. 2. 3.] # [0. 4. 5. 6.] @@ -968,7 +966,6 @@ class CosineSimilarity(layers.Layer): import paddle import paddle.nn as nn import numpy as np - paddle.disable_static() np.random.seed(0) x1 = np.random.rand(2,3) @@ -978,7 +975,7 @@ class CosineSimilarity(layers.Layer): cos_sim_func = nn.CosineSimilarity(axis=0) result = cos_sim_func(x1, x2) - print(result.numpy()) + print(result) # [0.99806249 0.9817672 0.94987036] """ -- GitLab