Functional form of a for statement. The loop operates on a state, which includes all symbols that are variant across loop iterations, excluding the iterate as well as the variables local to the loop. For example, given the loop below that calculates the geometric and arithmetic means or
(iter_,
extra_test,
body,
get_state,
set_state,
init_vars,
basic_symbol_names=None,
composite_symbol_names=None)
| 262 | |
| 263 | |
| 264 | def for_stmt(iter_, |
| 265 | extra_test, |
| 266 | body, |
| 267 | get_state, |
| 268 | set_state, |
| 269 | init_vars, |
| 270 | basic_symbol_names=None, |
| 271 | composite_symbol_names=None): |
| 272 | """Functional form of a for statement. |
| 273 | |
| 274 | The loop operates on a state, which includes all symbols that are |
| 275 | variant across loop iterations, excluding the iterate as well as the |
| 276 | variables local to the loop. |
| 277 | |
| 278 | For example, given the loop below that calculates the geometric and |
| 279 | arithmetic means or some numbers: |
| 280 | |
| 281 | geo_mean = 1 |
| 282 | arith_mean = 0 |
| 283 | for i in range(n): |
| 284 | a = numbers[i] |
| 285 | geo_mean *= a |
| 286 | arith_mean += a |
| 287 | |
| 288 | The state is represented by the variables geo_mean and arith_mean. The |
| 289 | argument for initial_state may contain the tuple (1, 0), the body will |
| 290 | include the arguments geo_mean and arith_mean and will return a tuple |
| 291 | representing the new values for geo_mean and respectively arith_mean. |
| 292 | |
| 293 | Args: |
| 294 | iter_: The entity being iterated over. |
| 295 | extra_test: Callable with the state as arguments, and boolean return type. |
| 296 | An additional loop condition. |
| 297 | body: Callable with the iterate and the state as arguments, and state as |
| 298 | return type. The actual loop body. |
| 299 | get_state: Additional callable which can capture additional state (such as |
| 300 | the values of composite symbols). This is only useful when staging the |
| 301 | loop. |
| 302 | set_state: Additional callable which save values captured by get_state back |
| 303 | into the Python environment. This is only useful when staging the loop. |
| 304 | init_vars: Tuple containing the initial state. |
| 305 | basic_symbol_names: Tuple containing basic loop var names. |
| 306 | composite_symbol_names: Tuple containing composite loop var names. |
| 307 | |
| 308 | Returns: |
| 309 | Tuple containing the final state. |
| 310 | """ |
| 311 | if tensor_util.is_tensor(iter_): |
| 312 | if tensors.is_range_tensor(iter_): |
| 313 | return _tf_range_for_stmt(iter_, extra_test, body, get_state, set_state, |
| 314 | init_vars, basic_symbol_names, |
| 315 | composite_symbol_names) |
| 316 | else: |
| 317 | return _known_len_tf_for_stmt(iter_, extra_test, body, get_state, |
| 318 | set_state, init_vars, basic_symbol_names, |
| 319 | composite_symbol_names) |
| 320 | |
| 321 | if isinstance(iter_, dataset_ops.DatasetV2): |
nothing calls this directly
no test coverage detected