Control Flow¶
In programming languages, the control flow determines the order in which statements are executed. Common control flows contain sequential execution, branching, and looping. PaddlePaddle Fluid inherits this concept and provides a variety of control flow APIs to control the execution logic of the deep learning model during training or prediction.
IfElse¶
Conditional branch, for the input of a batch, according to the given conditions, select the process in true_block
or false_block
to execute respectively, and then merge the outputs of the two branches into one after the execution. In general, conditional expressions can be generated by a logical comparison API such as api_fluid_layers_less_than, api_fluid_layers_equal.
Please refer to api_fluid_layers_IfElse
Note: A new OP api_fluid_layers_cond is highly recommended instead of IfElse
. OP api_fluid_layers_cond is easier to use and is called with less code but does the same thing as IfElse
.
Switch¶
Switch, like the switch-case
declaration commonly found in programming languages, selects different branch to execute depending on the value of the input expression. Specifically, the Switch
control flow defined by Fluid has the following characteristics:
The condition of the case is a bool type value, which is a tensor type Variable in the Program;
It checks each case one by one, selects the first case that satisfies the condition, and exits the block after completion of the execution;
If all cases do not meet the conditions, the default case will be selected for execution.
Please refer to api_fluid_layers_Switch
Note: A new OP api_fluid_layers_case is highly recommended instead of Switch
. OP api_fluid_layers_case is easier to use and is called with less code but does the same thing as Switch
.
While¶
When the condition is true, repeatedly execute logic in the block
which While
flow belongs to until the condition is judged to be false and the loop will be ended. The related APIs are as follows:
api_fluid_layers_increment : It is usually used to count the number of loops;
api_fluid_layers_array_read : Reads Variable from the specified location in
LOD_TENSOR_ARRAY
to perform calculations;api_fluid_layers_array_write : Writes the Variable back to the specified location in
LOD_TENSOR_ARRAY
and stores the result of the calculation.
Please refer to api_fluid_layers_While
Note: A new OP api_fluid_layers_while_loop is highly recommended instead of While
. OP api_fluid_layers_while_loop is easier to use and is called with less code but does the same thing as While
.
DynamicRNN¶
Dynamic RNN can process a batch of unequal(variable)-length sequence data, which accepts the variable with lod_level=1
as input. In the block
of DynamicRNN
, the user needs to customize RNN’s single-step calculation logic. At each time step, the user can write the state to be remembered to the memory
of DynamicRNN
and write the required output to its output
.
api_fluid_layers_sequence_last_step gets the output of the last time step of DynamicRNN
.
Please refer to api_fluid_layers_DynamicRNN
StaticRNN¶
Static RNN can only process fixed-length sequence data, and accept Variable with lod_level=0
as input. Similar to DynamicRNN
, at each single time step of the RNN, the user needs to customize the calculation logic and export the status and output.
Please refer to api_fluid_layers_StaticRNN