name_scope_cn.rst 1.6 KB
Newer Older
H
Hao Wang 已提交
1 2 3 4 5
.. _cn_api_fluid_name_scope:

name_scope
-------------------------------

6 7
**注意:该API仅支持【静态图】模式**

H
Hao Wang 已提交
8 9 10
.. py:function:: paddle.fluid.name_scope(prefix=None)


T
Tao Luo 已提交
11
该函数为operators生成不同的命名空间。该函数只用于调试和可视化,不建议用在其它方面。
H
Hao Wang 已提交
12 13 14


参数:
15
  - **prefix** (str,可选) - 名称前缀。默认值为None。
H
Hao Wang 已提交
16 17 18 19 20 21 22

**示例代码**

.. code-block:: python
          
     import paddle.fluid as fluid
     with fluid.name_scope("s1"):
T
Tao Luo 已提交
23
        a = fluid.data(name='data', shape=[None, 1], dtype='int32')
H
Hao Wang 已提交
24 25 26 27 28 29 30 31 32 33
        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

34 35 36 37
     # 没有指定的话默认OP在default main program中。
     for op in fluid.default_main_program().block(0).ops:
         # elementwise_add在/s1/中创建
         if op.type == 'elementwise_add':
T
Tao Luo 已提交
38
             assert op.desc.attr("op_namescope") == '/s1/'
39 40
         # elementwise_mul在/s1/s2中创建
         elif op.type == 'elementwise_mul':
T
Tao Luo 已提交
41
             assert op.desc.attr("op_namescope") == '/s1/s2/'
42 43
         # elementwise_div在/s1/s3中创建
         elif op.type == 'elementwise_div':
T
Tao Luo 已提交
44
             assert op.desc.attr("op_namescope") == '/s1/s3/'
45 46
         # elementwise_sum在/s4/中创建
         elif op.type == 'elementwise_sub':
T
Tao Luo 已提交
47
             assert op.desc.attr("op_namescope") == '/s4/'
48 49
         # pow在/s1_1/中创建
         elif op.type == 'pow':
T
Tao Luo 已提交
50
             assert op.desc.attr("op_namescope") == '/s1_1/'