def testGetVariablesDontReturnsTransients(self):
   with self.test_session():
     with variable_scope.variable_scope('A'):
       variables_lib2.local_variable(0)
     with variable_scope.variable_scope('B'):
       variables_lib2.local_variable(0)
     self.assertEquals([], variables_lib2.get_variables('A'))
     self.assertEquals([], variables_lib2.get_variables('B'))
 def testGetLocalVariablesReturnsTransients(self):
   with self.test_session():
     with variable_scope.variable_scope('A'):
       a = variables_lib2.local_variable(0)
     with variable_scope.variable_scope('B'):
       b = variables_lib2.local_variable(0)
     self.assertEquals([a], variables_lib2.get_local_variables('A'))
     self.assertEquals([b], variables_lib2.get_local_variables('B'))
Exemple #3
0
 def test_local_variable(self):
   with self.test_session() as sess:
     self.assertEquals([], variables_lib.local_variables())
     value0 = 42
     variables_lib2.local_variable(value0)
     value1 = 43
     variables_lib2.local_variable(value1)
     variables = variables_lib.local_variables()
     self.assertEquals(2, len(variables))
     self.assertRaises(errors_impl.OpError, sess.run, variables)
     variables_lib.initialize_variables(variables).run()
     self.assertAllEqual(set([value0, value1]), set(sess.run(variables)))
Exemple #4
0
    def testEvalOpAndFinalOp(self):
        checkpoint_dir = os.path.join(self.get_temp_dir(),
                                      'eval_ops_and_final_ops')

        # Train a model for a single step to get a checkpoint.
        self._train_model(checkpoint_dir, num_steps=1)
        checkpoint_path = evaluation.wait_for_new_checkpoint(checkpoint_dir)

        # Create the model so we have something to restore.
        inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
        logistic_classifier(inputs)

        num_evals = 5
        final_increment = 9.0

        my_var = variables.local_variable(0.0, name='MyVar')
        eval_ops = state_ops.assign_add(my_var, 1.0)
        final_ops = array_ops.identity(my_var) + final_increment

        final_ops_values = evaluation.evaluate_once(
            checkpoint_path=checkpoint_path,
            eval_ops=eval_ops,
            final_ops={'value': final_ops},
            hooks=[
                evaluation.StopAfterNEvalsHook(num_evals),
            ])
        self.assertEqual(final_ops_values['value'],
                         num_evals + final_increment)
