program_guard

api_attr

declarative programming (static graph)

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

Change the global main program and startup program with “with” statement. Layer functions in the Python “with” block will append operators and variables 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.fluid as fluid

main_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(main_program, startup_program):
    data = fluid.data(name='image', shape=[None, 784, 784], dtype='float32')
    hidden = fluid.layers.fc(input=data, size=10, act='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.fluid as fluid

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