Skip to content

Commit

Permalink
update docs of program APIs (PaddlePaddle#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangting2020 authored Sep 28, 2020
1 parent 004aa43 commit b8ce6eb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
59 changes: 30 additions & 29 deletions doc/paddle/api/paddle/fluid/framework/default_main_program_cn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,52 @@ default_main_program
此接口可以获取当前用于存储op和variable描述信息的 ``default main program``
此接口可以获取当前用于存储OP和Tensor描述信息的 ``default main program``

``fluid.layers`` 接口中添加的op和variable会存储在 ``default main program``
``default main program`` 是许多编程接口中Program参数的默认值。例如对于 ``Executor.run()`` 如果用户没有传入Program参数,会默认使用 ``default main program``

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

可以使用 :ref:`cn_api_fluid_program_guard` 来替换 ``default main program``
可以使用 :ref:`cn_api_fluid_program_guard` 来切换 ``default main program``。

参数:
- 无

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

返回类型: :ref:`cn_api_fluid_Program`

**代码示例**

.. code-block:: python
import paddle.fluid as fluid
#示例网络:
data = fluid.data(name='image', shape=[None, 3, 224, 224], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64')
conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None)
bn1 = fluid.layers.batch_norm(conv1, act='relu')
pool1 = fluid.layers.pool2d(bn1, 2, 'max', 2)
conv2 = fluid.layers.conv2d(pool1, 16, 5, 1, act=None)
bn2 = fluid.layers.batch_norm(conv2, act='relu')
pool2 = fluid.layers.pool2d(bn2, 2, 'max', 2)
fc1 = fluid.layers.fc(pool2, size=50, act='relu')
fc2 = fluid.layers.fc(fc1, size=102, act='softmax')
loss = fluid.layers.cross_entropy(input=fc2, label=label)
loss = fluid.layers.mean(loss)
opt = fluid.optimizer.Momentum(
import paddle
paddle.enable_static()
# Sample Network:
data = paddle.data(name='image', shape=[None, 3, 224, 224], dtype='float32')
label = paddle.data(name='label', shape=[None, 1], dtype='int64')
conv1 = paddle.static.nn.conv2d(data, 4, 5, 1, act=None)
bn1 = paddle.static.nn.batch_norm(conv1, act='relu')
pool1 = paddle.nn.functional.pool2d(bn1, 2, 'max', 2)
conv2 = paddle.static.nn.conv2d(pool1, 16, 5, 1, act=None)
bn2 = paddle.static.nn.batch_norm(conv2, act='relu')
pool2 = paddle.nn.functional.pool2d(bn2, 2, 'max', 2)
fc1 = paddle.static.nn.fc(pool2, size=50, act='relu')
fc2 = paddle.static.nn.fc(fc1, size=102, act='softmax')
loss = paddle.nn.functional.loss.cross_entropy(input=fc2, label=label)
loss = paddle.mean(loss)
opt = paddle.optimizer.Momentum(
learning_rate=0.1,
momentum=0.9,
regularization=fluid.regularizer.L2Decay(1e-4))
weight_decay=paddle.regularizer.L2Decay(1e-4))
opt.minimize(loss)
print(fluid.default_main_program().num_blocks)
print(fluid.default_main_program().blocks[0].var('image'))
#print the number of blocks in the program, 1 in this case
print(paddle.static.default_main_program().num_blocks) # 1
#print the default_main_program
print(paddle.static.default_main_program())
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,29 @@ default_startup_program
该函数可以获取默认/全局 startup :ref:`cn_api_fluid_Program` (初始化启动程序)。

:ref:`_cn_api_fluid_layers` 中的函数会新建参数或 :ref:`cn_api_paddle_data_reader_reader` (读取器) 或 `NCCL <https://developer.nvidia.com/nccl>`_ 句柄作为全局变量
``paddle.nn`` 中的函数将参数初始化OP追加到 ``startup program`` 中, 运行 ``startup program`` 会完成参数的初始化

startup_program会使用内在的OP(算子)去初始化他们,并由 :ref:`_cn_api_fluid_layers` 中的函数将这些OP追加到startup :ref:`cn_api_fluid_Program`
该函数将返回默认的或当前的 ``startup program`` 。用户可以使用 :ref:`cn_api_fluid_program_guard` 来切换 :ref:`cn_api_fluid_Program` 。

该函数将返回默认的或当前的startup_program。用户可以使用 :ref:`cn_api_fluid_program_guard` 去切换 :ref:`cn_api_fluid_default_startup_program` 。

返回: 当前的默认/全局 初始化 :ref:`cn_api_fluid_Program`
返回: 当前的默认/全局的 ``startup program`` 。

返回类型: :ref:`cn_api_fluid_Program`

**代码示例:**

.. code-block:: python
import paddle.fluid as fluid
main_program = fluid.Program()
startup_program = fluid.Program()
with fluid.program_guard(main_program=main_program, startup_program=startup_program):
x = fluid.layers.data(name="x", shape=[-1, 784], dtype='float32')
y = fluid.layers.data(name="y", shape=[-1, 1], dtype='int32')
z = fluid.layers.fc(name="fc", input=x, size=10, act="relu")
print("main program is: {}".format(fluid.default_main_program()))
print("start up program is: {}".format(fluid.default_startup_program()))
import paddle
paddle.enable_static()
main_program = paddle.static.Program()
startup_program = paddle.static.Program()
with paddle.static.program_guard(main_program=main_program, startup_program=startup_program):
x = paddle.data(name="x", shape=[-1, 784], dtype='float32')
y = paddle.data(name="y", shape=[-1, 1], dtype='int32')
z = paddle.static.nn.fc(name="fc", input=x, size=10, act="relu")
print("main program is: {}".format(paddle.static.default_main_program()))
print("start up program is: {}".format(paddle.static.default_startup_program()))

0 comments on commit b8ce6eb

Please sign in to comment.