Exemple #5
0
    def testEvaluateWithEvalFeedDict(self):
        # Create a checkpoint.
        checkpoint_dir = os.path.join(self.get_temp_dir(),
                                      'evaluate_with_eval_feed_dict')
        self._train_model(checkpoint_dir, num_steps=1)

        # We need a variable that that the saver will try to restore.
        variables.get_or_create_global_step()

        # Create a variable and an eval op that increments it with a placeholder.
        my_var = variables.local_variable(0.0, name='my_var')
        increment = array_ops.placeholder(dtype=dtypes.float32)
        eval_ops = state_ops.assign_add(my_var, increment)

        increment_value = 3
        num_evals = 5
        expected_value = increment_value * num_evals
        final_values = evaluation.evaluate_repeatedly(
            checkpoint_dir=checkpoint_dir,
            eval_ops=eval_ops,
            feed_dict={increment: 3},
            final_ops={'my_var': array_ops.identity(my_var)},
            hooks=[
                evaluation.StopAfterNEvalsHook(num_evals),
            ],
            max_number_of_evaluations=1)
        self.assertEqual(final_values['my_var'], expected_value)
 def testLocalVariableNameAndShape(self):
   with self.test_session():
     with variable_scope.variable_scope('A'):
       a = variables_lib2.local_variable([1, 1, 1, 1, 1], name='a')
       self.assertEquals(a.op.name, 'A/a')
       self.assertListEqual(a.get_shape().as_list(), [5])
       self.assertListEqual([a], variables_lib2.get_local_variables())
  def testEvaluateWithEvalFeedDict(self):
    # Create a checkpoint.
    checkpoint_dir = os.path.join(self.get_temp_dir(),
                                  'evaluate_with_eval_feed_dict')
    self._train_model(checkpoint_dir, num_steps=1)

    # We need a variable that that the saver will try to restore.
    variables.get_or_create_global_step()

    # Create a variable and an eval op that increments it with a placeholder.
    my_var = variables.local_variable(0.0, name='my_var')
    increment = array_ops.placeholder(dtype=dtypes.float32)
    eval_ops = state_ops.assign_add(my_var, increment)

    increment_value = 3
    num_evals = 5
    expected_value = increment_value * num_evals
    final_values = evaluation.evaluate_repeatedly(
        checkpoint_dir=checkpoint_dir,
        eval_ops=eval_ops,
        feed_dict={increment: 3},
        final_ops={'my_var': array_ops.identity(my_var)},
        hooks=[evaluation.StopAfterNEvalsHook(num_evals),],
        max_number_of_evaluations=1)
    self.assertEqual(final_values['my_var'], expected_value)
  def testEvalOpAndFinalOp(self):
    checkpoint_dir = os.path.join(self.get_temp_dir(), 'eval_ops_and_final_ops')

    # Train a model for a single step to get a checkpoint.
    self._train_model(checkpoint_dir, num_steps=1)
    checkpoint_path = evaluation.wait_for_new_checkpoint(checkpoint_dir)

    # Create the model so we have something to restore.
    inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
    logistic_classifier(inputs)

    num_evals = 5
    final_increment = 9.0

    my_var = variables.local_variable(0.0, name='MyVar')
    eval_ops = state_ops.assign_add(my_var, 1.0)
    final_ops = array_ops.identity(my_var) + final_increment

    final_ops_values = evaluation.evaluate_once(
        checkpoint_path=checkpoint_path,
        eval_ops=eval_ops,
        final_ops={'value': final_ops},
        hooks=[
            evaluation.StopAfterNEvalsHook(num_evals),
        ])
    self.assertEqual(final_ops_values['value'], num_evals + final_increment)
Exemple #9
0
    def testTrainWithLocalVariable(self):
        with ops.Graph().as_default():
            random_seed.set_random_seed(0)
            tf_inputs = constant_op.constant(self._inputs,
                                             dtype=dtypes.float32)
            tf_labels = constant_op.constant(self._labels,
                                             dtype=dtypes.float32)

            local_multiplier = variables_lib.local_variable(1.0)

            tf_predictions = logistic_classifier(tf_inputs) * local_multiplier
            losses.log_loss(tf_labels, tf_predictions)
            total_loss = losses.get_total_loss()
            optimizer = gradient_descent.GradientDescentOptimizer(
                learning_rate=1.0)
            train_op = training.create_train_op(total_loss, optimizer)

            loss = training.train(
                train_op,
                None,
                hooks=[basic_session_run_hooks.StopAtStepHook(num_steps=300)],
                save_summaries_steps=None,
                save_checkpoint_secs=None)
            self.assertIsNotNone(loss)
            self.assertLess(loss, .015)
Exemple #10
0
    def testTrainWithLocalVariable(self):
        logdir = os.path.join(tempfile.mkdtemp(prefix=self.get_temp_dir()),
                              'tmp_logs')
        with ops.Graph().as_default():
            random_seed.set_random_seed(0)
            tf_inputs = constant_op.constant(self._inputs,
                                             dtype=dtypes.float32)
            tf_labels = constant_op.constant(self._labels,
                                             dtype=dtypes.float32)

            local_multiplier = variables_lib2.local_variable(1.0)

            tf_predictions = LogisticClassifier(tf_inputs) * local_multiplier
            loss_ops.log_loss(tf_predictions, tf_labels)
            total_loss = loss_ops.get_total_loss()

            optimizer = gradient_descent.GradientDescentOptimizer(
                learning_rate=1.0)

            train_op = learning.create_train_op(total_loss, optimizer)

            loss = learning.train(train_op,
                                  logdir,
                                  number_of_steps=300,
                                  log_every_n_steps=10)
            self.assertIsNotNone(loss)
            self.assertLess(loss, .015)
