switch

paddle.fluid.unique_name.switch(new_generator=None, new_para_name_checker=None)[source]

Switch the namespace of in current context to a new namespace. Though switch() and guard() can both change namespace, guard() is recommended since it can manage the context better together with with statement.

Parameters
  • new_generator (UniqueNameGenerator, optional) – A new UniqueNameGenerator, not required normally. Default is None, which means switch to a new anonymous namespace.

  • new_para_name_checker (DygraphParameterNameChecker, optional) – A new DygraphParameterNameChecker, not required normally. Default is None, which means switch to a new parameter name checker.

Returns

The previous UniqueNameGenerator. DygraphParameterNameChecker: The previous DygraphParameterNameChecker

Return type

UniqueNameGenerator

Examples

import paddle.fluid as fluid
name1 = fluid.unique_name.generate('fc')
name2 = fluid.unique_name.generate('fc')
print(name1, name2) # fc_0, fc_1

pre_generator, pre_dygraph_name_checker = fluid.unique_name.switch() # switch to a new anonymous namespace.
name2 = fluid.unique_name.generate('fc')
print(name2) # fc_0

fluid.unique_name.switch(pre_generator, pre_dygraph_name_checker) # switch back to pre_generator.
name3 = fluid.unique_name.generate('fc')
print(name3) # fc_2, since pre_generator has generated fc_0, fc_1.