Ejemplo n.º 1
0
    def test_simple_cluster(self):
        """Tests that the clusters are correct."""
        num_classes = 2
        graph = ops.Graph()
        with graph.as_default() as g:
            g.seed = 5
            with self.test_session() as sess:
                data = constant_op.constant(self.data, dtype=dtypes.float32)
                loss_op, scores, assignments, training_op, init_op, _ = gmm_ops.gmm(
                    data, 'random', num_classes, random_seed=self.seed)

                variables.global_variables_initializer().run()
                sess.run(init_op)
                first_loss = sess.run(loss_op)
                for _ in xrange(self.iterations):
                    sess.run(training_op)
                assignments = sess.run(assignments)
                end_loss = sess.run(loss_op)
                scores = sess.run(scores)
                self.assertEqual((self.num_examples, 1), scores.shape)
                accuracy = np.mean(
                    np.asarray(self.true_assignments) == np.squeeze(
                        assignments))
                logging.info('Accuracy: %f', accuracy)
                logging.info('First loss: %f, end loss: %f', first_loss,
                             end_loss)
                self.assertGreater(end_loss, first_loss)
                self.assertGreater(accuracy, 0.98)
Ejemplo n.º 2
0
 def _model_fn(features, labels, mode, config):
   """Model function."""
   assert labels is None, labels
   (all_scores,
    model_predictions,
    losses, training_op,
    init_op,
    is_initialized) = gmm_ops.gmm(self._parse_tensor_or_dict(features),
                                  self._training_initial_clusters,
                                  self._num_clusters, self._random_seed,
                                  self._covariance_type,
                                  self._params)
   incr_step = state_ops.assign_add(variables.get_global_step(), 1)
   loss = math_ops.reduce_sum(losses)
   training_op = with_dependencies([training_op, incr_step], loss)
   training_hooks = [_InitializeClustersHook(
       init_op, is_initialized, config.is_chief)]
   predictions = {
       GMM.ALL_SCORES: all_scores[0],
       GMM.ASSIGNMENTS: model_predictions[0][0],
   }
   eval_metric_ops = {
       GMM.SCORES: _streaming_sum(loss),
   }
   return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions,
                                  eval_metric_ops=eval_metric_ops,
                                  loss=loss, train_op=training_op,
                                  training_hooks=training_hooks)
Ejemplo n.º 3
0
  def test_simple_cluster(self):
    """Tests that the clusters are correct."""
    num_classes = 2
    graph = ops.Graph()
    with graph.as_default() as g:
      g.seed = 5
      with self.test_session() as sess:
        data = constant_op.constant(self.data, dtype=dtypes.float32)
        loss_op, scores, assignments, training_op, init_op, _ = gmm_ops.gmm(
            data, 'random', num_classes, random_seed=self.seed)

        variables.global_variables_initializer().run()
        sess.run(init_op)
        first_loss = sess.run(loss_op)
        for _ in xrange(self.iterations):
          sess.run(training_op)
        assignments = sess.run(assignments)
        end_loss = sess.run(loss_op)
        scores = sess.run(scores)
        self.assertEqual((self.num_examples, 1), scores.shape)
        accuracy = np.mean(
            np.asarray(self.true_assignments) == np.squeeze(assignments))
        logging.info('Accuracy: %f', accuracy)
        logging.info('First loss: %f, end loss: %f', first_loss, end_loss)
        self.assertGreater(end_loss, first_loss)
        self.assertGreater(accuracy, 0.98)
Ejemplo n.º 4
0
 def _model_fn(features, labels, mode, config):
   """Model function."""
   assert labels is None, labels
   (loss,
    scores,
    model_predictions,
    training_op,
    init_op,
    is_initialized) = gmm_ops.gmm(self._parse_tensor_or_dict(features),
                                  self._training_initial_clusters,
                                  self._num_clusters, self._random_seed,
                                  self._covariance_type,
                                  self._params)
   incr_step = state_ops.assign_add(training_util.get_global_step(), 1)
   training_op = with_dependencies([training_op, incr_step], loss)
   training_hooks = [_InitializeClustersHook(
       init_op, is_initialized, config.is_chief)]
   predictions = {
       GMM.ASSIGNMENTS: model_predictions[0][0],
   }
   eval_metric_ops = {
       GMM.SCORES: scores,
       GMM.LOG_LIKELIHOOD: _streaming_sum(loss),
   }
   return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions,
                                  eval_metric_ops=eval_metric_ops,
                                  loss=loss, train_op=training_op,
                                  training_hooks=training_hooks)
Ejemplo n.º 5
0
 def _get_eval_ops(self, features, _, unused_metrics):
     (_, _, losses, _) = gmm_ops.gmm(self._parse_tensor_or_dict(features),
                                     self._training_initial_clusters,
                                     self._num_clusters, self._random_seed,
                                     self._covariance_type, self._params)
     return {
         GMM.SCORES: math_ops.reduce_sum(losses),
     }
Ejemplo n.º 6
0
 def _get_predict_ops(self, features):
     (all_scores, model_predictions, _,
      _) = gmm_ops.gmm(features, self._training_initial_clusters,
                       self._num_clusters, self._random_seed,
                       self._covariance_type, self._params)
     return {
         GMM.ALL_SCORES: all_scores[0],
         GMM.ASSIGNMENTS: model_predictions[0][0],
     }