Exemple #11
0
  def testOnlyFinalOp(self):
    checkpoint_dir = os.path.join(self.get_temp_dir(), 'only_final_ops')

    # Train a model for a single step to get a checkpoint.
    self._train_model(checkpoint_dir, num_steps=1)
    checkpoint_path = evaluation.wait_for_new_checkpoint(checkpoint_dir)

    # Create the model so we have something to restore.
    inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
    logistic_classifier(inputs)

    final_increment = 9.0

    my_var = variables.local_variable(0.0, name='MyVar')
    final_ops = array_ops.identity(my_var) + final_increment

    final_ops_values = evaluation.evaluate_once(
        checkpoint_path=checkpoint_path, final_ops={'value': final_ops})
    self.assertEqual(final_ops_values['value'], final_increment)
    def _build_inference_graph(self):
        """Build simple inference graph.

    This includes a regular variable, local variable, and fake table.

    Returns:
      Tuple of 3 `Tensor` objects, 2 input and 1 output.
    """
        variables_lib.create_global_step()
        in0 = variables.Variable(1.0)
        in1 = variables_lib.local_variable(2.0)
        fake_table = variables.Variable(3.0,
                                        trainable=False,
                                        collections=['fake_tables'],
                                        name='fake_table_var')
        in0.graph.add_to_collections([ops.GraphKeys.TABLE_INITIALIZERS],
                                     fake_table.initializer)
        out = in0 + in1 + fake_table
        return in0, in1, out
Exemple #13
0
    def testOnlyFinalOp(self):
        checkpoint_dir = os.path.join(self.get_temp_dir(), 'only_final_ops')

        # Train a model for a single step to get a checkpoint.
        self._train_model(checkpoint_dir, num_steps=1)
        checkpoint_path = evaluation.wait_for_new_checkpoint(checkpoint_dir)

        # Create the model so we have something to restore.
        inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
        logistic_classifier(inputs)

        final_increment = 9.0

        my_var = variables.local_variable(0.0, name='MyVar')
        final_ops = array_ops.identity(my_var) + final_increment

        final_ops_values = evaluation.evaluate_once(
            checkpoint_path=checkpoint_path, final_ops={'value': final_ops})
        self.assertEqual(final_ops_values['value'], final_increment)
Exemple #14
0
def get_or_create_eval_step():
    """Gets or creates the eval step `Tensor`.

  Returns:
    A `Tensor` representing a counter for the evaluation step.

  Raises:
    ValueError: If multiple `Tensors` have been added to the
      `tf.GraphKeys.EVAL_STEP` collection.
  """
    graph = ops.get_default_graph()
    eval_steps = graph.get_collection(ops.GraphKeys.EVAL_STEP)
    if len(eval_steps) == 1:
        return eval_steps[0]
    elif len(eval_steps) > 1:
        raise ValueError('Multiple tensors added to tf.GraphKeys.EVAL_STEP')
    else:
        counter = variables.local_variable(0.0, name='eval_step')
        graph.add_to_collection(ops.GraphKeys.EVAL_STEP, counter)
        return counter
 def test_train_loss(self):
     with ops.Graph().as_default() as g, self.test_session(g):
         variables_lib.create_global_step()
         loss_var = variables_lib.local_variable(10.0)
         train_op = control_flow_ops.group(
             state_ops.assign_add(variables_lib.get_global_step(), 1),
             state_ops.assign_add(loss_var, -1.0))
         self._assert_summaries(self._output_dir)
         self._assert_ckpt(self._output_dir, False)
         loss = learn.graph_actions.train(g,
                                          output_dir=self._output_dir,
                                          train_op=train_op,
                                          loss_op=loss_var.value(),
                                          steps=6)
         # TODO (ebrevdo,ptucker,ispir): this meta_graph_def lacks the id:1119 gh:1120
         # SaverDef, so we can't add it to the summary assertion test below.
         # meta_graph_def = meta_graph.create_meta_graph_def()
         self.assertEqual(4.0, loss)
         self._assert_summaries(self._output_dir, expected_graphs=[g])
         self._assert_ckpt(self._output_dir, True)
