MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / while_loop

Function while_loop

python/paddle/static/nn/control_flow.py:758–1022  ·  view source on GitHub ↗

:api_attr: Static Graph while_loop is one of the control flows. Repeats while_loop `body` until `cond` returns False. Notice: Local variables defined in ``body`` cannot be obtained through ``fetch_list`` of ``Executor`` , variables should be defined outside ``body`` an

(cond, body, loop_vars, is_test=False, name=None)

Source from the content-addressed store, hash-verified

756
757
758def while_loop(cond, body, loop_vars, is_test=False, name=None):
759 """
760 :api_attr: Static Graph
761
762 while_loop is one of the control flows. Repeats while_loop `body` until `cond` returns False.
763
764 Notice:
765 Local variables defined in ``body`` cannot be obtained through ``fetch_list`` of ``Executor`` , variables should
766 be defined outside ``body`` and placed in ``loop_vars`` for looping, then these variables can be fetched by ``fetch_list`` .
767
768 Args:
769 cond(Callable): A callable returning a boolean tensor controlling whether to continue looping. And ``cond`` takes
770 as many arguments as ``loop_vars`` .
771 body(Callable): A callable returning a tuple or list of tensors or DenseTensorArrays of the same arity
772 (length and structure) and types as ``loops_vars`` . And ``body`` takes as many arguments as ``loop_vars`` .
773 loop_vars(list|tuple): A list or tuple of tensors or DenseTensorArrays that is passed to both ``cond`` and ``body`` .
774 is_test(bool, optional): A flag indicating whether execution is in test phase. Default value is False.
775 name(str, optional): Normally there is no need for users to set this property. For more information, please
776 refer to :ref:`api_guide_Name`. Default is None.
777
778 Returns:
779 A list or tuple of Tensors or DenseTensorArrays which returned by ``body`` .
780
781 Examples:
782 .. code-block:: pycon
783
784 >>> import paddle
785 >>> paddle.enable_static()
786
787 >>> def cond(i, ten):
788 ... return i < ten
789
790 >>> def body(i, ten):
791 ... i = i + 1
792 ... return [i, ten]
793
794 >>> main_program = paddle.static.default_main_program()
795 >>> startup_program = paddle.static.default_startup_program()
796 >>> with paddle.static.program_guard(main_program, startup_program):
797 ... i = paddle.full(shape=[1], fill_value=0, dtype='int64') # loop counter
798 ... ten = paddle.full(shape=[1], fill_value=10, dtype='int64') # loop length
799 ... i, ten = paddle.static.nn.while_loop(cond, body, [i, ten])
800
801 ... exe = paddle.static.Executor(paddle.CPUPlace())
802 ... res = exe.run(main_program, feed={}, fetch_list=[i])
803 ... print(res)
804 [array([10], dtype=int64)]
805 """
806 if not callable(cond):
807 raise TypeError("cond in while_loop should be callable")
808 if not callable(body):
809 raise TypeError("body in while_loop should be callable")
810 check_type(loop_vars, 'loop_vars', (list, tuple), 'static.nn.while_loop')
811 if len(loop_vars) == 0:
812 raise ValueError("loop_vars in while_loop should not be empty")
813
814 pre_cond = cond(*loop_vars)
815

Callers 2

popFunction · 0.90
_run_paddle_whileFunction · 0.90

Calls 15

blockMethod · 0.95
in_pir_modeFunction · 0.90
map_structureFunction · 0.90
flattenFunction · 0.90
assert_same_structureFunction · 0.90
hold_mutable_varsFunction · 0.90
copy_mutable_varsFunction · 0.90
TypeErrorClass · 0.85
ValueErrorClass · 0.85
check_variable_and_dtypeFunction · 0.85
listFunction · 0.85

Tested by

no test coverage detected