提交 c941c361 编写于 作者: L LiuChiaChi

Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into...

Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into remove-input-requirment-in-dygraph-CModel
...@@ -2488,9 +2488,6 @@ def _error_message(what, arg_name, op_name, right_value, error_value): ...@@ -2488,9 +2488,6 @@ def _error_message(what, arg_name, op_name, right_value, error_value):
def case(pred_fn_pairs, default=None, name=None): def case(pred_fn_pairs, default=None, name=None):
''' '''
:api_attr: Static Graph :api_attr: Static Graph
:alias_main: paddle.nn.case
:alias: paddle.nn.case,paddle.nn.control_flow.case
:old_api: paddle.fluid.layers.case
This operator works like an if-elif-elif-else chain. This operator works like an if-elif-elif-else chain.
...@@ -2500,7 +2497,7 @@ def case(pred_fn_pairs, default=None, name=None): ...@@ -2500,7 +2497,7 @@ def case(pred_fn_pairs, default=None, name=None):
name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`. name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns: Returns:
Variable|list(Variable): Tensors returned by the callable from the first pair whose pred is True, Tensor|list(Tensor): Tensors returned by the callable from the first pair whose pred is True,
or Tensors returned by ``default`` if no pred in ``pred_fn_pairs`` is True and ``default`` is not None, or Tensors returned by ``default`` if no pred in ``pred_fn_pairs`` is True and ``default`` is not None,
or Tensors returned by the last callable in ``pred_fn_pairs`` if no pred in ``pred_fn_pairs`` is True and ``default`` is None. or Tensors returned by the last callable in ``pred_fn_pairs`` if no pred in ``pred_fn_pairs`` is True and ``default`` is None.
...@@ -2508,45 +2505,47 @@ def case(pred_fn_pairs, default=None, name=None): ...@@ -2508,45 +2505,47 @@ def case(pred_fn_pairs, default=None, name=None):
TypeError: If the type of ``pred_fn_pairs`` is not list or tuple. TypeError: If the type of ``pred_fn_pairs`` is not list or tuple.
TypeError: If the type of elements in ``pred_fn_pairs`` is not tuple. TypeError: If the type of elements in ``pred_fn_pairs`` is not tuple.
TypeError: If the size of tuples in ``pred_fn_pairs`` is not 2. TypeError: If the size of tuples in ``pred_fn_pairs`` is not 2.
TypeError: If the first element of 2-tuple in ``pred_fn_pairs`` is not Variable. TypeError: If the first element of 2-tuple in ``pred_fn_pairs`` is not a Tensor.
TypeError: If the second element of 2-tuple in ``pred_fn_pairs`` is not callable. TypeError: If the second element of 2-tuple in ``pred_fn_pairs`` is not callable.
TypeError: If ``default`` is not None but it is not callable. TypeError: If ``default`` is not None but it is not callable.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
import paddle.fluid.layers as layers
paddle.enable_static()
def fn_1(): def fn_1():
return layers.fill_constant(shape=[1, 2], dtype='float32', value=1) return paddle.fill_constant(shape=[1, 2], dtype='float32', value=1)
def fn_2(): def fn_2():
return layers.fill_constant(shape=[2, 2], dtype='int32', value=2) return paddle.fill_constant(shape=[2, 2], dtype='int32', value=2)
def fn_3(): def fn_3():
return layers.fill_constant(shape=[3], dtype='int32', value=3) return paddle.fill_constant(shape=[3], dtype='int32', value=3)
main_program = fluid.default_startup_program() main_program = paddle.static.default_startup_program()
startup_program = fluid.default_main_program() startup_program = paddle.static.default_main_program()
with fluid.program_guard(main_program, startup_program):
x = layers.fill_constant(shape=[1], dtype='float32', value=0.3)
y = layers.fill_constant(shape=[1], dtype='float32', value=0.1)
z = layers.fill_constant(shape=[1], dtype='float32', value=0.2)
pred_1 = layers.less_than(z, x) # true: 0.2 < 0.3 with paddle.static.program_guard(main_program, startup_program):
pred_2 = layers.less_than(x, y) # false: 0.3 < 0.1 x = paddle.fill_constant(shape=[1], dtype='float32', value=0.3)
pred_3 = layers.equal(x, y) # false: 0.3 == 0.1 y = paddle.fill_constant(shape=[1], dtype='float32', value=0.1)
z = paddle.fill_constant(shape=[1], dtype='float32', value=0.2)
pred_1 = paddle.less_than(z, x) # true: 0.2 < 0.3
pred_2 = paddle.less_than(x, y) # false: 0.3 < 0.1
pred_3 = paddle.equal(x, y) # false: 0.3 == 0.1
# Call fn_1 because pred_1 is True # Call fn_1 because pred_1 is True
out_1 = layers.case( out_1 = paddle.static.nn.case(
pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3) pred_fn_pairs=[(pred_1, fn_1), (pred_2, fn_2)], default=fn_3)
# Argument default is None and no pred in pred_fn_pairs is True. fn_3 will be called. # Argument default is None and no pred in pred_fn_pairs is True. fn_3 will be called.
# because fn_3 is the last callable in pred_fn_pairs. # because fn_3 is the last callable in pred_fn_pairs.
out_2 = layers.case(pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)]) out_2 = paddle.static.nn.case(pred_fn_pairs=[(pred_2, fn_2), (pred_3, fn_3)])
exe = fluid.Executor(fluid.CPUPlace()) exe = paddle.static.Executor(paddle.CPUPlace())
res_1, res_2 = exe.run(main_program, fetch_list=[out_1, out_2]) res_1, res_2 = exe.run(main_program, fetch_list=[out_1, out_2])
print(res_1) # [[1. 1.]] print(res_1) # [[1. 1.]]
print(res_2) # [3 3 3] print(res_2) # [3 3 3]
......
...@@ -38,7 +38,6 @@ from .clip import GradientClipByValue #DEFINE_ALIAS ...@@ -38,7 +38,6 @@ from .clip import GradientClipByValue #DEFINE_ALIAS
# from .clip import set_gradient_clip #DEFINE_ALIAS # from .clip import set_gradient_clip #DEFINE_ALIAS
from .clip import clip #DEFINE_ALIAS from .clip import clip #DEFINE_ALIAS
from .clip import clip_by_norm #DEFINE_ALIAS from .clip import clip_by_norm #DEFINE_ALIAS
from .control_flow import case #DEFINE_ALIAS
from .control_flow import cond #DEFINE_ALIAS from .control_flow import cond #DEFINE_ALIAS
# from .control_flow import DynamicRNN #DEFINE_ALIAS # from .control_flow import DynamicRNN #DEFINE_ALIAS
# from .control_flow import StaticRNN #DEFINE_ALIAS # from .control_flow import StaticRNN #DEFINE_ALIAS
......
...@@ -13,14 +13,12 @@ ...@@ -13,14 +13,12 @@
# limitations under the License. # limitations under the License.
# TODO: define the control flow api # TODO: define the control flow api
from ..fluid.layers import case #DEFINE_ALIAS
from ..fluid.layers import cond #DEFINE_ALIAS from ..fluid.layers import cond #DEFINE_ALIAS
from ..fluid.layers import while_loop #DEFINE_ALIAS from ..fluid.layers import while_loop #DEFINE_ALIAS
from ..fluid.layers import switch_case #DEFINE_ALIAS from ..fluid.layers import switch_case #DEFINE_ALIAS
__all__ = [ __all__ = [
'case',
'cond', 'cond',
# 'DynamicRNN', # 'DynamicRNN',
# 'StaticRNN', # 'StaticRNN',
......
...@@ -17,6 +17,7 @@ __all__ = [ ...@@ -17,6 +17,7 @@ __all__ = [
'batch_norm', 'batch_norm',
'embedding', 'embedding',
'bilinear_tensor_product', 'bilinear_tensor_product',
'case',
'conv2d', 'conv2d',
'conv2d_transpose', 'conv2d_transpose',
'conv3d', 'conv3d',
...@@ -39,6 +40,7 @@ __all__ = [ ...@@ -39,6 +40,7 @@ __all__ = [
from ...fluid.layers import fc #DEFINE_ALIAS from ...fluid.layers import fc #DEFINE_ALIAS
from ...fluid.layers import batch_norm #DEFINE_ALIAS from ...fluid.layers import batch_norm #DEFINE_ALIAS
from ...fluid.layers import bilinear_tensor_product #DEFINE_ALIAS from ...fluid.layers import bilinear_tensor_product #DEFINE_ALIAS
from ...fluid.layers import case #DEFINE_ALIAS
from ...fluid.layers import conv2d #DEFINE_ALIAS from ...fluid.layers import conv2d #DEFINE_ALIAS
from ...fluid.layers import conv2d_transpose #DEFINE_ALIAS from ...fluid.layers import conv2d_transpose #DEFINE_ALIAS
from ...fluid.layers import conv3d #DEFINE_ALIAS from ...fluid.layers import conv3d #DEFINE_ALIAS
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册