未验证 提交 f6834034 编写于 作者: L littletomatodonkey 提交者: GitHub

fix sample code (#28446)

上级 dc6b2321
...@@ -1226,26 +1226,23 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None): ...@@ -1226,26 +1226,23 @@ def pad(x, pad, mode='constant', value=0, data_format="NCHW", name=None):
Code Examples: Code Examples:
.. code-block:: python .. code-block:: python
import numpy as np import numpy as np
import paddle import paddle
import paddle.nn.functional as F import paddle.nn.functional as F
paddle.disable_static()
# example 1 # example 1
x_shape = (1, 1, 3) x_shape = (1, 1, 3)
x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1 x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
tensor_x = paddle.to_tensor(x) y = F.pad(x, [2, 3], value=1, mode='constant', data_format="NCL")
y = F.pad(tensor_x, pad=[2, 3], value=1, mode='constant') print(y)
print(y.numpy())
# [[[1. 1. 1. 2. 3. 1. 1. 1.]]] # [[[1. 1. 1. 2. 3. 1. 1. 1.]]]
# example 2 # example 2
x_shape = (1, 1, 2, 3) x_shape = (1, 1, 2, 3)
x = np.arange(np.prod(x_shape), dtype=np.float32).reshape(x_shape) + 1 x = paddle.arange(np.prod(x_shape), dtype="float32").reshape(x_shape) + 1
tensor_x = paddle.to_tensor(x) y = F.pad(x, [1, 2, 1, 1], value=1, mode='circular')
y = F.pad(tensor_x, pad=[1, 2, 1, 1], value=1, mode='circular') print(y)
print(y.numpy())
# [[[[6. 4. 5. 6. 4. 5.] # [[[[6. 4. 5. 6. 4. 5.]
# [3. 1. 2. 3. 1. 2.] # [3. 1. 2. 3. 1. 2.]
# [6. 4. 5. 6. 4. 5.] # [6. 4. 5. 6. 4. 5.]
...@@ -1361,6 +1358,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): ...@@ -1361,6 +1358,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8):
Examples: Examples:
.. code-block:: text .. code-block:: text
Case 0: Case 0:
x1 = [[0.8024077 0.9927354 0.27238318 0.8344984 ] x1 = [[0.8024077 0.9927354 0.27238318 0.8344984 ]
[0.48949873 0.5797396 0.65444374 0.66510963] [0.48949873 0.5797396 0.65444374 0.66510963]
...@@ -1376,10 +1374,10 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): ...@@ -1376,10 +1374,10 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8):
Code Examples: Code Examples:
.. code-block:: python .. code-block:: python
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np import numpy as np
paddle.disable_static()
np.random.seed(0) np.random.seed(0)
x1 = np.random.rand(2,3) x1 = np.random.rand(2,3)
...@@ -1387,7 +1385,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8): ...@@ -1387,7 +1385,7 @@ def cosine_similarity(x1, x2, axis=1, eps=1e-8):
x1 = paddle.to_tensor(x1) x1 = paddle.to_tensor(x1)
x2 = paddle.to_tensor(x2) x2 = paddle.to_tensor(x2)
result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0) result = paddle.nn.functional.cosine_similarity(x1, x2, axis=0)
print(result.numpy()) print(result)
# [0.99806249 0.9817672 0.94987036] # [0.99806249 0.9817672 0.94987036]
""" """
......
...@@ -744,7 +744,6 @@ class Pad1D(layers.Layer): ...@@ -744,7 +744,6 @@ class Pad1D(layers.Layer):
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np import numpy as np
paddle.disable_static()
input_shape = (1, 2, 3) input_shape = (1, 2, 3)
pad = [1, 2] pad = [1, 2]
...@@ -752,7 +751,7 @@ class Pad1D(layers.Layer): ...@@ -752,7 +751,7 @@ class Pad1D(layers.Layer):
data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1
my_pad = nn.Pad1D(padding=pad, mode=mode) my_pad = nn.Pad1D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result.numpy()) print(result)
# [[[0. 1. 2. 3. 0. 0.] # [[[0. 1. 2. 3. 0. 0.]
# [0. 4. 5. 6. 0. 0.]]] # [0. 4. 5. 6. 0. 0.]]]
""" """
...@@ -821,14 +820,13 @@ class Pad2D(layers.Layer): ...@@ -821,14 +820,13 @@ class Pad2D(layers.Layer):
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np import numpy as np
paddle.disable_static()
input_shape = (1, 1, 2, 3) input_shape = (1, 1, 2, 3)
pad = [1, 0, 1, 2] pad = [1, 0, 1, 2]
mode = "constant" mode = "constant"
data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1
my_pad = nn.Pad2D(padding=pad, mode=mode) my_pad = nn.Pad2D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result.numpy()) print(result)
# [[[[0. 0. 0. 0.] # [[[[0. 0. 0. 0.]
# [0. 1. 2. 3.] # [0. 1. 2. 3.]
# [0. 4. 5. 6.] # [0. 4. 5. 6.]
...@@ -906,7 +904,7 @@ class Pad3D(layers.Layer): ...@@ -906,7 +904,7 @@ class Pad3D(layers.Layer):
data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1 data = paddle.arange(np.prod(input_shape), dtype="float32").reshape(input_shape) + 1
my_pad = nn.Pad3D(padding=pad, mode=mode) my_pad = nn.Pad3D(padding=pad, mode=mode)
result = my_pad(data) result = my_pad(data)
print(result.numpy()) print(result)
# [[[[[0. 0. 0. 0.] # [[[[[0. 0. 0. 0.]
# [0. 1. 2. 3.] # [0. 1. 2. 3.]
# [0. 4. 5. 6.] # [0. 4. 5. 6.]
...@@ -968,7 +966,6 @@ class CosineSimilarity(layers.Layer): ...@@ -968,7 +966,6 @@ class CosineSimilarity(layers.Layer):
import paddle import paddle
import paddle.nn as nn import paddle.nn as nn
import numpy as np import numpy as np
paddle.disable_static()
np.random.seed(0) np.random.seed(0)
x1 = np.random.rand(2,3) x1 = np.random.rand(2,3)
...@@ -978,7 +975,7 @@ class CosineSimilarity(layers.Layer): ...@@ -978,7 +975,7 @@ class CosineSimilarity(layers.Layer):
cos_sim_func = nn.CosineSimilarity(axis=0) cos_sim_func = nn.CosineSimilarity(axis=0)
result = cos_sim_func(x1, x2) result = cos_sim_func(x1, x2)
print(result.numpy()) print(result)
# [0.99806249 0.9817672 0.94987036] # [0.99806249 0.9817672 0.94987036]
""" """
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册