Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
2bcb7c0a
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
2bcb7c0a
编写于
10月 12, 2020
作者:
J
joejiong
提交者:
GitHub
10月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Mutiply allows non-tensor data input (#27690)
Mutiply allows non-tensor data input
上级
55e63763
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
87 addition
and
0 deletion
+87
-0
python/paddle/fluid/tests/unittests/test_multiply.py
python/paddle/fluid/tests/unittests/test_multiply.py
+75
-0
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+12
-0
未找到文件。
python/paddle/fluid/tests/unittests/test_multiply.py
浏览文件 @
2bcb7c0a
...
...
@@ -26,6 +26,7 @@ class TestMultiplyAPI(unittest.TestCase):
def
__run_static_graph_case
(
self
,
x_data
,
y_data
,
axis
=-
1
):
with
program_guard
(
Program
(),
Program
()):
paddle
.
enable_static
()
x
=
paddle
.
static
.
data
(
name
=
'x'
,
shape
=
x_data
.
shape
,
dtype
=
x_data
.
dtype
)
y
=
paddle
.
static
.
data
(
...
...
@@ -42,6 +43,21 @@ class TestMultiplyAPI(unittest.TestCase):
res
=
outs
[
0
]
return
res
def
__run_static_graph_case_with_numpy_input
(
self
,
x_data
,
y_data
,
axis
=-
1
):
with
program_guard
(
Program
(),
Program
()):
paddle
.
enable_static
()
res
=
tensor
.
multiply
(
x_data
,
y_data
,
axis
=
axis
)
place
=
fluid
.
CUDAPlace
(
0
)
if
fluid
.
core
.
is_compiled_with_cuda
(
)
else
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
outs
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
{
'x'
:
x_data
,
'y'
:
y_data
},
fetch_list
=
[
res
])
res
=
outs
[
0
]
return
res
def
__run_dynamic_graph_case
(
self
,
x_data
,
y_data
,
axis
=-
1
):
paddle
.
disable_static
()
x
=
paddle
.
to_tensor
(
x_data
)
...
...
@@ -49,27 +65,52 @@ class TestMultiplyAPI(unittest.TestCase):
res
=
paddle
.
multiply
(
x
,
y
,
axis
=
axis
)
return
res
.
numpy
()
def
__run_dynamic_graph_case_with_numpy_input
(
self
,
x_data
,
y_data
,
axis
=-
1
):
paddle
.
disable_static
()
res
=
paddle
.
multiply
(
x_data
,
y_data
,
axis
=
axis
)
return
res
.
numpy
()
def
test_multiply
(
self
):
"""test_multiply."""
np
.
random
.
seed
(
7
)
# test static computation graph: 1-d array
x_data
=
np
.
random
.
rand
(
200
)
y_data
=
np
.
random
.
rand
(
200
)
res
=
self
.
__run_static_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph: 1-d array
x_data
=
np
.
random
.
rand
(
200
)
y_data
=
np
.
random
.
rand
(
200
)
res
=
self
.
__run_static_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph: 2-d array
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
2
,
500
)
res
=
self
.
__run_static_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph with_primitives: 2-d array
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
2
,
500
)
res
=
self
.
__run_static_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph: broadcast
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
500
)
res
=
self
.
__run_static_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph with_primitives: broadcast
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
500
)
res
=
self
.
__run_static_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test static computation graph: broadcast with axis
x_data
=
np
.
random
.
rand
(
2
,
300
,
40
)
y_data
=
np
.
random
.
rand
(
300
)
...
...
@@ -77,24 +118,50 @@ class TestMultiplyAPI(unittest.TestCase):
expected
=
np
.
multiply
(
x_data
,
y_data
[...,
np
.
newaxis
])
self
.
assertTrue
(
np
.
allclose
(
res
,
expected
))
# test static computation graph with_primitives: broadcast with axis
x_data
=
np
.
random
.
rand
(
2
,
300
,
40
)
y_data
=
np
.
random
.
rand
(
300
)
res
=
self
.
__run_static_graph_case_with_numpy_input
(
x_data
,
y_data
,
axis
=
1
)
expected
=
np
.
multiply
(
x_data
,
y_data
[...,
np
.
newaxis
])
self
.
assertTrue
(
np
.
allclose
(
res
,
expected
))
# test dynamic computation graph: 1-d array
x_data
=
np
.
random
.
rand
(
200
)
y_data
=
np
.
random
.
rand
(
200
)
res
=
self
.
__run_dynamic_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic numpy input computation graph: 1-d array
x_data
=
np
.
random
.
rand
(
200
)
y_data
=
np
.
random
.
rand
(
200
)
res
=
self
.
__run_dynamic_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic computation graph: 2-d array
x_data
=
np
.
random
.
rand
(
20
,
50
)
y_data
=
np
.
random
.
rand
(
20
,
50
)
res
=
self
.
__run_dynamic_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic numpy input computation graph: 1-d array
x_data
=
np
.
random
.
rand
(
20
,
50
)
y_data
=
np
.
random
.
rand
(
20
,
50
)
res
=
self
.
__run_dynamic_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic computation graph: broadcast
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
500
)
res
=
self
.
__run_dynamic_graph_case
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic computation graph with numpy tensor: broadcast
x_data
=
np
.
random
.
rand
(
2
,
500
)
y_data
=
np
.
random
.
rand
(
500
)
res
=
self
.
__run_dynamic_graph_case_with_numpy_input
(
x_data
,
y_data
)
self
.
assertTrue
(
np
.
allclose
(
res
,
np
.
multiply
(
x_data
,
y_data
)))
# test dynamic computation graph: broadcast with axis
x_data
=
np
.
random
.
rand
(
2
,
300
,
40
)
y_data
=
np
.
random
.
rand
(
300
)
...
...
@@ -102,6 +169,14 @@ class TestMultiplyAPI(unittest.TestCase):
expected
=
np
.
multiply
(
x_data
,
y_data
[...,
np
.
newaxis
])
self
.
assertTrue
(
np
.
allclose
(
res
,
expected
))
# test dynamic computation graph with numpy tensor: broadcast with axis
x_data
=
np
.
random
.
rand
(
2
,
300
,
40
)
y_data
=
np
.
random
.
rand
(
300
)
res
=
self
.
__run_dynamic_graph_case_with_numpy_input
(
x_data
,
y_data
,
axis
=
1
)
expected
=
np
.
multiply
(
x_data
,
y_data
[...,
np
.
newaxis
])
self
.
assertTrue
(
np
.
allclose
(
res
,
expected
))
class
TestMultiplyError
(
unittest
.
TestCase
):
"""TestMultiplyError."""
...
...
python/paddle/tensor/math.py
浏览文件 @
2bcb7c0a
...
...
@@ -472,15 +472,27 @@ def multiply(x, y, axis=-1, name=None):
"""
op_type
=
'elementwise_mul'
act
=
None
if
x
.
dtype
!=
y
.
dtype
:
raise
TypeError
(
'Input tensors must be same type, but received type of x: %s, type of y: %s '
%
(
x
.
dtype
,
y
.
dtype
))
if
in_dygraph_mode
():
if
not
isinstance
(
x
,
(
paddle
.
Tensor
)):
x
=
paddle
.
to_tensor
(
x
)
if
not
isinstance
(
y
,
(
paddle
.
Tensor
)):
y
=
paddle
.
to_tensor
(
y
)
return
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=
axis
,
act
=
act
,
op_name
=
op_type
)
if
not
isinstance
(
x
,
(
paddle
.
Tensor
,
Variable
)):
x
=
paddle
.
static
.
data
(
name
=
'x'
,
shape
=
x
.
shape
,
dtype
=
x
.
dtype
)
if
not
isinstance
(
y
,
(
paddle
.
Tensor
,
Variable
)):
y
=
paddle
.
static
.
data
(
name
=
'y'
,
shape
=
y
.
shape
,
dtype
=
y
.
dtype
)
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
def
maximum
(
x
,
y
,
axis
=-
1
,
name
=
None
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录