Ejemplo n.º 7
0
 def _get_train_ops(self, features, _):
     (_, _, losses,
      training_op) = gmm_ops.gmm(features, self._training_initial_clusters,
                                 self._num_clusters, self._random_seed,
                                 self._covariance_type, self._params)
     incr_step = tf.assign_add(tf.contrib.framework.get_global_step(), 1)
     loss = tf.reduce_sum(losses)
     training_op = with_dependencies([training_op, incr_step], loss)
     return training_op, loss
Ejemplo n.º 8
0
 def _get_predict_ops(self, features):
   (all_scores, model_predictions, _, _) = gmm_ops.gmm(
       self._parse_tensor_or_dict(features), self._training_initial_clusters,
       self._num_clusters, self._random_seed, self._covariance_type,
       self._params)
   return {
       GMM.ALL_SCORES: all_scores[0],
       GMM.ASSIGNMENTS: model_predictions[0][0],
   }
Ejemplo n.º 9
0
 def _get_train_ops(self, features, _):
   (_, _, losses, training_op) = gmm_ops.gmm(
       self._parse_tensor_or_dict(features), self._training_initial_clusters,
       self._num_clusters, self._random_seed, self._covariance_type,
       self._params)
   incr_step = state_ops.assign_add(variables.get_global_step(), 1)
   loss = math_ops.reduce_sum(losses)
   training_op = with_dependencies([training_op, incr_step], loss)
   return training_op, loss
Ejemplo n.º 10
0
 def _get_train_ops(self, features, _):
     (_, _, losses,
      training_op) = gmm_ops.gmm(self._parse_tensor_or_dict(features),
                                 self._training_initial_clusters,
                                 self._num_clusters, self._random_seed,
                                 self._covariance_type, self._params)
     incr_step = state_ops.assign_add(variables.get_global_step(), 1)
     loss = math_ops.reduce_sum(losses)
     training_op = with_dependencies([training_op, incr_step], loss)
     return training_op, loss
Ejemplo n.º 11
0
 def _get_eval_ops(self, features, _, unused_metrics):
   (_,
    _,
    losses,
    _) = gmm_ops.gmm(
        self._parse_tensor_or_dict(features),
        self._training_initial_clusters,
        self._num_clusters,
        self._random_seed,
        self._covariance_type,
        self._params)
   return {GMM.SCORES: _streaming_sum(math_ops.reduce_sum(losses))}
Ejemplo n.º 12
0
 def _get_eval_ops(self, features, _, unused_metrics):
   (_,
    _,
    losses,
    _) = gmm_ops.gmm(
        features,
        self._training_initial_clusters,
        self._num_clusters,
        self._random_seed,
        self._covariance_type,
        self._params)
   return {
       GMM.SCORES: tf.reduce_sum(losses),
   }
Ejemplo n.º 13
0
 def _get_train_ops(self, features, _):
   (_,
    _,
    losses,
    training_op) = gmm_ops.gmm(
        features,
        self._training_initial_clusters,
        self._num_clusters,
        self._random_seed,
        self._covariance_type,
        self._params)
   incr_step = tf.assign_add(tf.contrib.framework.get_global_step(), 1)
   loss = tf.reduce_sum(losses)
   training_op = with_dependencies([training_op, incr_step], loss)
   return training_op, loss
Ejemplo n.º 14
0
    def test_simple_cluster(self):
        """Tests that the clusters are correct."""
        num_classes = 2
        graph = ops.Graph()
        with graph.as_default() as g:
            g.seed = 5
            with self.test_session() as sess:
                data = constant_op.constant(self.data, dtype=dtypes.float32)
                _, assignments, _, training_op = gmm_ops.gmm(data, "random", num_classes, random_seed=self.seed)

                variables.global_variables_initializer().run()
                for _ in xrange(self.iterations):
                    sess.run(training_op)
                assignments = sess.run(assignments)
                accuracy = np.mean(np.asarray(self.true_assignments) == np.squeeze(assignments))
                logging.info("Accuracy: %f", accuracy)
                self.assertGreater(accuracy, 0.98)
Ejemplo n.º 15
0
    def test_simple_cluster(self):
        """Tests that the clusters are correct."""
        num_classes = 2
        graph = tf.Graph()
        with graph.as_default() as g:
            g.seed = 5
            with self.test_session() as sess:
                data = tf.constant(self.data, dtype=tf.float32)
                _, assignments, _, training_op = gmm_ops.gmm(
                    data, 'random', num_classes, random_seed=self.seed)

                tf.initialize_all_variables().run()
                for _ in xrange(self.iterations):
                    sess.run(training_op)
                assignments = sess.run(assignments)
                accuracy = np.mean(
                    np.asarray(self.true_assignments) == np.squeeze(
                        assignments))
                logging.info('Accuracy: %f', accuracy)
                self.assertGreater(accuracy, 0.98)
Ejemplo n.º 16
0
 def _model_fn(features, labels, mode):
   """Model function."""
   assert labels is None, labels
   (all_scores, model_predictions, losses, training_op) = gmm_ops.gmm(
       self._parse_tensor_or_dict(features), self._training_initial_clusters,
       self._num_clusters, self._random_seed, self._covariance_type,
       self._params)
   incr_step = state_ops.assign_add(variables.get_global_step(), 1)
   loss = math_ops.reduce_sum(losses)
   training_op = with_dependencies([training_op, incr_step], loss)
   predictions = {
       GMM.ALL_SCORES: all_scores[0],
       GMM.ASSIGNMENTS: model_predictions[0][0],
   }
   eval_metric_ops = {
       GMM.SCORES: _streaming_sum(loss),
   }
   return model_fn_lib.ModelFnOps(mode=mode, predictions=predictions,
                                  eval_metric_ops=eval_metric_ops,
                                  loss=loss, train_op=training_op)