Builds a training loop that executes a fixed number of iterations. The set of loop-carried tensors correspond to `inputs`. `body` must be a function that takes and returns the values of the loop-carried tensors. Args: n: the number of loop iterations body: a Python function that bu
(n, body, inputs=None, infeed_queue=None, name=None)
| 179 | |
| 180 | |
| 181 | def repeat(n, body, inputs=None, infeed_queue=None, name=None): |
| 182 | """Builds a training loop that executes a fixed number of iterations. |
| 183 | |
| 184 | The set of loop-carried tensors correspond to `inputs`. |
| 185 | `body` must be a function that takes and returns the values of the |
| 186 | loop-carried tensors. |
| 187 | |
| 188 | Args: |
| 189 | n: the number of loop iterations |
| 190 | body: a Python function that builds the loop body. |
| 191 | inputs: a list of initial values passed into the training loop or |
| 192 | None (equivalent to an empty list). |
| 193 | infeed_queue: if not None, the infeed queue from which to append a tuple |
| 194 | of arguments as inputs to condition. |
| 195 | name: (Deprecated) Does nothing. |
| 196 | Returns: |
| 197 | The final values of the loop-carried tensors. |
| 198 | Raises: |
| 199 | ValueError: if there is a type error. |
| 200 | """ |
| 201 | def _convert_to_list(xs): |
| 202 | if not isinstance(xs, (list, tuple)): |
| 203 | return [xs] |
| 204 | else: |
| 205 | return list(xs) |
| 206 | |
| 207 | def cond(i, *args): |
| 208 | del args |
| 209 | return i < n |
| 210 | |
| 211 | def body_wrapper(i, *args): |
| 212 | return [i + 1] + _convert_to_list(body(*args)) |
| 213 | |
| 214 | inputs = [0] if inputs is None else [0] + _convert_to_list(inputs) |
| 215 | outputs = while_loop( |
| 216 | cond, body_wrapper, inputs=inputs, infeed_queue=infeed_queue, name=name) |
| 217 | outputs = _convert_to_list(outputs) |
| 218 | if len(outputs) == 1: |
| 219 | # Returns the Op rather than an empty list. |
| 220 | return outputs[0].op |
| 221 | else: |
| 222 | return outputs[1:] |
nothing calls this directly
no test coverage detected