Example #1
0
    def nested_template():
      nested1 = template.make_template_internal(
          "nested",
          variable_scoped_function_no_return_value,
          create_graph_function_=True)
      nested2 = template.make_template_internal(
          "nested",
          variable_scoped_function_no_return_value,
          create_graph_function_=True)
      nested1()
      nested2()
      v1 = nested1.variables
      v2 = nested2.variables

      # nested1 and nested2 should not share variables
      self.assertNotEqual(v1, v2)

      # Variables created by nested1 should be isolated from variables
      # created by nested2.
      self.assertEqual(nested1.variables, v1)
      self.assertEqual(nested2.variables, v2)
      self.assertEqual(nested1.trainable_variables, v1)
      self.assertEqual(nested2.trainable_variables, v2)
      self.assertEqual(len(nested1.non_trainable_variables), 0)
      self.assertEqual(len(nested2.non_trainable_variables), 0)
    def test_make_template_with_defun(self):
        def variable_scoped_function_no_return_value(scope_name):
            # defun cannot compile functions that return non-Tensor objects
            with variable_scope.variable_scope(scope_name):
                _ = variable_scope.get_variable(
                    "dummy",
                    shape=[1],
                    initializer=init_ops.zeros_initializer())

        tmpl = template.make_template_internal(
            "s1",
            variable_scoped_function_no_return_value,
            create_graph_function_=True,
            scope_name="test")

        # The first invocation of tmpl1 creates variables, the second should
        # be executed as a graph function.
        tmpl()
        v1 = tmpl.variables
        tmpl()
        v2 = tmpl.variables

        self.assertEqual(len(v1), len(v2))
        for v, w in zip(v1, v2):
            self.assertIs(v, w)
        self.assertEqual("s1/test/dummy:0", v1[0].name)
Example #3
0
  def test_graph_function_no_name(self):
    with context.eager_mode():

      def f(_, y):
        return y + 1

      partial = functools.partial(f, 1.0)
      tmpl = template.make_template_internal(
          "a", partial, create_graph_function_=True)
      self.assertAllEqual(tmpl(ops.convert_to_tensor(1.0)), 2.0)
        def nested_template():
            nested1 = template.make_template_internal(
                "nested",
                variable_scoped_function_no_return_value,
                create_graph_function_=True)
            nested2 = template.make_template_internal(
                "nested",
                variable_scoped_function_no_return_value,
                create_graph_function_=True)
            nested1()
            nested2()
            v1 = nested1.variables
            v2 = nested2.variables

            self.assertEqual(len(v1), 1)
            self.assertEqual(len(v2), 1)

            # nested1 and nested2 should not share variables
            self.assertIsNot(v1[0], v2[0])
            self.assertIs(nested1.trainable_variables[0], v1[0])
            self.assertIs(nested2.trainable_variables[0], v2[0])
            self.assertEqual(len(nested1.non_trainable_variables), 0)
            self.assertEqual(len(nested2.non_trainable_variables), 0)
  def test_make_template_with_defun(self):

    def variable_scoped_function_no_return_value(scope_name):
      # defun cannot compile functions that return non-Tensor objects
      with variable_scope.variable_scope(scope_name):
        _ = variable_scope.get_variable(
            "dummy", shape=[1], initializer=init_ops.zeros_initializer())

    tmpl = template.make_template_internal(
        "s1",
        variable_scoped_function_no_return_value,
        create_graph_function_=True,
        scope_name="test")

    # The first invocation of tmpl1 creates variables, the second should
    # be executed as a graph function.
    tmpl()
    v1 = tmpl.variables
    tmpl()
    v2 = tmpl.variables

    self.assertSequenceEqual(v1, v2)
    self.assertEqual("s1/test/dummy:0", v1[0].name)