declarative

paddle.jit. declarative ( function=None, input_spec=None ) [source]

Converts imperative dygraph APIs into declarative function APIs. Decorator @declarative handles the Program and Executor of static mode and returns the result as dygraph Tensor(s). Users could use the returned dygraph Tensor(s) to do imperative training, inference, or other operations. If the decorated function calls other imperative function, the called one will be converted into declarative function as well.

Parameters
  • function (callable) – callable imperative function.

  • input_spec (list[InputSpec]) – list of InputSpec to specific the shape/dtype/name information of each input Tensor.

Returns

containing the numerical result.

Return type

Tensor(s)

Examples

import paddle
from paddle.jit import to_static

@to_static
def func(x):
    if paddle.mean(x) < 0:
        x_v = x - 1
    else:
        x_v = x + 1
    return x_v

x = paddle.ones([1, 2], dtype='float32')
x_v = func(x)
print(x_v) # [[2. 2.]]