name_scope

paddle.static. name_scope ( prefix=None ) [源代码]

该函数为静态图下的 operators 生成不同的命名空间。

注解

该函数只用于静态图下的调试和可视化,不建议用在其它方面,否则会引起内存泄露。

参数

  • prefix (str,可选) - 名称前缀。默认值为 None。

代码示例

>>> 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/'