The context for the loop construct.
| 1402 | # not only conditionals and loops but also control dependency and |
| 1403 | # subgraphs. |
| 1404 | class WhileContext(ControlFlowContext): |
| 1405 | """The context for the loop construct.""" |
| 1406 | |
| 1407 | def __init__(self, |
| 1408 | maximum_iterations=None, |
| 1409 | parallel_iterations=10, |
| 1410 | back_prop=True, |
| 1411 | swap_memory=False, |
| 1412 | name="while_context", |
| 1413 | grad_state=None, |
| 1414 | context_def=None, |
| 1415 | import_scope=None): |
| 1416 | """"Creates a `WhileContext`. |
| 1417 | |
| 1418 | Args: |
| 1419 | maximum_iterations: Optional upper bound on number of loop iterations. |
| 1420 | parallel_iterations: The number of iterations allowed to run in parallel. |
| 1421 | back_prop: Whether backprop is enabled for this while loop. |
| 1422 | swap_memory: Whether GPU-CPU memory swap is enabled for this loop. |
| 1423 | name: Optional name prefix for the returned tensors. |
| 1424 | grad_state: The gradient loop state. |
| 1425 | context_def: Optional `WhileContextDef` protocol buffer to initialize the |
| 1426 | `Whilecontext` python object from. |
| 1427 | import_scope: Optional `string`. Name scope to add. Only used when |
| 1428 | initialing from protocol buffer. |
| 1429 | """ |
| 1430 | if context_def: |
| 1431 | self._init_from_proto(context_def, import_scope=import_scope) |
| 1432 | else: |
| 1433 | ControlFlowContext.__init__(self) |
| 1434 | self._init_from_args(maximum_iterations, parallel_iterations, back_prop, |
| 1435 | swap_memory, name) |
| 1436 | # The gradient loop state. |
| 1437 | self._grad_state = grad_state |
| 1438 | |
| 1439 | def _init_from_args(self, maximum_iterations, parallel_iterations, back_prop, |
| 1440 | swap_memory, name): |
| 1441 | """Creates a new `WhileContext` from arguments. |
| 1442 | |
| 1443 | Args: |
| 1444 | maximum_iterations: Optional upper bound on number of loop iterations. |
| 1445 | parallel_iterations: The number of iterations allowed to run in parallel. |
| 1446 | back_prop: Whether backprop is enabled for this while loop. |
| 1447 | swap_memory: Whether GPU-CPU memory swap is enabled for this loop. |
| 1448 | name: Optional name prefix for the returned tensors. |
| 1449 | |
| 1450 | Raises: |
| 1451 | ValueError: If `parallel_iterations` has invalid value. |
| 1452 | """ |
| 1453 | if not isinstance(parallel_iterations, int) or (parallel_iterations <= 0): |
| 1454 | raise ValueError("`parallel_iterations` must be a positive integer: " |
| 1455 | "%s" % parallel_iterations) |
| 1456 | self._name = ops.get_default_graph().unique_name(name) |
| 1457 | self._maximum_iterations = maximum_iterations |
| 1458 | self._parallel_iterations = parallel_iterations |
| 1459 | self._back_prop = back_prop |
| 1460 | self._swap_memory = swap_memory |
| 1461 | # We use this node to control constants created by the pred lambda. |
no outgoing calls
no test coverage detected