enable_to_static

paddle.jit. enable_to_static ( enable_to_static_bool ) [源代码]

全局启用或禁用从动态图到静态图的转换。

参数

  • enable_to_static_bool (bool) - 启用或禁用动转静。为 True 时启用动转静, 为 False 时关闭动转静。

代码示例

>>> import paddle
>>> @paddle.jit.to_static
>>> def func(x):
...     if paddle.mean(x) > 0:
...         x_v = x - 1
...     else:
...         x_v = x + 1
...     return x_v
...
>>> paddle.jit.enable_to_static(False)

>>> x = paddle.ones([1, 2])
>>> # ProgramTranslator is disabled so the func is run in dygraph
>>> print(func(x))
Tensor(shape=[1, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 0.]])