Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
45711dad
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
45711dad
编写于
8月 22, 2020
作者:
W
WangXi
提交者:
GitHub
8月 22, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【API】rename div to divide, add floor_divide, remainder (#26434)
上级
f5d13498
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
175 addition
and
86 deletion
+175
-86
paddle/fluid/operators/elementwise/elementwise_mod_op.cc
paddle/fluid/operators/elementwise/elementwise_mod_op.cc
+4
-4
python/paddle/__init__.py
python/paddle/__init__.py
+5
-1
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+3
-0
python/paddle/fluid/tests/unittests/test_elementwise_div_op.py
...n/paddle/fluid/tests/unittests/test_elementwise_div_op.py
+5
-5
python/paddle/fluid/tests/unittests/test_elementwise_floordiv_op.py
...dle/fluid/tests/unittests/test_elementwise_floordiv_op.py
+23
-0
python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
...n/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
+23
-0
python/paddle/tensor/__init__.py
python/paddle/tensor/__init__.py
+5
-1
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+107
-75
未找到文件。
paddle/fluid/operators/elementwise/elementwise_mod_op.cc
浏览文件 @
45711dad
...
@@ -25,14 +25,14 @@ class ElementwiseModOpMaker : public ElementwiseOpMaker {
...
@@ -25,14 +25,14 @@ class ElementwiseModOpMaker : public ElementwiseOpMaker {
void
AddInputX
()
override
{
void
AddInputX
()
override
{
AddInput
(
"X"
,
AddInput
(
"X"
,
"(
Variable), Tensor or LoD
Tensor of any dimensions. Its dtype "
"(
Tensor),
Tensor of any dimensions. Its dtype "
"should be int32, int64."
);
"should be int32, int64
, float32 or float64
."
);
}
}
void
AddInputY
()
override
{
void
AddInputY
()
override
{
AddInput
(
"Y"
,
AddInput
(
"Y"
,
"(
Variable), Tensor or LoD
Tensor of any dimensions. Its dtype "
"(
Tensor),
Tensor of any dimensions. Its dtype "
"should be int32, int64."
);
"should be int32, int64
, float32 or float64
."
);
}
}
std
::
string
GetOpFuntionality
()
const
override
{
std
::
string
GetOpFuntionality
()
const
override
{
...
...
python/paddle/__init__.py
浏览文件 @
45711dad
...
@@ -176,7 +176,11 @@ from .tensor.math import maximum #DEFINE_ALIAS
...
@@ -176,7 +176,11 @@ from .tensor.math import maximum #DEFINE_ALIAS
from
.tensor.math
import
min
#DEFINE_ALIAS
from
.tensor.math
import
min
#DEFINE_ALIAS
from
.tensor.math
import
minimum
#DEFINE_ALIAS
from
.tensor.math
import
minimum
#DEFINE_ALIAS
from
.tensor.math
import
mm
#DEFINE_ALIAS
from
.tensor.math
import
mm
#DEFINE_ALIAS
from
.tensor.math
import
div
#DEFINE_ALIAS
from
.tensor.math
import
divide
#DEFINE_ALIAS
from
.tensor.math
import
floor_divide
#DEFINE_ALIAS
from
.tensor.math
import
remainder
#DEFINE_ALIAS
from
.tensor.math
import
mod
#DEFINE_ALIAS
from
.tensor.math
import
floor_mod
#DEFINE_ALIAS
from
.tensor.math
import
multiply
#DEFINE_ALIAS
from
.tensor.math
import
multiply
#DEFINE_ALIAS
from
.tensor.math
import
add
#DEFINE_ALIAS
from
.tensor.math
import
add
#DEFINE_ALIAS
from
.tensor.math
import
atan
#DEFINE_ALIAS
from
.tensor.math
import
atan
#DEFINE_ALIAS
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
45711dad
...
@@ -11484,6 +11484,7 @@ Examples:
...
@@ -11484,6 +11484,7 @@ Examples:
return _elementwise_op(LayerHelper('elementwise_add', **locals()))
return _elementwise_op(LayerHelper('elementwise_add', **locals()))
@deprecated(since="2.0.0", update_to="paddle.divide")
def elementwise_div(x, y, axis=-1, act=None, name=None):
def elementwise_div(x, y, axis=-1, act=None, name=None):
"""
"""
:alias_main: paddle.elementwise_div
:alias_main: paddle.elementwise_div
...
@@ -11907,6 +11908,7 @@ Examples:
...
@@ -11907,6 +11908,7 @@ Examples:
return _elementwise_op(LayerHelper('elementwise_pow', **locals()))
return _elementwise_op(LayerHelper('elementwise_pow', **locals()))
@deprecated(since="2.0.0", update_to="paddle.remainder")
def elementwise_mod(x, y, axis=-1, act=None, name=None):
def elementwise_mod(x, y, axis=-1, act=None, name=None):
"""
"""
:alias_main: paddle.elementwise_mod
:alias_main: paddle.elementwise_mod
...
@@ -11944,6 +11946,7 @@ Examples:
...
@@ -11944,6 +11946,7 @@ Examples:
return _elementwise_op(LayerHelper('elementwise_mod', **locals()))
return _elementwise_op(LayerHelper('elementwise_mod', **locals()))
@deprecated(since="2.0.0", update_to="paddle.floor_divide")
def elementwise_floordiv(x, y, axis=-1, act=None, name=None):
def elementwise_floordiv(x, y, axis=-1, act=None, name=None):
"""
"""
:alias_main: paddle.elementwise_floordiv
:alias_main: paddle.elementwise_floordiv
...
...
python/paddle/fluid/tests/unittests/test_elementwise_div_op.py
浏览文件 @
45711dad
...
@@ -240,22 +240,22 @@ class TestElementwiseDivBroadcast(unittest.TestCase):
...
@@ -240,22 +240,22 @@ class TestElementwiseDivBroadcast(unittest.TestCase):
self
.
assertEqual
((
out_result
==
(
2
/
x
)).
all
(),
True
)
self
.
assertEqual
((
out_result
==
(
2
/
x
)).
all
(),
True
)
class
TestDivOp
(
unittest
.
TestCase
):
class
TestDiv
ide
Op
(
unittest
.
TestCase
):
def
test_name
(
self
):
def
test_name
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
()):
with
fluid
.
program_guard
(
fluid
.
Program
()):
x
=
fluid
.
data
(
name
=
"x"
,
shape
=
[
2
,
3
],
dtype
=
"float32"
)
x
=
fluid
.
data
(
name
=
"x"
,
shape
=
[
2
,
3
],
dtype
=
"float32"
)
y
=
fluid
.
data
(
name
=
'y'
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
y
=
fluid
.
data
(
name
=
'y'
,
shape
=
[
2
,
3
],
dtype
=
'float32'
)
y_1
=
paddle
.
div
(
x
,
y
,
name
=
'div_res'
)
y_1
=
paddle
.
div
ide
(
x
,
y
,
name
=
'div_res'
)
self
.
assertEqual
((
'div_res'
in
y_1
.
name
),
True
)
self
.
assertEqual
((
'div_res'
in
y_1
.
name
),
True
)
def
test_dygraph
(
self
):
def
test_dygraph
(
self
):
with
fluid
.
dygraph
.
guard
():
with
fluid
.
dygraph
.
guard
():
np_x
=
np
.
array
([
2
,
3
,
4
]).
astype
(
'float64'
)
np_x
=
np
.
array
([
2
,
3
,
4
]).
astype
(
'float64'
)
np_y
=
np
.
array
([
1
,
5
,
2
]).
astype
(
'float64'
)
np_y
=
np
.
array
([
1
,
5
,
2
]).
astype
(
'float64'
)
x
=
fluid
.
dygraph
.
to_variable
(
np_x
)
x
=
paddle
.
to_tensor
(
np_x
)
y
=
fluid
.
dygraph
.
to_variable
(
np_y
)
y
=
paddle
.
to_tensor
(
np_y
)
z
=
paddle
.
div
(
x
,
y
)
z
=
paddle
.
div
ide
(
x
,
y
)
np_z
=
z
.
numpy
()
np_z
=
z
.
numpy
()
z_expected
=
np
.
array
([
2.
,
0.6
,
2.
])
z_expected
=
np
.
array
([
2.
,
0.6
,
2.
])
self
.
assertEqual
((
np_z
==
z_expected
).
all
(),
True
)
self
.
assertEqual
((
np_z
==
z_expected
).
all
(),
True
)
...
...
python/paddle/fluid/tests/unittests/test_elementwise_floordiv_op.py
浏览文件 @
45711dad
...
@@ -15,6 +15,8 @@
...
@@ -15,6 +15,8 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
op_test
import
OpTest
from
op_test
import
OpTest
...
@@ -65,5 +67,26 @@ class TestElementwiseModOp_scalar(TestElementwiseModOp):
...
@@ -65,5 +67,26 @@ class TestElementwiseModOp_scalar(TestElementwiseModOp):
self
.
out
=
np
.
floor_divide
(
self
.
x
,
self
.
y
)
self
.
out
=
np
.
floor_divide
(
self
.
x
,
self
.
y
)
class
TestFloorDivideOp
(
unittest
.
TestCase
):
def
test_name
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
()):
x
=
fluid
.
data
(
name
=
"x"
,
shape
=
[
2
,
3
],
dtype
=
"int64"
)
y
=
fluid
.
data
(
name
=
'y'
,
shape
=
[
2
,
3
],
dtype
=
'int64'
)
y_1
=
paddle
.
floor_divide
(
x
,
y
,
name
=
'div_res'
)
self
.
assertEqual
((
'div_res'
in
y_1
.
name
),
True
)
def
test_dygraph
(
self
):
with
fluid
.
dygraph
.
guard
():
np_x
=
np
.
array
([
2
,
3
,
8
,
7
]).
astype
(
'int64'
)
np_y
=
np
.
array
([
1
,
5
,
3
,
3
]).
astype
(
'int64'
)
x
=
paddle
.
to_tensor
(
np_x
)
y
=
paddle
.
to_tensor
(
np_y
)
z
=
paddle
.
floor_divide
(
x
,
y
)
np_z
=
z
.
numpy
()
z_expected
=
np
.
array
([
2
,
0
,
2
,
2
])
self
.
assertEqual
((
np_z
==
z_expected
).
all
(),
True
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py
浏览文件 @
45711dad
...
@@ -15,6 +15,8 @@
...
@@ -15,6 +15,8 @@
from
__future__
import
print_function
from
__future__
import
print_function
import
unittest
import
unittest
import
numpy
as
np
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
import
paddle.fluid.core
as
core
from
op_test
import
OpTest
from
op_test
import
OpTest
...
@@ -82,5 +84,26 @@ class TestElementwiseModOpDouble(TestElementwiseModOpFloat):
...
@@ -82,5 +84,26 @@ class TestElementwiseModOpDouble(TestElementwiseModOpFloat):
self
.
dtype
=
np
.
float64
self
.
dtype
=
np
.
float64
class
TestRemainderOp
(
unittest
.
TestCase
):
def
test_name
(
self
):
with
fluid
.
program_guard
(
fluid
.
Program
()):
x
=
fluid
.
data
(
name
=
"x"
,
shape
=
[
2
,
3
],
dtype
=
"int64"
)
y
=
fluid
.
data
(
name
=
'y'
,
shape
=
[
2
,
3
],
dtype
=
'int64'
)
y_1
=
paddle
.
remainder
(
x
,
y
,
name
=
'div_res'
)
self
.
assertEqual
((
'div_res'
in
y_1
.
name
),
True
)
def
test_dygraph
(
self
):
with
fluid
.
dygraph
.
guard
():
np_x
=
np
.
array
([
2
,
3
,
8
,
7
]).
astype
(
'int64'
)
np_y
=
np
.
array
([
1
,
5
,
3
,
3
]).
astype
(
'int64'
)
x
=
paddle
.
to_tensor
(
np_x
)
y
=
paddle
.
to_tensor
(
np_y
)
z
=
paddle
.
remainder
(
x
,
y
)
np_z
=
z
.
numpy
()
z_expected
=
np
.
array
([
0
,
3
,
2
,
1
])
self
.
assertEqual
((
np_z
==
z_expected
).
all
(),
True
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
python/paddle/tensor/__init__.py
浏览文件 @
45711dad
...
@@ -144,7 +144,11 @@ from .math import maximum #DEFINE_ALIAS
...
@@ -144,7 +144,11 @@ from .math import maximum #DEFINE_ALIAS
from
.math
import
min
#DEFINE_ALIAS
from
.math
import
min
#DEFINE_ALIAS
from
.math
import
minimum
#DEFINE_ALIAS
from
.math
import
minimum
#DEFINE_ALIAS
from
.math
import
mm
#DEFINE_ALIAS
from
.math
import
mm
#DEFINE_ALIAS
from
.math
import
div
#DEFINE_ALIAS
from
.math
import
divide
#DEFINE_ALIAS
from
.math
import
floor_divide
#DEFINE_ALIAS
from
.math
import
remainder
#DEFINE_ALIAS
from
.math
import
mod
#DEFINE_ALIAS
from
.math
import
floor_mod
#DEFINE_ALIAS
from
.math
import
multiply
#DEFINE_ALIAS
from
.math
import
multiply
#DEFINE_ALIAS
from
.math
import
add
#DEFINE_ALIAS
from
.math
import
add
#DEFINE_ALIAS
from
.math
import
atan
#DEFINE_ALIAS
from
.math
import
atan
#DEFINE_ALIAS
...
...
python/paddle/tensor/math.py
浏览文件 @
45711dad
...
@@ -111,7 +111,11 @@ __all__ = [
...
@@ -111,7 +111,11 @@ __all__ = [
'min'
,
'min'
,
'minimum'
,
'minimum'
,
'mm'
,
'mm'
,
'div'
,
'divide'
,
'floor_divide'
,
'remainder'
,
'mod'
,
'floor_mod'
,
'multiply'
,
'multiply'
,
'add'
,
'add'
,
'atan'
,
'atan'
,
...
@@ -263,114 +267,143 @@ Examples:
...
@@ -263,114 +267,143 @@ Examples:
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
def
div
(
x
,
y
,
name
=
None
):
def
div
ide
(
x
,
y
,
name
=
None
):
"""
"""
Example
s:
Divide two tensors element-wise. The equation i
s:
.. code-block:: python
.. math::
out = x / y
import paddle
**Note**:
import paddle.fluid as fluid
``paddle.divide`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting` .
import numpy as np
def gen_data():
Args:
return {
x (Tensor): the input tensor, it's data type should be float32, float64, int32, int64.
"x": np.array([2, 3, 4]).astype('float32'),
y (Tensor): the input tensor, it's data type should be float32, float64, int32, int64.
"y": np.array([1, 5, 2]).astype('float32')
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
}
x = fluid.data(name="x", shape=[3], dtype='float32')
Returns:
y = fluid.data(name="y", shape=[3], dtype='float32')
N-D Tensor. A location into which the result is stored. It's dimension equals with $x$.
z = paddle.div(x, y)
# z = x / y
place = fluid.CPUPlace()
Examples:
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
fetch_list=[z.name])
print(z_value) # [2., 0.6, 2.]
.. code-block:: python
import paddle
import numpy as np
.. code-block:: python
paddle.disable_static()
import paddle
np_x = np.array([2, 3, 4]).astype('float64')
import paddle.fluid as fluid
np_y = np.array([1, 5, 2]).astype('float64')
import numpy as np
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.divide(x, y)
print(z.numpy()) # [2., 0.6, 2.]
def gen_data():
"""
return {
op_type
=
'elementwise_div'
"x": np.ones((2, 3, 4, 5)).astype('float32'),
axis
=
-
1
"y": np.zeros((4, 5)).astype('float32')
act
=
None
}
if
in_dygraph_mode
():
return
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=
axis
,
act
=
act
,
op_name
=
op_type
)
x = fluid.data(name="x", shape=[2, 3, 4, 5], dtype='float32')
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
y = fluid.data(name="y", shape=[4, 5], dtype='float32')
z = paddle.div(x, y, name='z')
# z = x / y
place = fluid.CPUPlace()
exe = fluid.Executor(place)
z_value = exe.run(feed=gen_data(),
def
floor_divide
(
x
,
y
,
name
=
None
):
fetch_list=[z.name])
"""
Floor divide two tensors element-wise. The equation is:
print(z_value[0])
.. math::
print(z_value[0].shape) # z.shape=[2,3,4,5]
out = x // y
**Note**:
``paddle.floor_divide`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting` .
.. code-block:: python
Args:
x (Tensor): the input tensor, it's data type should be int32, int64.
y (Tensor): the input tensor, it's data type should be int32, int64.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
import paddle
Returns:
import paddle.fluid as fluid
N-D Tensor. A location into which the result is stored. It's dimension equals with $x$.
import numpy as np
def gen_data():
Examples:
return {
"x": np.random.randint(1, 5, size=[2, 3, 4, 5]).astype('float32'),
"y": np.random.randint(1, 5, size=[5]).astype('float32')
}
x = fluid.data(name="x", shape=[2,3,4,5], dtype='float32')
.. code-block:: python
y = fluid.data(name="y", shape=[5], dtype='float32')
z = paddle.div(x, y)
# z = x / y
place = fluid.CPUPlace()
import paddle
exe = fluid.Executor(place)
import numpy as np
z_value = exe.run(feed=gen_data(),
paddle.disable_static()
fetch_list=[z.name])
print(z_value[0])
print(z_value[0].shape) # z.shape=[2,3,4,5]
np_x = np.array([2, 3, 8, 7])
np_y = np.array([1, 5, 3, 3])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.floor_divide(x, y)
print(z.numpy()) # [2, 0, 2, 2]
.. code-block:: python
"""
op_type
=
'elementwise_floordiv'
axis
=
-
1
if
in_dygraph_mode
():
return
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=
axis
,
op_name
=
op_type
)
import paddle
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
import paddle.fluid as fluid
import numpy as np
with fluid.dygraph.guard(fluid.CPUPlace()):
np_x = np.array([2, 3, 4]).astype('float64')
np_y = np.array([1, 5, 2]).astype('float64')
x = fluid.dygraph.to_variable(np_x)
y = fluid.dygraph.to_variable(np_y)
z = paddle.div(x, y)
np_z = z.numpy()
print(np_z) # [2., 0.6, 2.]
def
remainder
(
x
,
y
,
name
=
None
):
"""
"""
op_type
=
'elementwise_div'
Mod two tensors element-wise. The equation is:
.. math::
out = x \% y
**Note**:
``paddle.remainder`` supports broadcasting. If you want know more about broadcasting, please refer to :ref:`user_guide_broadcasting` .
Args:
x (Tensor): the input tensor, it's data type should be int32, int64.
y (Tensor): the input tensor, it's data type should be int32, int64.
name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
Returns:
N-D Tensor. A location into which the result is stored. It's dimension equals with $x$.
Examples:
.. code-block:: python
import paddle
import numpy as np
paddle.disable_static()
np_x = np.array([2, 3, 8, 7])
np_y = np.array([1, 5, 3, 3])
x = paddle.to_tensor(np_x)
y = paddle.to_tensor(np_y)
z = paddle.remainder(x, y)
print(z.numpy()) # [0, 3, 2, 1]
"""
op_type
=
'elementwise_mod'
axis
=
-
1
axis
=
-
1
act
=
None
if
in_dygraph_mode
():
if
in_dygraph_mode
():
return
_elementwise_op_in_dygraph
(
return
_elementwise_op_in_dygraph
(
x
,
y
,
axis
=
axis
,
act
=
act
,
op_name
=
op_type
)
x
,
y
,
axis
=
axis
,
op_name
=
op_type
)
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
return
_elementwise_op
(
LayerHelper
(
op_type
,
**
locals
()))
mod
=
remainder
#DEFINE_ALIAS
floor_mod
=
remainder
#DEFINE_ALIAS
def
multiply
(
x
,
y
,
axis
=-
1
,
name
=
None
):
def
multiply
(
x
,
y
,
axis
=-
1
,
name
=
None
):
"""
"""
:alias_main: paddle.multiply
:alias_main: paddle.multiply
...
@@ -512,7 +545,6 @@ Examples:
...
@@ -512,7 +545,6 @@ Examples:
for
func
in
[
for
func
in
[
add
,
add
,
div
,
maximum
,
maximum
,
minimum
,
minimum
,
multiply
multiply
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录