default_main_program

此接口可以获取当前用于存储op和variable描述信息的 default main program

fluid.layers 接口中添加的op和variable会存储在 default main program

default main program 是fluid的许多编程接口中Program参数的默认值。例如对于 Executor.run() 如果用户没有传入Program参数,会默认使用 default main program

可以使用 program_guard 来替换 default main program

  • 参数:

返回: 当前默认用于存储op和variable描述的Program

返回类型: Program

代码示例

  1. import paddle.fluid as fluid
  2.  
  3. #示例网络:
  4. data = fluid.layers.data(name='image', shape=[3, 224, 224], dtype='float32')
  5. label = fluid.layers.data(name='label', shape=[1], dtype='int64')
  6.  
  7. conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None)
  8. bn1 = fluid.layers.batch_norm(conv1, act='relu')
  9. pool1 = fluid.layers.pool2d(bn1, 2, 'max', 2)
  10. conv2 = fluid.layers.conv2d(pool1, 16, 5, 1, act=None)
  11. bn2 = fluid.layers.batch_norm(conv2, act='relu')
  12. pool2 = fluid.layers.pool2d(bn2, 2, 'max', 2)
  13.  
  14. fc1 = fluid.layers.fc(pool2, size=50, act='relu')
  15. fc2 = fluid.layers.fc(fc1, size=102, act='softmax')
  16.  
  17. loss = fluid.layers.cross_entropy(input=fc2, label=label)
  18. loss = fluid.layers.mean(loss)
  19. opt = fluid.optimizer.Momentum(
  20. learning_rate=0.1,
  21. momentum=0.9,
  22. regularization=fluid.regularizer.L2Decay(1e-4))
  23. opt.minimize(loss)
  24.  
  25. print(fluid.default_main_program().num_blocks)
  26. print(fluid.default_main_program().blocks[0].var('image'))