.. note::: 1. :code:`Program.clone()` method DOES NOT clone :ref:`api_paddle_io_DataLoader` . 2. Recommend you to use :code:`clone` before using :code:`Optimizer.minimize` . 3. This API has no effect in Dygraph Mode. Create a new Program with for
(self, for_test=False)
| 6547 | return self.desc._version() |
| 6548 | |
| 6549 | def clone(self, for_test=False): |
| 6550 | """ |
| 6551 | .. note::: |
| 6552 | 1. :code:`Program.clone()` method DOES NOT clone :ref:`api_paddle_io_DataLoader` . |
| 6553 | 2. Recommend you to use :code:`clone` before using :code:`Optimizer.minimize` . |
| 6554 | 3. This API has no effect in Dygraph Mode. |
| 6555 | |
| 6556 | Create a new Program with forward content of original one when ``for_test=True``. |
| 6557 | Create a new Program as same as the original one when ``for_test=False``. |
| 6558 | |
| 6559 | Some operators, e.g., :ref:`api_paddle_base_layers_batch_norm` , behave differently between |
| 6560 | training and testing. They have an attribute, :code:`is_test`, to |
| 6561 | control this behaviour. This method will change the :code:`is_test` |
| 6562 | attribute of them to :code:`True` when :code:`for_test=True`. |
| 6563 | |
| 6564 | * Set for_test to False when you want to clone the program for training. |
| 6565 | * Set for_test to True when you want to clone the program for testing. |
| 6566 | We will prune the backward and optimize part of the program when you |
| 6567 | use :code:`clone` after :code:`Optimizer.minimize`, but we still |
| 6568 | recommend you to use :code:`clone` before using :code:`Optimizer.minimize`. |
| 6569 | |
| 6570 | Examples: |
| 6571 | .. code-block:: pycon |
| 6572 | :name: code-example-1 |
| 6573 | |
| 6574 | >>> import paddle |
| 6575 | >>> import paddle.static as static |
| 6576 | |
| 6577 | >>> paddle.enable_static() |
| 6578 | |
| 6579 | >>> img = static.data(name='image', shape=[None, 784]) |
| 6580 | >>> pred = static.nn.fc(x=img, size=10, activation='relu') |
| 6581 | >>> loss = paddle.mean(pred) |
| 6582 | >>> # Here we use clone before Momentum |
| 6583 | >>> test_program = static.default_main_program().clone(for_test=True) |
| 6584 | >>> optimizer = paddle.optimizer.Momentum(learning_rate=0.01, momentum=0.9) |
| 6585 | >>> optimizer.minimize(loss) |
| 6586 | |
| 6587 | Args: |
| 6588 | |
| 6589 | for_test (bool): True if change the :code:`is_test` attribute of operators to :code:`True` |
| 6590 | and prune the backward and optimize part of the program. The default value is :code:`False` . |
| 6591 | |
| 6592 | Returns: |
| 6593 | Program: A new Program with forward content of original one when ``for_test=True``. A new Program as same as the original one when ``for_test=False`` |
| 6594 | |
| 6595 | |
| 6596 | Examples: |
| 6597 | |
| 6598 | .. note:: |
| 6599 | The Program's order maybe different after :code:`clone` and |
| 6600 | this will not affect your training or testing progress. In the following |
| 6601 | example we give you an simple method :code:`print_prog(program)` to |
| 6602 | print Program Descs inorder to make sure you have same print result |
| 6603 | after :code:`clone`: |
| 6604 | |
| 6605 | .. code-block:: pycon |
| 6606 | :name: code-example-2 |