未验证 提交 eb37ee2a 编写于 作者: Z Zhang Ting 提交者: GitHub

modify docs of program API, test=document_fix (#27600)

上级 36c04102
...@@ -5278,32 +5278,32 @@ def default_startup_program(): ...@@ -5278,32 +5278,32 @@ def default_startup_program():
""" """
Get default/global startup program. Get default/global startup program.
The layer function in :ref:`api_fluid_layers` will create parameters, :ref:`api_paddle_data_reader_reader` , The :code:`paddle.nn` function will append the initialization operators into startup program.
`NCCL <https://developer.nvidia.com/nccl>`_ handles as global variables. The :code:`startup_program` will The :code:`startup_program` will initialize the parameters by the OPs.
initialize them by the OPs in startup :ref:`api_fluid_Program` . The :ref:`api_fluid_layers` function will
append these initialization operators into startup program. This method will return the default or the current startup program. Users can use
:ref:`api_paddle_fluid_framework_program_guard` to switch :ref:`api_paddle_fluid_framework_Program` .
This method will return the :code:`default` or the :code:`current` startup Returns:
program. Users can use :ref:`api_fluid_program_guard` to switch :ref:`api_fluid_Program` . Program: current default startup program.
Returns: current default startup :ref:`api_fluid_Program`
Returns type: :ref:`api_fluid_Program` Returns type:
Examples: Examples:
.. 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()))
""" """
return _startup_program_ return _startup_program_
...@@ -5311,52 +5311,53 @@ def default_startup_program(): ...@@ -5311,52 +5311,53 @@ def default_startup_program():
def default_main_program(): def default_main_program():
""" """
This API can be used to get ``default main program`` which store the This API can be used to get ``default main program`` which store the
descriptions of ``op`` and ``variable``. descriptions of Ops and tensors.
For example ``z = fluid.layers.elementwise_add(x, y)`` will create a new ``elementwise_add`` For example ``z = paddle.elementwise_add(x, y)`` will create a new ``elementwise_add``
``op`` and a new ``z`` ``variable``, and they will be recorded in ``default main program`` Op and a new ``z`` tensor, and they will be recorded in ``default main program`` .
The ``default_main_program`` is the default value for ``Program`` parameter in The ``default main program`` is the default value for ``Program`` parameter in
a lot of ``fluid`` APIs. For example, the :code:`Executor.run()` will execute the a lot of APIs. For example, the :code:`Executor.run()` will execute the
:code:`default_main_program` when the program is not specified. :code:`default_main_program` when the program is not specified.
If you want to replace the ``default main program``, you can use :ref:`api_fluid_program_guard` If you want to switch the ``default main program``, you can use :ref:`api_paddle_fluid_framework_program_guard` .
Returns: Returns:
:ref:`api_fluid_Program`: a ``Program`` which holding the descriptions of ops and variables in the network. Program: A ``Program`` which holding the descriptions of OPs and tensors in the network.
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle.fluid as fluid import paddle
paddle.enable_static()
# Sample Network: # Sample Network:
data = fluid.data(name='image', shape=[None, 3, 224, 224], dtype='float32') data = paddle.data(name='image', shape=[None, 3, 224, 224], dtype='float32')
label = fluid.data(name='label', shape=[None, 1], dtype='int64') 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 the number of blocks in the program, 1 in this case #print the number of blocks in the program, 1 in this case
print(fluid.default_main_program().num_blocks) print(paddle.static.default_main_program().num_blocks) #[1]
#print the description of variable 'image' #print the description of variable 'image'
print(fluid.default_main_program().blocks[0].var('image')) print(paddle.static.default_main_program())
""" """
return _main_program_ return _main_program_
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册