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,
maximum_iterations=None,
name=None)
| 2301 | # pylint: disable=redefined-outer-name |
| 2302 | @tf_export("while_loop", v1=[]) |
| 2303 | def while_loop_v2(cond, |
| 2304 | body, |
| 2305 | loop_vars, |
| 2306 | shape_invariants=None, |
| 2307 | parallel_iterations=10, |
| 2308 | back_prop=True, |
| 2309 | swap_memory=False, |
| 2310 | maximum_iterations=None, |
| 2311 | name=None): |
| 2312 | """Repeat `body` while the condition `cond` is true. |
| 2313 | |
| 2314 | `cond` is a callable returning a boolean scalar tensor. `body` is a callable |
| 2315 | returning a (possibly nested) tuple, namedtuple or list of tensors of the same |
| 2316 | arity (length and structure) and types as `loop_vars`. `loop_vars` is a |
| 2317 | (possibly nested) tuple, namedtuple or list of tensors that is passed to both |
| 2318 | `cond` and `body`. `cond` and `body` both take as many arguments as there are |
| 2319 | `loop_vars`. |
| 2320 | |
| 2321 | In addition to regular Tensors or IndexedSlices, the body may accept and |
| 2322 | return TensorArray objects. The flows of the TensorArray objects will |
| 2323 | be appropriately forwarded between loops and during gradient calculations. |
| 2324 | |
| 2325 | Note that `while_loop` calls `cond` and `body` *exactly once* (inside the |
| 2326 | call to `while_loop`, and not at all during `Session.run()`). `while_loop` |
| 2327 | stitches together the graph fragments created during the `cond` and `body` |
| 2328 | calls with some additional graph nodes to create the graph flow that |
| 2329 | repeats `body` until `cond` returns false. |
| 2330 | |
| 2331 | For correctness, `tf.while_loop()` strictly enforces shape invariants for |
| 2332 | the loop variables. A shape invariant is a (possibly partial) shape that |
| 2333 | is unchanged across the iterations of the loop. An error will be raised |
| 2334 | if the shape of a loop variable after an iteration is determined to be more |
| 2335 | general than or incompatible with its shape invariant. For example, a shape |
| 2336 | of [11, None] is more general than a shape of [11, 17], and [11, 21] is not |
| 2337 | compatible with [11, 17]. By default (if the argument `shape_invariants` is |
| 2338 | not specified), it is assumed that the initial shape of each tensor in |
| 2339 | `loop_vars` is the same in every iteration. The `shape_invariants` argument |
| 2340 | allows the caller to specify a less specific shape invariant for each loop |
| 2341 | variable, which is needed if the shape varies between iterations. The |
| 2342 | `tf.Tensor.set_shape` |
| 2343 | function may also be used in the `body` function to indicate that |
| 2344 | the output loop variable has a particular shape. The shape invariant for |
| 2345 | SparseTensor and IndexedSlices are treated specially as follows: |
| 2346 | |
| 2347 | a) If a loop variable is a SparseTensor, the shape invariant must be |
| 2348 | TensorShape([r]) where r is the rank of the dense tensor represented |
| 2349 | by the sparse tensor. It means the shapes of the three tensors of the |
| 2350 | SparseTensor are ([None], [None, r], [r]). NOTE: The shape invariant here |
| 2351 | is the shape of the SparseTensor.dense_shape property. It must be the shape of |
| 2352 | a vector. |
| 2353 | |
| 2354 | b) If a loop variable is an IndexedSlices, the shape invariant must be |
| 2355 | a shape invariant of the values tensor of the IndexedSlices. It means |
| 2356 | the shapes of the three tensors of the IndexedSlices are (shape, [shape[0]], |
| 2357 | [shape.ndims]). |
| 2358 | |
| 2359 | `while_loop` implements non-strict semantics, enabling multiple iterations |
| 2360 | to run in parallel. The maximum number of parallel iterations can be |