Wraps a TF 1.X function and returns an eager-compatible function. All functions wrapped in the same `WrappedGraph` will have access to the same graph (`tf.compat.v1.get_default_graph` to get the graph object within a function, or `WrappedGraph.graph` to get the graph outside a funct
(self, fn, signature, name=None)
| 450 | return self._variable_holder.variables |
| 451 | |
| 452 | def wrap_function(self, fn, signature, name=None): |
| 453 | """Wraps a TF 1.X function and returns an eager-compatible function. |
| 454 | |
| 455 | All functions wrapped in the same `WrappedGraph` will have access to the |
| 456 | same graph (`tf.compat.v1.get_default_graph` to get the graph object |
| 457 | within a function, or `WrappedGraph.graph` to get the graph outside a |
| 458 | function). Variables created within the function will be added to the |
| 459 | `variables` list. |
| 460 | |
| 461 | Function inputs: All inputs to the function must be tensors (nested ok), |
| 462 | with their shapes and dtypes defined in the `signature` argument. |
| 463 | |
| 464 | Function outputs: |
| 465 | |
| 466 | * The 1.X function may return tensors, variables, and ops. The wrapped |
| 467 | eager-compatible function will always return tensors in the same nested |
| 468 | structure. |
| 469 | * Variables are replaced with a tensor containing the latest read values. |
| 470 | * Returned ops are executed, and replaced with None. |
| 471 | * The order of op execution and variable reads in the return is |
| 472 | nondeterministic. For example: |
| 473 | |
| 474 | ``` |
| 475 | def update_var(x): |
| 476 | v = tf.Variable(0) |
| 477 | op = tf.compat.v1.assign(v, x).op |
| 478 | return v, op |
| 479 | |
| 480 | g = WrappedGraph() |
| 481 | fn = g.wrap_function(update_var) |
| 482 | read_value, _ = fn(tf.constant(3)) |
| 483 | print(read_value.numpy()) # could be 0 or 3 |
| 484 | print(g.variables[0].numpy()) # always 3 |
| 485 | ``` |
| 486 | |
| 487 | To ensure that ops in the function are executed (e.g. ops added to the |
| 488 | `tf.GraphKeys.UPDATE_OPS` collection), include them in the function returns. |
| 489 | |
| 490 | Args: |
| 491 | fn: a 1.X tensorflow function. |
| 492 | signature: a possibly nested sequence of `TensorSpecs` specifying the |
| 493 | shapes and dtypes of the arguments. |
| 494 | name: an optional string name for the function. The function will be saved |
| 495 | with key `name` in the `functions` dictionary. |
| 496 | |
| 497 | Returns: |
| 498 | An eager-compatible function. |
| 499 | """ |
| 500 | return self._wrap_function(fn, signature=signature, name=name) |
| 501 | |
| 502 | def _wrap_function(self, |
| 503 | fn, |