Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
liming1019
Paddle
提交
f6834034
P
Paddle
项目概览
liming1019
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f6834034
编写于
11月 05, 2020
作者:
L
littletomatodonkey
提交者:
GitHub
11月 05, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix sample code (#28446)
上级
dc6b2321
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
15 addition
and
20 deletion
+15
-20
python/paddle/nn/functional/common.py
python/paddle/nn/functional/common.py
+11
-13
python/paddle/nn/layer/common.py
python/paddle/nn/layer/common.py
+4
-7
未找到文件。
python/paddle/nn/functional/common.py
浏览文件 @
f6834034
...
@@ -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]
"""
"""
...
...
python/paddle/nn/layer/common.py
浏览文件 @
f6834034
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录