|TensorFlow 1 version||View source on GitHub|
body while the condition
cond is true. (deprecated argument values)
tf.while_loop( cond, body, loop_vars, shape_invariants=None, parallel_iterations=10, back_prop=True, swap_memory=False, maximum_iterations=None, name=None )
Used in the notebooks
|Used in the guide||Used in the tutorials|
cond is a callable returning a boolean scalar tensor.
body is a callable
returning a (possibly nested) tuple, namedtuple or list of tensors of the same
arity (length and structure) and types as
loop_vars is a
(possibly nested) tuple, namedtuple or list of tensors that is passed to both
body both take as many arguments as there are
In addition to regular Tensors or IndexedSlices, the body may accept and return TensorArray objects. The flows of the TensorArray objects will be appropriately forwarded between loops and during gradient calculations.
body exactly once (inside the
while_loop, and not at all during
stitches together the graph fragments created during the
calls with some additional graph nodes to create the graph flow that
cond returns false.
tf.while_loop() strictly enforces shape invariants for
the loop variables. A shape invariant is a (possibly partial) shape that
is unchanged across the iterations of the loop. An error will be raised
if the shape of a loop variable after an iteration is determined to be more
general than or incompatible with its shape invariant. For example, a shape
of [11, None] is more general than a shape of [11, 17], and [11, 21] is not
compatible with [11, 17]. By default (if the argument
not specified), it is assumed that the initial shape of each tensor in
loop_vars is the same in every iteration. The
allows the caller to specify a less specific shape invariant for each loop
variable, which is needed if the shape varies between iterations. The
function may also be used in the
body function to indicate that
the output loop variable has a particular shape. The shape invariant for
SparseTensor and IndexedSlices are treated specially as follows:
a) If a loop variable is a SparseTensor, the shape invariant must be TensorShape([r]) where r is the rank of the dense tensor represented by the sparse tensor. It means the shapes of the three tensors of the SparseTensor are ([None], [None, r], [r]). NOTE: The shape invariant here is the shape of the SparseTensor.dense_shape property. It must be the shape of a vector.
b) If a loop variable is an IndexedSlices, the shape invariant must be a shape invariant of the values tensor of the IndexedSlices. It means the shapes of the three tensors of the IndexedSlices are (shape, [shape], [shape.ndims]).
while_loop implements non-strict semantics, enabling multiple iterations
to run in parallel. The maximum number of parallel iterations can be
parallel_iterations, which gives users some control over
memory consumption and execution order. For correct programs,
should return the same result for any parallel_iterations > 0.
For training, TensorFlow stores the tensors that are produced in the forward inference and are needed in back propagation. These tensors are a main source of memory consumption and often cause OOM errors when training on GPUs. When the flag swap_memory is true, we swap out these tensors from GPU to CPU. This for example allows us to train RNN models with very long sequences and large batches.
||A callable that represents the termination condition of the loop.|
||A callable that represents the loop body.|
A (possibly nested) tuple, namedtuple or list of numpy array,
||The shape invariants for the loop variables.|
||The number of iterations allowed to run in parallel. It must be a positive integer.|
(optional) Deprecated. False disables support for back
propagation. Prefer using
||Whether GPU-CPU memory swap|