program_guard

paddle.static. program_guard ( main_program, startup_program=None ) [source]
Api_attr

Static Graph

Change the global main program and startup program with with statement. Layer functions in the Python with block will append operators and Tensors to the new main programs.

Parameters
  • main_program (Program) – New main program inside with statement.

  • startup_program (Program, optional) – New startup program inside with statement. None means not changing startup program, default_startup_program is still used. Default: None.

Examples

>>> import paddle

>>> paddle.enable_static()
>>> main_program = paddle.static.Program()
>>> startup_program = paddle.static.Program()
>>> with paddle.static.program_guard(main_program, startup_program):
...     data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')
...     hidden = paddle.static.nn.fc(x=data, size=10, activation='relu')

Notes: The temporary Program can be used if the user does not need to construct either of startup program or main program.

Examples

>>> import paddle

>>> paddle.enable_static()
>>> main_program = paddle.static.Program()
>>> # does not care about startup program. Just pass a temporary value.
>>> with paddle.static.program_guard(main_program, paddle.static.Program()):
...     data = paddle.static.data(name='image', shape=[None, 784, 784], dtype='float32')

Used in the guide/tutorials