switch

paddle.utils.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
>>> name1 = paddle.utils.unique_name.generate('fc')
>>> name2 = paddle.utils.unique_name.generate('fc')
>>> print(name1, name2)
fc_0 fc_1

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

>>> paddle.utils.unique_name.switch(pre_generator, pre_dygraph_name_checker) # switch back to pre_generator.
>>> name3 = paddle.utils.unique_name.generate('fc')
>>> print(name3)
fc_2