mindspore.ops.WhileLoop
- class mindspore.ops.WhileLoop[source]
Provide a useful op for reducing compilation times of while loop. The execution logic of the WhileLoop operator can be roughly represented by the following code:
def WhileLoop(cond_func, loop_func, init_val): while(cond_func(init_val)): init_val = loop_func(init_val) return init_val
The current WhileLoop operator has the following syntactic limitations:
Using a side-effect function as loop_func is currently not support, such as operations that modify parameters, global variables, etc.
The return value of loop_func being of a different type or shape from the init_val is currently not support.
Warning
This is an experimental API that is subject to change or deletion.
- Inputs:
cond_func (Function) - The condition function.
loop_func (Function) - The loop function, take one argument and return value has the same type with input argument.
init_val (Union[Tensor, number, str, bool, list, tuple, dict]) - The initial value.
- Outputs:
Union[Tensor, number, str, bool, list, tuple, dict], the final result of the while loop, has same type and shape with input init_val .
- Raises
TypeError – If cond_func is not a function.
TypeError – If loop_func is not a function.
ValueError – If loop_func cannot take init_val as input or has different output type or shape with init_val .
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> from mindspore import ops >>> def loop_while_fun(init_val): ... val = init_val ... val = val + 1 ... return val ... >>> init_state = 10 >>> while_loop = ops.WhileLoop() >>> result = while_loop(lambda x : x < 100, loop_while_fun, init_state) >>> print(result) 100