name_scope

paddle.static. name_scope ( prefix=None ) [source]

Generate hierarchical name prefix for the operators in Static Graph.

Note

This should only used for debugging and visualization purpose. Don’t use it for serious analysis such as graph/program transformations. Don’t use it in dygraph, since it will cause memory leak.

Parameters

prefix (str, optional) – prefix. Default is none.

Examples

>>> import paddle
>>> paddle.enable_static()
>>> with paddle.static.name_scope("s1"):
...     a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
...     b = a + paddle.to_tensor(1)
...     with paddle.static.name_scope("s2"):
...         c = b * paddle.to_tensor(1)
...     with paddle.static.name_scope("s3"):
...         d = c / paddle.to_tensor(1)
>>> with paddle.static.name_scope("s1"):
...     f = paddle.tensor.pow(d, paddle.to_tensor(2.0))
>>> with paddle.static.name_scope("s4"):
...     g = f - paddle.to_tensor(1)

>>> # Op are created in the default main program.
>>> for op in paddle.static.default_main_program().block(0).ops:
...     # elementwise_add is created in /s1/
...     if op.type == 'elementwise_add':
...         assert op.desc.attr("op_namescope") == '/s1/'
...     # elementwise_mul is created in '/s1/s2'
...     elif op.type == 'elementwise_mul':
...         assert op.desc.attr("op_namescope") == '/s1/s2/'
...     # elementwise_div is created in '/s1/s3'
...     elif op.type == 'elementwise_div':
...         assert op.desc.attr("op_namescope") == '/s1/s3/'
...     # elementwise_sum is created in '/s4'
...     elif op.type == 'elementwise_sub':
...         assert op.desc.attr("op_namescope") == '/s4/'
...     # pow is created in /s1_1/
...     elif op.type == 'pow':
...         assert op.desc.attr("op_namescope") == '/s1_1/'