declarative_cn.rst 739 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
.. _cn_api_fluid_dygraph_declarative:

declarative
-------------------------------

.. py:decorator:: paddle.fluid.dygraph.jit.declarative

本装饰器将函数内的动态图API转化为静态图API。此装饰器自动处理静态图模式下的
Program和Executor,并将结果作为动态图VarBase返回。

**示例代码**

.. code-block:: python

    import paddle.fluid as fluid
    import numpy as np
    from paddle.fluid.dygraph.jit import declarative

    @declarative
    def func(x):
        x = fluid.dygraph.to_variable(x)
        if fluid.layers.mean(x) < 0:
            x_v = x - 1
        else:
            x_v = x + 1
        return x_v

    x = np.ones([1, 2])
    x_v = func(x)
    print(x_v.numpy()) # [[2. 2.]]