name_scope

Note: This API is only avaliable in [Static Graph] mode

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

Generate hierarchical name prefix for the operators.

Note

This should only used for debugging and visualization purpose. Don’t use it for serious analysis such as graph/program transformations.

Parameters

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

Examples

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

# Op are created in the default main program.
for op in fluid.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/'