Exemple #16
0
def get_or_create_eval_step():
  """Gets or creates the eval step `Tensor`.

  Returns:
    A `Tensor` representing a counter for the evaluation step.

  Raises:
    ValueError: If multiple `Tensors` have been added to the
      `tf.GraphKeys.EVAL_STEP` collection.
  """
  graph = ops.get_default_graph()
  eval_steps = graph.get_collection(ops.GraphKeys.EVAL_STEP)
  if len(eval_steps) == 1:
    return eval_steps[0]
  elif len(eval_steps) > 1:
    raise ValueError('Multiple tensors added to tf.GraphKeys.EVAL_STEP')
  else:
    counter = variables.local_variable(0.0, name='eval_step')
    graph.add_to_collection(ops.GraphKeys.EVAL_STEP, counter)
    return counter
  def _build_inference_graph(self):
    """Build simple inference graph.

    This includes a regular variable, local variable, and fake table.

    Returns:
      Tuple of 3 `Tensor` objects, 2 input and 1 output.
    """
    variables_lib.create_global_step()
    in0 = variables.Variable(1.0)
    in1 = variables_lib.local_variable(2.0)
    fake_table = variables.Variable(
        3.0,
        trainable=False,
        collections=['fake_tables'],
        name='fake_table_var')
    in0.graph.add_to_collections([ops.GraphKeys.TABLE_INITIALIZERS],
                                 fake_table.initializer)
    out = in0 + in1 + fake_table
    return in0, in1, out
 def test_train_loss(self):
   with ops.Graph().as_default() as g, self.test_session(g):
     variables_lib.create_global_step()
     loss_var = variables_lib.local_variable(10.0)
     train_op = control_flow_ops.group(
         state_ops.assign_add(variables_lib.get_global_step(), 1),
         state_ops.assign_add(loss_var, -1.0))
     self._assert_summaries(self._output_dir)
     self._assert_ckpt(self._output_dir, False)
     loss = learn.graph_actions.train(
         g,
         output_dir=self._output_dir,
         train_op=train_op,
         loss_op=loss_var.value(),
         steps=6)
     # TODO(ebrevdo,ptucker,ispir): this meta_graph_def lacks the
     # SaverDef, so we can't add it to the summary assertion test below.
     # meta_graph_def = meta_graph.create_meta_graph_def()
     self.assertEqual(4.0, loss)
     self._assert_summaries(self._output_dir, expected_graphs=[g])
     self._assert_ckpt(self._output_dir, True)
  def testTrainWithLocalVariable(self):
    logdir = os.path.join(
        tempfile.mkdtemp(prefix=self.get_temp_dir()), 'tmp_logs')
    with ops.Graph().as_default():
      random_seed.set_random_seed(0)
      tf_inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
      tf_labels = constant_op.constant(self._labels, dtype=dtypes.float32)

      local_multiplier = variables_lib2.local_variable(1.0)

      tf_predictions = LogisticClassifier(tf_inputs) * local_multiplier
      loss_ops.log_loss(tf_predictions, tf_labels)
      total_loss = loss_ops.get_total_loss()

      optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=1.0)

      train_op = learning.create_train_op(total_loss, optimizer)

      loss = learning.train(
          train_op, logdir, number_of_steps=300, log_every_n_steps=10)
      self.assertIsNotNone(loss)
      self.assertLess(loss, .015)
