未验证 提交 49114ceb 编写于 作者: W Wilber 提交者: GitHub

modify less_than api (#1278)

* modify less_than api

* modify leaky_relu api

* modify log api

* modify logical_and api

* modify logical_or api

* modify logical_not api

* modify logical_xor api
上级 bf4869d8
...@@ -5,26 +5,37 @@ leaky_relu ...@@ -5,26 +5,37 @@ leaky_relu
.. py:function:: paddle.fluid.layers.leaky_relu(x, alpha=0.02, name=None) .. py:function:: paddle.fluid.layers.leaky_relu(x, alpha=0.02, name=None)
LeakyRelu 激活函数 LeakyRelu激活函数
.. math:: out=max(x,α∗x) .. math:: out=max(x,α∗x)
参数: 参数:
- **x** (Variable) - LeakyRelu Operator的输入 - **x** (Variable) - 输入的多维LoDTensor/Tensor,数据类型为:float32,float64。
- **alpha** (FLOAT|0.02) - 负斜率,值很小。 - **alpha** (float) - 负斜率,缺省值为0.02。
- **name** (str|None) - 此层的名称(可选)。如果设置为None,该层将被自动命名。 - **name** (str,可选) - 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` ,默认值为None。
返回: 与 ``x`` 维度相同,数据类型相同的LodTensor/Tensor。
返回类型: Variable
**代码示例:** **代码示例:**
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.layers.data(name="x", shape=[2,3,16,16], dtype="float32") import numpy as np
y = fluid.layers.leaky_relu(x, alpha=0.01)
# Graph Organizing
x = fluid.layers.data(name="x", shape=[2], dtype="float32")
res = fluid.layers.leaky_relu(x, alpha=0.1)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[-1, 2], [3, -4]]).astype(np.float32)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i}, fetch_list=[res])
print(res_val) # [[-0.1, 2], [3, -0.4]]
...@@ -6,26 +6,42 @@ less_than ...@@ -6,26 +6,42 @@ less_than
.. py:function:: paddle.fluid.layers.less_than(x, y, force_cpu=None, cond=None) .. py:function:: paddle.fluid.layers.less_than(x, y, force_cpu=None, cond=None)
该函数按元素出现顺序依次在X,Y上操作,并返回 ``Out`` ,它们三个都是n维tensor(张量)。 该OP逐元素地返回 :math:`x < y` 的逻辑值,使用重载算子 `<` 可以有相同的计算函数效果
其中,X、Y可以是任何类型的tensor,Out张量的各个元素可以通过 :math:`Out=X<Y` 计算得出。
参数: 参数:
- **x** (Variable) – ``less_than`` 运算的左操作数 - **x** (Variable) - 进行比较的第一个输入,是一个多维的LoDTensor/Tensor,数据类型可以是float32,float64,int32,int64。
- **y** (Variable) – ``less_than`` 运算的右操作数 - **y** (Variable) - 进行比较的第二个输入,是一个多维的LoDTensor/Tensor,数据类型可以是float32,float64,int32,int64。
- **force_cpu** (BOOLEAN) – 值True则强制将输出变量写入CPU内存中。否则,将其写入目前所在的运算设备上。默认为True - **force_cpu** (bool) – 如果为True则强制将输出变量写入CPU内存中,否则将其写入目前所在的运算设备上。默认值为False。注意:该属性已弃用,其值始终是False。
- **cond** (Variable|None) – 可选的用于存储 ``less_than`` 输出结果的变量,为None则由函数自动生成Out变量 - **cond** (Variable,可选) – 指定算子输出结果的LoDTensor/Tensor,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果。
返回: n维bool型tensor,其中各个元素可以通过 *Out=X<Y* 计算得出 返回:输出结果的LoDTensor/Tensor,数据的shape和输入x一致。
返回类型: Variable,数据类型为bool。
**代码示例**: **代码示例**:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
label = fluid.layers.data(name='y', shape=[1], dtype='int64') import numpy as np
limit = fluid.layers.fill_constant(shape=[1], dtype='int64', value=5)
cond = fluid.layers.less_than(x=label, y=limit) # Graph Organizing
x = fluid.layers.data(name='x', shape=[2], dtype='float64')
y = fluid.layers.data(name='y', shape=[2], dtype='float64')
result = fluid.layers.less_than(x=x, y=y)
# The comment lists another available method.
# result = fluid.layers.fill_constant(shape=[2], dtype='float64', value=0)
# fluid.layers.less_than(x=x, y=y, cond=result)
# Create an executor using CPU as example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1, 2], [3, 4]]).astype(np.float64)
y_i = np.array([[2, 2], [1, 3]]).astype(np.float64)
result_value, = exe.run(fluid.default_main_program(), feed={'x':x_i, 'y':y_i}, fetch_list=[result])
print(result_value) # [[True, False], [False, False]]
...@@ -6,19 +6,19 @@ log ...@@ -6,19 +6,19 @@ log
.. py:function:: paddle.fluid.layers.log(x, name=None) .. py:function:: paddle.fluid.layers.log(x, name=None)
给定输入张量,计算其每个元素的自然对数 Log激活函数(计算自然对数)
.. math:: .. math::
\\Out=ln(x)\\ \\Out=ln(x)\\
参数: 参数:
- **x** (Variable) – 输入张量 - **x** (Variable) – 该OP的输入为LodTensor/Tensor。数据类型为float32,float64。
- **name** (str|None, default None) – 该layer的名称,如果为None,自动命名 - **name** (str,可选) – 该参数供开发人员打印调试信息时使用,具体用法请参见 :ref:`api_guide_Name` ,默认值为None。
返回:给定输入张量计算自然对数 返回:Log算子自然对数输出
返回类型: 变量(variable) 返回类型: Variable - 该OP的输出为LodTensor/Tensor,数据类型为输入一致。
**代码示例** **代码示例**
...@@ -26,16 +26,18 @@ log ...@@ -26,16 +26,18 @@ log
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
x = fluid.layers.data(name="x", shape=[3, 4], dtype="float32") import numpy as np
output = fluid.layers.log(x)
# Graph Organizing
x = fluid.layers.data(name="x", shape=[1], dtype="float32")
res = fluid.layers.log(x)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1], [2]]).astype(np.float32)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i}, fetch_list=[res])
print(res_val) # [[0.], [0.6931472]]
...@@ -5,22 +5,20 @@ logical_and ...@@ -5,22 +5,20 @@ logical_and
.. py:function:: paddle.fluid.layers.logical_and(x, y, out=None, name=None) .. py:function:: paddle.fluid.layers.logical_and(x, y, out=None, name=None)
logical_and算子 该OP逐元素的对 ``X`` 和 ``Y`` 两LoDTensor/Tensor进行逻辑与运算。
它在 ``X`` 和 ``Y`` 上以元素方式操作,并返回 ``Out`` 。``X``、 ``Y`` 和 ``Out`` 是N维布尔张量(Tensor)。 ``Out`` 的每个元素的计算公式为:
.. math:: .. math::
Out = X \&\& Y Out = X \&\& Y
参数: 参数:
- **x** (Variable)- (LoDTensor)logical_and算子的左操作数 - **x** (Variable)- 逻辑与运算的第一个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **y** (Variable)- (LoDTensor)logical_and算子的右操作数 - **y** (Variable)- 逻辑与运算的第二个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **out** (Tensor)- 输出逻辑运算的张量。 - **out** (Variable,可选)- 指定算子输出结果的LoDTensor/Tensor,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果。
- **name** (basestring | None)- 输出的名称 - **name** (str,可选)- 该参数供开发人员打印调试信息时使用,具体用法参见 :ref:`api_guide_Name` ,默认值为None
返回: (LoDTensor)n-dim bool张量。每个元素的计算公式: :math:`Out = X \&\& Y` 返回:与 ``x`` 维度相同,数据类型相同的LoDTensor/Tensor。
返回类型: 输出(Variable)。 返回类型:Variable
**代码示例:** **代码示例:**
...@@ -28,17 +26,23 @@ logical_and算子 ...@@ -28,17 +26,23 @@ logical_and算子
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
left = fluid.layers.data( import numpy as np
name='left', shape=[1], dtype='bool')
right = fluid.layers.data( # Graph organizing
name='right', shape=[1], dtype='bool') x = fluid.layers.data(name='x', shape=[2], dtype='bool')
result = fluid.layers.logical_and(x=left, y=right) y = fluid.layers.data(name='y', shape=[2], dtype='bool')
res = fluid.layers.logical_and(x=x, y=y)
# The comment lists another available method.
# res = fluid.layers.fill_constant(shape=[2], dtype='bool', value=0)
# fluid.layers.logical_and(x=x, y=y, out=res)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1, 0], [0, 1]]).astype(np.bool)
y_i = np.array([[1, 1], [0, 0]]).astype(np.bool)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i, 'y':y_i}, fetch_list=[res])
print(res_val) # [[True, False], [False, False]]
...@@ -3,37 +3,42 @@ ...@@ -3,37 +3,42 @@
logical_not logical_not
------------------------------- -------------------------------
.. py:function:: paddle.fluid.layers.logical_not(x, out=None, name=None) .. py:function:: paddle.fluid.layers.logical_not(, out=None, name=None)
logical_not算子 该OP逐元素的对 ``X`` LoDTensor/Tensor进行逻辑非运算
它在 ``X`` 上以元素方式操作,并返回 ``Out`` 。 ``X`` 和 ``Out`` 是N维布尔张量(Tensor)。 ``Out`` 的每个元素的计算公式为:
.. math:: .. math::
Out = !X Out = !X
参数: 参数:
- **x** (Variable)- (LoDTensor)logical_not算子的操作数 - **x** (Variable)- 逻辑非运算的输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **out** (Tensor)- 输出逻辑运算的张量。 - **out** (Variable,可选)- 指定算子输出结果的LoDTensor/Tensor,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果。
- **name** (basestring | None)- 输出的名称。 - **name** (str,可选)- 该参数供开发人员打印调试信息时使用,具体用法参见 :ref:`api_guide_Name` ,默认值为None。
返回: (LoDTensor)n维布尔张量。
返回类型: 输出(Variable) 返回:与 ``x`` 维度相同,数据类型相同的LoDTensor/Tensor
返回类型:Variable
**代码示例:** **代码示例:**
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
left = fluid.layers.data( import numpy as np
name='left', shape=[1], dtype='bool')
result = fluid.layers.logical_not(x=left) # Graph organizing
x = fluid.layers.data(name='x', shape=[2], dtype='bool')
res = fluid.layers.logical_not(x)
# The comment lists another availble method.
# res = fluid.layers.fill_constant(shape=[2], dtype='bool', value=0)
# fluid.layers.logical_not(x, out=res)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1, 0]]).astype(np.bool)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i}, fetch_list=[res])
print(res_val) # [[False, True]]
...@@ -5,39 +5,44 @@ logical_or ...@@ -5,39 +5,44 @@ logical_or
.. py:function:: paddle.fluid.layers.logical_or(x, y, out=None, name=None) .. py:function:: paddle.fluid.layers.logical_or(x, y, out=None, name=None)
logical_or算子 该OP逐元素的对 ``X`` 和 ``Y`` 两LoDTensor/Tensor进行逻辑或运算。
它在 ``X`` 和 ``Y`` 上以元素方式操作,并返回 ``Out`` 。 ``X`` 、 ``Y`` 和 ``Out`` 是N维布尔张量(Tensor)。 ``Out`` 的每个元素的计算公式为:
.. math:: .. math::
Out = X || Y Out = X || Y
参数: 参数:
- **x** (Variable)- (LoDTensor)logical_or算子的左操作数 - **x** (Variable)- 逻辑或运算的第一个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **y** (Variable)- (LoDTensor)logical_or算子的右操作数 - **y** (Variable)- 逻辑或运算的第二个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **out** (Tensor)- 输出逻辑运算的张量。 - **out** (Variable,可选)- 指定算子输出结果的LoDTensor/Tensor,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果。
- **name** (basestring | None)- 输出的名称。 - **name** (str,可选)- 该参数供开发人员打印调试信息时使用,具体用法参见 :ref:`api_guide_Name` ,默认值为None。
返回: (LoDTensor)n维布尔张量。每个元素的计算公式: :math:`Out = X || Y`
返回类型: 输出(Variable) 返回:与 ``x`` 维度相同,数据类型相同的LoDTensor/Tensor
返回类型:Variable
**代码示例:** **代码示例:**
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
left = fluid.layers.data( import numpy as np
name='left', shape=[1], dtype='bool')
right = fluid.layers.data( # Graph organizing
name='right', shape=[1], dtype='bool') x = fluid.layers.data(name='x', shape=[2], dtype='bool')
result = fluid.layers.logical_or(x=left, y=right) y = fluid.layers.data(name='y', shape=[2], dtype='bool')
res = fluid.layers.logical_or(x=x, y=y)
# The comment lists another available method.
# res = fluid.layers.fill_constant(shape=[2], dtype='bool', value=0)
# fluid.layers.logical_or(x=x, y=y, out=res)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1, 0], [0, 1]]).astype(np.bool)
y_i = np.array([[1, 1], [0, 0]]).astype(np.bool)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i, 'y':y_i}, fetch_list=[res])
print(res_val) # [[True, True], [False, True]]
...@@ -5,23 +5,21 @@ logical_xor ...@@ -5,23 +5,21 @@ logical_xor
.. py:function:: paddle.fluid.layers.logical_xor(x, y, out=None, name=None) .. py:function:: paddle.fluid.layers.logical_xor(x, y, out=None, name=None)
logical_xor算子 该OP逐元素的对 ``X`` 和 ``Y`` 两LoDTensor/Tensor进行逻辑异或运算。
它在 ``X`` 和 ``Y`` 上以元素方式操作,并返回 ``Out`` 。 ``X`` 、 ``Y`` 和 ``Out`` 是N维布尔张量(Tensor)。 ``Out`` 的每个元素的计算公式为:
.. math:: .. math::
Out = (X || Y) \&\& !(X \&\& Y) Out = (X || Y) \&\& !(X \&\& Y)
参数: 参数:
- **x** (Variable)- (LoDTensor)logical_xor算子的左操作数 - **x** (Variable)- 逻辑异或运算的第一个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **y** (Variable)- (LoDTensor)logical_xor算子的右操作数 - **y** (Variable)- 逻辑异或运算的第二个输入,是一个多维的LoDTensor/Tensor,数据类型只能是bool。
- **out** (Tensor)- 输出逻辑运算的张量 - **out** (Variable,可选)- 指定算子输出结果的LoDTensor/Tensor,可以是程序中已经创建的任何Variable。默认值为None,此时将创建新的Variable来保存输出结果
- **name** (basestring | None)- 输出的名称 - **name** (str,可选)- 该参数供开发人员打印调试信息时使用,具体用法参见 :ref:`api_guide_Name` ,默认值为None
返回: (LoDTensor)n维布尔张量。
返回类型: 输出(Variable) 返回:与 ``x`` 维度相同,数据类型相同的LoDTensor/Tensor
返回类型:Variable
**代码示例:** **代码示例:**
...@@ -29,14 +27,23 @@ logical_xor算子 ...@@ -29,14 +27,23 @@ logical_xor算子
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle.fluid as fluid
left = fluid.layers.data( import numpy as np
name='left', shape=[1], dtype='bool')
right = fluid.layers.data( # Graph organizing
name='right', shape=[1], dtype='bool') x = fluid.layers.data(name='x', shape=[2], dtype='bool')
result = fluid.layers.logical_xor(x=left, y=right) y = fluid.layers.data(name='y', shape=[2], dtype='bool')
res = fluid.layers.logical_xor(x=x, y=y)
# The comment lists another available method.
# res = fluid.layers.fill_constant(shape=[2], dtype='bool', value=0)
# fluid.layers.logical_xor(x=x, y=y, out=res)
# Create an executor using CPU as an example
exe = fluid.Executor(fluid.CPUPlace())
exe.run(fluid.default_startup_program())
# Execute
x_i = np.array([[1, 0], [0, 1]]).astype(np.bool)
y_i = np.array([[1, 1], [0, 0]]).astype(np.bool)
res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i, 'y':y_i}, fetch_list=[res])
print(res_val) # [[False, True], [False, True]]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册