name_scope

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

该函数为静态图下的operators生成不同的命名空间。该函数只用于静态图下的调试和可视化,不建议用在其它方面。

参数:

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

示例代码

  1. import paddle
  2. paddle.enable_static()
  3. with paddle.static.name_scope("s1"):
  4. a = paddle.static.data(name='data', shape=[None, 1], dtype='int32')
  5. b = a + 1
  6. with paddle.static.name_scope("s2"):
  7. c = b * 1
  8. with paddle.static.name_scope("s3"):
  9. d = c / 1
  10. with paddle.static.name_scope("s1"):
  11. f = paddle.tensor.pow(d, 2.0)
  12. print(f)
  13. with paddle.static.name_scope("s4"):
  14. g = f - 1
  15. # Op are created in the default main program.
  16. for op in paddle.static.default_main_program().block(0).ops:
  17. # elementwise_add is created in /s1/
  18. if op.type == 'elementwise_add':
  19. assert op.desc.attr("op_namescope") == '/s1/'
  20. # elementwise_mul is created in '/s1/s2'
  21. elif op.type == 'elementwise_mul':
  22. assert op.desc.attr("op_namescope") == '/s1/s2/'
  23. # elementwise_div is created in '/s1/s3'
  24. elif op.type == 'elementwise_div':
  25. assert op.desc.attr("op_namescope") == '/s1/s3/'
  26. # elementwise_sub is created in '/s4'
  27. elif op.type == 'elementwise_sub':
  28. assert op.desc.attr("op_namescope") == '/s4/'
  29. # pow is created in /s1_1/
  30. elif op.type == 'pow':
  31. assert op.desc.attr("op_namescope") == '/s1_1/'