guard

paddle.utils.unique_name. guard ( new_generator=None ) [source]

Change the namespace of unique name with with statement. After calling it, a new namespace in the context of with will be created, and it will number names from zero again when calling generate() with same key.

Parameters

new_generator (str|bytes, optional) – New name of global namespace. Note that str in Python2 was spilted into str and bytes in Python3, so here are two types. Default is None. If not None, new_generator will be added into the prefix of unique name generated by generate().

Returns

None.

Examples

>>> import paddle
>>> with paddle.utils.unique_name.guard():
...     name_1 = paddle.utils.unique_name.generate('fc')
>>> with paddle.utils.unique_name.guard():
...     name_2 = paddle.utils.unique_name.generate('fc')
>>> print(name_1, name_2)
fc_0 fc_0

>>> with paddle.utils.unique_name.guard('A'):
...     name_1 = paddle.utils.unique_name.generate('fc')
>>> with paddle.utils.unique_name.guard('B'):
...     name_2 = paddle.utils.unique_name.generate('fc')
>>> print(name_1, name_2)
Afc_0 Bfc_0