MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / while_loop

Function while_loop

tensorflow/python/ops/control_flow_ops.py:2483–2757  ·  view source on GitHub ↗

Repeat `body` while the condition `cond` is true. `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`. `loop_vars` is a (possibly nes

(cond,
               body,
               loop_vars,
               shape_invariants=None,
               parallel_iterations=10,
               back_prop=True,
               swap_memory=False,
               name=None,
               maximum_iterations=None,
               return_same_structure=False)

Source from the content-addressed store, hash-verified

2481# pylint: disable=redefined-outer-name
2482@tf_export(v1=["while_loop"])
2483def while_loop(cond,
2484 body,
2485 loop_vars,
2486 shape_invariants=None,
2487 parallel_iterations=10,
2488 back_prop=True,
2489 swap_memory=False,
2490 name=None,
2491 maximum_iterations=None,
2492 return_same_structure=False):
2493 """Repeat `body` while the condition `cond` is true.
2494
2495 `cond` is a callable returning a boolean scalar tensor. `body` is a callable
2496 returning a (possibly nested) tuple, namedtuple or list of tensors of the same
2497 arity (length and structure) and types as `loop_vars`. `loop_vars` is a
2498 (possibly nested) tuple, namedtuple or list of tensors that is passed to both
2499 `cond` and `body`. `cond` and `body` both take as many arguments as there are
2500 `loop_vars`.
2501
2502 In addition to regular Tensors or IndexedSlices, the body may accept and
2503 return TensorArray objects. The flows of the TensorArray objects will
2504 be appropriately forwarded between loops and during gradient calculations.
2505
2506 Note that `while_loop` calls `cond` and `body` *exactly once* (inside the
2507 call to `while_loop`, and not at all during `Session.run()`). `while_loop`
2508 stitches together the graph fragments created during the `cond` and `body`
2509 calls with some additional graph nodes to create the graph flow that
2510 repeats `body` until `cond` returns false.
2511
2512 For correctness, `tf.while_loop()` strictly enforces shape invariants for
2513 the loop variables. A shape invariant is a (possibly partial) shape that
2514 is unchanged across the iterations of the loop. An error will be raised
2515 if the shape of a loop variable after an iteration is determined to be more
2516 general than or incompatible with its shape invariant. For example, a shape
2517 of [11, None] is more general than a shape of [11, 17], and [11, 21] is not
2518 compatible with [11, 17]. By default (if the argument `shape_invariants` is
2519 not specified), it is assumed that the initial shape of each tensor in
2520 `loop_vars` is the same in every iteration. The `shape_invariants` argument
2521 allows the caller to specify a less specific shape invariant for each loop
2522 variable, which is needed if the shape varies between iterations. The
2523 `tf.Tensor.set_shape`
2524 function may also be used in the `body` function to indicate that
2525 the output loop variable has a particular shape. The shape invariant for
2526 SparseTensor and IndexedSlices are treated specially as follows:
2527
2528 a) If a loop variable is a SparseTensor, the shape invariant must be
2529 TensorShape([r]) where r is the rank of the dense tensor represented
2530 by the sparse tensor. It means the shapes of the three tensors of the
2531 SparseTensor are ([None], [None, r], [r]). NOTE: The shape invariant here
2532 is the shape of the SparseTensor.dense_shape property. It must be the shape of
2533 a vector.
2534
2535 b) If a loop variable is an IndexedSlices, the shape invariant must be
2536 a shape invariant of the values tensor of the IndexedSlices. It means
2537 the shapes of the three tensors of the IndexedSlices are (shape, [shape[0]],
2538 [shape.ndims]).
2539
2540 `while_loop` implements non-strict semantics, enabling multiple iterations

Callers 1

while_loop_v2Function · 0.70

Calls 10

BuildLoopMethod · 0.95
executing_eagerlyMethod · 0.80
add_to_collectionMethod · 0.80
condFunction · 0.70
bodyFunction · 0.70
WhileContextClass · 0.70
while_loopMethod · 0.45
name_scopeMethod · 0.45
numpyMethod · 0.45
constantMethod · 0.45

Tested by

no test coverage detected