提交 90194d8e 编写于 作者: Z zhangting2020

update docs of program APIs

上级 f457a8bf
...@@ -9,18 +9,16 @@ default_main_program ...@@ -9,18 +9,16 @@ 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` 返回类型: :ref:`cn_api_fluid_Program`
...@@ -28,32 +26,35 @@ default_main_program ...@@ -28,32 +26,35 @@ default_main_program
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
#示例网络: paddle.enable_static()
data = fluid.data(name='image', shape=[None, 3, 224, 224], dtype='float32') # Sample Network:
label = fluid.data(name='label', shape=[None, 1], dtype='int64') data = paddle.data(name='image', shape=[None, 3, 224, 224], dtype='float32')
label = paddle.data(name='label', shape=[None, 1], dtype='int64')
conv1 = fluid.layers.conv2d(data, 4, 5, 1, act=None) conv1 = paddle.static.nn.conv2d(data, 4, 5, 1, act=None)
bn1 = fluid.layers.batch_norm(conv1, act='relu') bn1 = paddle.static.nn.batch_norm(conv1, act='relu')
pool1 = fluid.layers.pool2d(bn1, 2, 'max', 2) pool1 = paddle.nn.functional.pool2d(bn1, 2, 'max', 2)
conv2 = fluid.layers.conv2d(pool1, 16, 5, 1, act=None) conv2 = paddle.static.nn.conv2d(pool1, 16, 5, 1, act=None)
bn2 = fluid.layers.batch_norm(conv2, act='relu') bn2 = paddle.static.nn.batch_norm(conv2, act='relu')
pool2 = fluid.layers.pool2d(bn2, 2, 'max', 2) pool2 = paddle.nn.functional.pool2d(bn2, 2, 'max', 2)
fc1 = fluid.layers.fc(pool2, size=50, act='relu') fc1 = paddle.static.nn.fc(pool2, size=50, act='relu')
fc2 = fluid.layers.fc(fc1, size=102, act='softmax') fc2 = paddle.static.nn.fc(fc1, size=102, act='softmax')
loss = fluid.layers.cross_entropy(input=fc2, label=label) loss = paddle.nn.functional.loss.cross_entropy(input=fc2, label=label)
loss = fluid.layers.mean(loss) loss = paddle.mean(loss)
opt = fluid.optimizer.Momentum( opt = paddle.optimizer.Momentum(
learning_rate=0.1, learning_rate=0.1,
momentum=0.9, momentum=0.9,
regularization=fluid.regularizer.L2Decay(1e-4)) weight_decay=paddle.regularizer.L2Decay(1e-4))
opt.minimize(loss) opt.minimize(loss)
print(fluid.default_main_program().num_blocks) #print the number of blocks in the program, 1 in this case
print(fluid.default_main_program().blocks[0].var('image')) print(paddle.static.default_main_program().num_blocks) # 1
#print the default_main_program
print(paddle.static.default_main_program())
...@@ -15,13 +15,11 @@ default_startup_program ...@@ -15,13 +15,11 @@ default_startup_program
该函数可以获取默认/全局 startup :ref:`cn_api_fluid_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` 。 返回: 当前的默认/全局的 ``startup program`` 。
返回: 当前的默认/全局 初始化 :ref:`cn_api_fluid_Program`
返回类型: :ref:`cn_api_fluid_Program` 返回类型: :ref:`cn_api_fluid_Program`
...@@ -29,17 +27,17 @@ startup_program会使用内在的OP(算子)去初始化他们,并由 :ref: ...@@ -29,17 +27,17 @@ startup_program会使用内在的OP(算子)去初始化他们,并由 :ref:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
main_program = fluid.Program() paddle.enable_static()
startup_program = fluid.Program() main_program = paddle.static.Program()
with fluid.program_guard(main_program=main_program, startup_program=startup_program): startup_program = paddle.static.Program()
x = fluid.layers.data(name="x", shape=[-1, 784], dtype='float32') with paddle.static.program_guard(main_program=main_program, startup_program=startup_program):
y = fluid.layers.data(name="y", shape=[-1, 1], dtype='int32') x = paddle.data(name="x", shape=[-1, 784], dtype='float32')
z = fluid.layers.fc(name="fc", input=x, size=10, act="relu") 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(fluid.default_main_program())) print("main program is: {}".format(paddle.static.default_main_program()))
print("start up program is: {}".format(fluid.default_startup_program())) print("start up program is: {}".format(paddle.static.default_startup_program()))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册