def test_local_variable(self):
     with self.cached_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.variables_initializer(variables).run()
         self.assertAllEqual(set([value0, value1]),
                             set(sess.run(variables)))
Пример #2
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 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)
Пример #3
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)
Пример #4
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)
Пример #5
0
    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.VariableV1(1.0)
        in1 = variables_lib.local_variable(2.0)
        fake_table = variables.VariableV1(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
Пример #6
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)
Пример #7
0
 def test_train_loss(self):
     with ops.Graph().as_default() as g, self.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)
Пример #8
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)