Esempio n. 1
0
 def __get__(self, instance, owner):
   """Makes it possible to defun instance methods."""
   del owner
   # `instance` here is the instance that this `Function` was accessed through
   # e.g., for
   #
   #   class Foo(object):
   #
   #     @function.defun
   #     def bar(self):
   #       ...
   #
   #   foo = Foo()
   #   foo.bar()  # `foo.bar` is a `Function` instance
   #
   # then `instance` will be `foo` (and `owner` will be `Foo`).  We create a
   # new instance of `Function` here to allow different instances each
   # to create variables once, thereby allowing methods to be decorated with
   # tf.function. Keeps a cache to avoid retracing the function every time the
   # descriptor is accessed.
   if instance not in self._descriptor_cache:
     if instance is None:
       return self
     self._descriptor_cache[instance] = (
         function_lib.class_method_to_instance_method(self, instance))
   return self._descriptor_cache[instance]
Esempio n. 2
0
 def __get__(self, instance, owner):
   """Makes it possible to defun instance methods."""
   del owner
   # `instance` here is the instance that this `Function` was accessed through
   # e.g., for
   #
   #   class Foo(object):
   #
   #     @function.defun
   #     def bar(self):
   #       ...
   #
   #   foo = Foo()
   #   foo.bar()  # `foo.bar` is a `Function` instance
   #
   # then `instance` will be `foo` (and `owner` will be `Foo`).  We create a
   # new instance of `Function` here to allow different instances each
   # to create variables once, thereby allowing methods to be decorated with
   # tf.function. Keeps a cache to avoid retracing the function every time the
   # descriptor is accessed.
   if instance not in self._descriptor_cache:
     if instance is None:
       return self
     self._descriptor_cache[instance] = (
         function_lib.class_method_to_instance_method(self, instance))
   return self._descriptor_cache[instance]