(self, args, kwargs)
| 596 | self._variable_scope_context_manager = None |
| 597 | |
| 598 | def _call_func(self, args, kwargs): |
| 599 | try: |
| 600 | vars_at_start = self._template_store.variables() |
| 601 | trainable_at_start = self._template_store.trainable_variables() |
| 602 | if self._variables_created: |
| 603 | result = self._func(*args, **kwargs) |
| 604 | else: |
| 605 | # The first time we run, restore variables if necessary (via |
| 606 | # Trackable). |
| 607 | with trackable_util.capture_dependencies(template=self): |
| 608 | result = self._func(*args, **kwargs) |
| 609 | |
| 610 | if self._variables_created: |
| 611 | # Variables were previously created, implying this is not the first |
| 612 | # time the template has been called. Check to make sure that no new |
| 613 | # trainable variables were created this time around. |
| 614 | trainable_variables = self._template_store.trainable_variables() |
| 615 | # If a variable that we intend to train is created as a side effect |
| 616 | # of creating a template, then that is almost certainly an error. |
| 617 | if len(trainable_at_start) != len(trainable_variables): |
| 618 | raise ValueError( |
| 619 | "Trainable variable created when calling a template " |
| 620 | "after the first time, perhaps you used tf.Variable " |
| 621 | "when you meant tf.get_variable: %s" % list( |
| 622 | object_identity.ObjectIdentitySet(trainable_variables) - |
| 623 | object_identity.ObjectIdentitySet(trainable_at_start))) |
| 624 | |
| 625 | # Non-trainable tracking variables are a legitimate reason why a new |
| 626 | # variable would be created, but it is a relatively advanced use-case, |
| 627 | # so log it. |
| 628 | variables = self._template_store.variables() |
| 629 | if len(vars_at_start) != len(variables): |
| 630 | logging.info( |
| 631 | "New variables created when calling a template after " |
| 632 | "the first time, perhaps you used tf.Variable when you " |
| 633 | "meant tf.get_variable: %s", |
| 634 | list( |
| 635 | object_identity.ObjectIdentitySet(variables) - |
| 636 | object_identity.ObjectIdentitySet(vars_at_start))) |
| 637 | else: |
| 638 | self._variables_created = True |
| 639 | return result |
| 640 | except Exception as exc: |
| 641 | # Reraise the exception, but append the original definition to the |
| 642 | # trace. |
| 643 | args = exc.args |
| 644 | if not args: |
| 645 | arg0 = "" |
| 646 | else: |
| 647 | arg0 = args[0] |
| 648 | trace = "".join( |
| 649 | _skip_common_stack_elements(self._stacktrace, |
| 650 | traceback.format_stack())) |
| 651 | arg0 = "%s\n\noriginally defined at:\n%s" % (arg0, trace) |
| 652 | new_args = [arg0] |
| 653 | new_args.extend(args[1:]) |
| 654 | exc.args = tuple(new_args) |
| 655 | raise |
no test coverage detected