Exemple #20
0
  def testTrainWithLocalVariable(self):
    with ops.Graph().as_default():
      random_seed.set_random_seed(0)
      tf_inputs = constant_op.constant(self._inputs, dtype=dtypes.float32)
      tf_labels = constant_op.constant(self._labels, dtype=dtypes.float32)

      local_multiplier = variables_lib.local_variable(1.0)

      tf_predictions = logistic_classifier(tf_inputs) * local_multiplier
      losses.log_loss(tf_labels, tf_predictions)
      total_loss = losses.get_total_loss()
      optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=1.0)
      train_op = training.create_train_op(total_loss, optimizer)

      loss = training.train(
          train_op,
          None,
          hooks=[basic_session_run_hooks.StopAtStepHook(num_steps=300)],
          save_summaries_steps=None,
          save_checkpoint_secs=None)
      self.assertIsNotNone(loss)
      self.assertLess(loss, .015)
Exemple #21
0
 def test_train_loss(self):
     with ops.Graph().as_default() as g, self.test_session(g):
         variables_lib.create_global_step()
         loss_var = variables_lib.local_variable(10.0)
         train_op = control_flow_ops.group(
             state_ops.assign_add(variables_lib.get_global_step(), 1),
             state_ops.assign_add(loss_var, -1.0))
         writer = learn.graph_actions.get_summary_writer(self._output_dir)
         self._assert_summaries(self._output_dir, writer)
         self._assert_ckpt(self._output_dir, False)
         loss = learn.graph_actions._monitored_train(  # pylint: disable=protected-access
             g,
             output_dir=self._output_dir,
             train_op=train_op,
             loss_op=loss_var.value(),
             steps=6)
         self.assertEqual(4.0, loss)
         self._assert_summaries(self._output_dir,
                                writer,
                                expected_graphs=[g],
                                expected_meta_graphs=None)
         self._assert_ckpt(self._output_dir, True)
 def test_train_loss(self):
   with ops.Graph().as_default() as g, self.test_session(g):
     variables_lib.create_global_step()
     loss_var = variables_lib.local_variable(10.0)
     train_op = control_flow_ops.group(
         state_ops.assign_add(variables_lib.get_global_step(), 1),
         state_ops.assign_add(loss_var, -1.0))
     writer = learn.graph_actions.get_summary_writer(self._output_dir)
     self._assert_summaries(self._output_dir, writer)
     self._assert_ckpt(self._output_dir, False)
     loss = learn.graph_actions._monitored_train(  # pylint: disable=protected-access
         g,
         output_dir=self._output_dir,
         train_op=train_op,
         loss_op=loss_var.value(),
         steps=6)
     self.assertEqual(4.0, loss)
     self._assert_summaries(
         self._output_dir,
         writer,
         expected_graphs=[g],
         expected_meta_graphs=None)
     self._assert_ckpt(self._output_dir, True)
 def testLocalVariableNotInVariablesToRestore(self):
   with self.test_session():
     with variable_scope.variable_scope('A'):
       a = variables_lib2.local_variable(0)
       self.assertFalse(a in variables_lib2.get_variables_to_restore())
       self.assertTrue(a in variables_lib.local_variables())
 def testLocalVariableNotInAllVariables(self):
   with self.test_session():
     with variable_scope.variable_scope('A'):
       a = variables_lib2.local_variable(0)
       self.assertFalse(a in variables_lib.global_variables())
       self.assertTrue(a in variables_lib.local_variables())
 def testInitializedVariableValue(self):
   with self.test_session() as sess:
     a = variables_lib2.local_variable([0, 0, 0, 0, 0], name='a')
     sess.run(variables_lib.local_variables_initializer())
     self.assertAllEqual(a.eval(), [0] * 5)