From bf99bc4a6288affe20c94aaadfa30e3045d0a846 Mon Sep 17 00:00:00 2001 From: Tao Luo Date: Mon, 28 Sep 2020 11:12:46 +0800 Subject: [PATCH] update name_scope example code (#27594) * update name_scope example code test=document_fix * refine name_scope doc test=document_fix --- python/paddle/fluid/framework.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 106c9a00361..8a84c584deb 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -506,11 +506,12 @@ def name_scope(prefix=None): """ :api_attr: Static Graph - Generate hierarchical name prefix for the operators. + 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. Args: prefix(str, optional): prefix. Default is none. @@ -518,21 +519,22 @@ def name_scope(prefix=None): Examples: .. code-block:: python - import paddle.fluid as fluid - with fluid.name_scope("s1"): - a = fluid.data(name='data', shape=[None, 1], dtype='int32') + import paddle + paddle.enable_static() + with paddle.static.name_scope("s1"): + a = paddle.data(name='data', shape=[None, 1], dtype='int32') b = a + 1 - with fluid.name_scope("s2"): + with paddle.static.name_scope("s2"): c = b * 1 - with fluid.name_scope("s3"): + with paddle.static.name_scope("s3"): d = c / 1 - with fluid.name_scope("s1"): - f = fluid.layers.pow(d, 2.0) - with fluid.name_scope("s4"): + with paddle.static.name_scope("s1"): + f = paddle.tensor.pow(d, 2.0) + with paddle.static.name_scope("s4"): g = f - 1 # Op are created in the default main program. - for op in fluid.default_main_program().block(0).ops: + 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/' -- GitLab