Ejemplo n.º 1
0
    def testIrisStreaming(self):
        iris = datasets.load_iris()

        def iris_data():
            while True:
                for x in iris.data:
                    yield x

        def iris_predict_data():
            for x in iris.data:
                yield x

        def iris_target():
            while True:
                for y in iris.target:
                    yield y

        classifier = learn.TensorFlowLinearClassifier(n_classes=3, steps=100)
        classifier.fit(iris_data(), iris_target())
        score1 = accuracy_score(iris.target, classifier.predict(iris.data))
        score2 = accuracy_score(iris.target,
                                classifier.predict(iris_predict_data()))
        self.assertGreater(score1, 0.5,
                           "Failed with score = {0}".format(score1))
        self.assertEqual(
            score2, score1, "Scores from {0} iterator doesn't "
            "match score {1} from full "
            "data.".format(score2, score1))
Ejemplo n.º 2
0
  def testIrisES(self):
    random.seed(42)

    iris = datasets.load_iris()
    x_train, x_test, y_train, y_test = train_test_split(iris.data,
                                                        iris.target,
                                                        test_size=0.2,
                                                        random_state=42)

    x_train, x_val, y_train, y_val = train_test_split(
        x_train, y_train, test_size=0.2)
    val_monitor = learn.monitors.ValidationMonitor(x_val, y_val,
                                                   early_stopping_rounds=100)

    # classifier without early stopping - overfitting
    classifier1 = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                                n_classes=3,
                                                steps=1000)
    classifier1.fit(x_train, y_train)
    accuracy_score(y_test, classifier1.predict(x_test))

    # classifier with early stopping - improved accuracy on testing set
    classifier2 = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                                n_classes=3,
                                                steps=1000)

    classifier2.fit(x_train, y_train, monitors=[val_monitor])
    accuracy_score(y_test, classifier2.predict(x_test))
Ejemplo n.º 3
0
 def testIris(self):
     path = tf.test.get_temp_dir() + '/tmp.saver'
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
Ejemplo n.º 4
0
  def testIrisStreaming(self):
    iris = datasets.load_iris()

    def iris_data():
      while True:
        for x in iris.data:
          yield x

    def iris_predict_data():
      for x in iris.data:
        yield x

    def iris_target():
      while True:
        for y in iris.target:
          yield y

    classifier = learn.TensorFlowLinearClassifier(n_classes=3, steps=100)
    classifier.fit(iris_data(), iris_target())
    score1 = accuracy_score(iris.target, classifier.predict(iris.data))
    score2 = accuracy_score(iris.target,
                            classifier.predict(iris_predict_data()))
    self.assertGreater(score1, 0.5, "Failed with score = {0}".format(score1))
    self.assertEqual(score2, score1, "Scores from {0} iterator doesn't "
                     "match score {1} from full "
                     "data.".format(score2, score1))
Ejemplo n.º 5
0
 def testDNNDropout0_1(self):
     # Dropping only a little.
     iris = datasets.load_iris()
     classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=3, dropout=0.1)
     classifier.fit(iris.data, iris.target)
     score = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertGreater(score, 0.9, "Failed with score = {0}".format(score))
Ejemplo n.º 6
0
  def testIrisMomentum(self):
    random.seed(42)

    iris = datasets.load_iris()
    x_train, x_test, y_train, y_test = train_test_split(iris.data,
                                                        iris.target,
                                                        test_size=0.2,
                                                        random_state=42)

    def custom_optimizer(learning_rate):
      return tf.train.MomentumOptimizer(learning_rate, 0.9)

    cont_features = [
        tf.contrib.layers.real_valued_column("", dimension=4)]
    classifier = learn.TensorFlowDNNClassifier(
        feature_columns=cont_features,
        hidden_units=[10, 20, 10],
        n_classes=3,
        steps=400,
        learning_rate=0.01,
        optimizer=custom_optimizer)
    classifier.fit(x_train, y_train)
    score = accuracy_score(y_test, classifier.predict(x_test))

    self.assertGreater(score, 0.65, "Failed with score = {0}".format(score))
Ejemplo n.º 7
0
    def testIrisMomentum(self):
        random.seed(42)

        iris = datasets.load_iris()
        x_train, x_test, y_train, y_test = train_test_split(iris.data,
                                                            iris.target,
                                                            test_size=0.2,
                                                            random_state=42)

        def custom_optimizer():
            return momentum_lib.MomentumOptimizer(learning_rate=0.01,
                                                  momentum=0.9)

        classifier = learn.DNNClassifier(
            hidden_units=[10, 20, 10],
            feature_columns=learn.infer_real_valued_columns_from_input(
                x_train),
            n_classes=3,
            optimizer=custom_optimizer,
            config=learn.RunConfig(tf_random_seed=1))
        classifier.fit(x_train, y_train, steps=400)
        predictions = np.array(list(classifier.predict_classes(x_test)))
        score = accuracy_score(y_test, predictions)

        self.assertGreater(score, 0.65,
                           "Failed with score = {0}".format(score))
Ejemplo n.º 8
0
    def testIrisMomentum(self):
        random.seed(42)

        iris = datasets.load_iris()
        X_train, X_test, y_train, y_test = train_test_split(iris.data,
                                                            iris.target,
                                                            test_size=0.2,
                                                            random_state=42)

        # setup exponential decay function
        def exp_decay(global_step):
            return tf.train.exponential_decay(learning_rate=0.1,
                                              global_step=global_step,
                                              decay_steps=100,
                                              decay_rate=0.001)

        def custom_optimizer(learning_rate):
            return tf.train.MomentumOptimizer(learning_rate, 0.9)

        classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                                   n_classes=3,
                                                   steps=800,
                                                   learning_rate=exp_decay,
                                                   optimizer=custom_optimizer)
        classifier.fit(X_train, y_train)
        score = accuracy_score(y_test, classifier.predict(X_test))

        self.assertGreater(score, 0.7, "Failed with score = {0}".format(score))
Ejemplo n.º 9
0
  def testIrisMomentum(self):
    random.seed(42)

    iris = datasets.load_iris()
    x_train, x_test, y_train, y_test = train_test_split(iris.data,
                                                        iris.target,
                                                        test_size=0.2,
                                                        random_state=42)
    # setup exponential decay function
    def exp_decay(global_step):
      return tf.train.exponential_decay(learning_rate=0.1,
                                        global_step=global_step,
                                        decay_steps=100,
                                        decay_rate=0.001)

    def custom_optimizer(learning_rate):
      return tf.train.MomentumOptimizer(learning_rate, 0.9)

    classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                               n_classes=3,
                                               steps=400,
                                               learning_rate=exp_decay,
                                               optimizer=custom_optimizer)
    classifier.fit(x_train, y_train)
    score = accuracy_score(y_test, classifier.predict(x_test))

    self.assertGreater(score, 0.65, "Failed with score = {0}".format(score))
Ejemplo n.º 10
0
  def testIrisStreaming(self):
    iris = datasets.load_iris()

    def iris_data():
      while True:
        for x in iris.data:
          yield x

    def iris_predict_data():
      for x in iris.data:
        yield x

    def iris_target():
      while True:
        for y in iris.target:
          yield y

    classifier = learn.LinearClassifier(
        feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
        n_classes=3)
    classifier.fit(iris_data(), iris_target(), max_steps=500)
    score1 = accuracy_score(iris.target, classifier.predict(iris.data))
    score2 = accuracy_score(iris.target,
                            classifier.predict(iris_predict_data()))
    self.assertGreater(score1, 0.5, "Failed with score = {0}".format(score1))
    self.assertEqual(score2, score1, "Scores from {0} iterator doesn't "
                     "match score {1} from full "
                     "data.".format(score2, score1))
Ejemplo n.º 11
0
 def testIris(self):
   path = tf.test.get_temp_dir() + '/tmp.saver'
   random.seed(42)
   iris = datasets.load_iris()
   classifier = learn.TensorFlowLinearClassifier(n_classes=3)
   classifier.fit(iris.data, iris.target)
   classifier.save(path)
Ejemplo n.º 12
0
  def testIrisStreaming(self):
    iris = datasets.load_iris()

    def iris_data():
      while True:
        for x in iris.data:
          yield x

    def iris_predict_data():
      for x in iris.data:
        yield x

    def iris_target():
      while True:
        for y in iris.target:
          yield y

    classifier = learn.LinearClassifier(
        feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
        n_classes=3)
    classifier.fit(iris_data(), iris_target(), max_steps=500)
    score1 = accuracy_score(iris.target,
                            list(classifier.predict(iris.data)))
    score2 = accuracy_score(iris.target,
                            list(classifier.predict(iris_predict_data())))
    self.assertGreater(score1, 0.5, "Failed with score = {0}".format(score1))
    self.assertEqual(score2, score1, "Scores from {0} iterator doesn't "
                     "match score {1} from full "
                     "data.".format(score2, score1))
Ejemplo n.º 13
0
 def testIrisClassWeight(self):
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(
         n_classes=3, class_weight=[0.1, 0.8, 0.1])
     classifier.fit(iris.data, iris.target)
     score = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertLess(score, 0.7, "Failed with score = {0}".format(score))
Ejemplo n.º 14
0
 def testIrisClassWeight(self):
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(
         n_classes=3, class_weight=[0.1, 0.8, 0.1])
     classifier.fit(iris.data, iris.target)
     score = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertLess(score, 0.7, "Failed with score = {0}".format(score))
Ejemplo n.º 15
0
 def testIris(self):
   iris = datasets.load_iris()
   classifier = learn.TensorFlowLinearClassifier(
       feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
       n_classes=3)
   classifier.fit(iris.data, [x for x in iris.target])
   score = accuracy_score(iris.target, classifier.predict(iris.data))
   self.assertGreater(score, 0.7, "Failed with score = {0}".format(score))
Ejemplo n.º 16
0
 def testNoCheckpoints(self):
     random.seed(42)
     iris = datasets.load_iris()
     cont_features = [tf.contrib.layers.real_valued_column('', dimension=4)]
     classifier = learn.DNNClassifier(feature_columns=cont_features,
                                      hidden_units=[10, 20, 10],
                                      n_classes=3)
     classifier.fit(iris.data, iris.target, max_steps=100)
Ejemplo n.º 17
0
 def testIrisSummaries(self):
   iris = datasets.load_iris()
   output_dir = tempfile.mkdtemp() + "learn_tests/"
   classifier = learn.TensorFlowLinearClassifier(n_classes=3,
                                                 model_dir=output_dir)
   classifier.fit(iris.data, iris.target)
   score = accuracy_score(iris.target, classifier.predict(iris.data))
   self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 18
0
 def testNoCheckpoints(self):
   path = tf.test.get_temp_dir() + '/tmp/tmp.saver4'
   random.seed(42)
   iris = datasets.load_iris()
   classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                              n_classes=3)
   classifier.fit(iris.data, iris.target)
   classifier.save(path)
Ejemplo n.º 19
0
 def testNoCheckpoints(self):
     path = tf.test.get_temp_dir() + '/tmp/tmp.saver4'
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                                n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
Ejemplo n.º 20
0
 def testNoCheckpoints(self):
   random.seed(42)
   iris = datasets.load_iris()
   cont_features = [
       tf.contrib.layers.real_valued_column('', dimension=4)]
   classifier = learn.DNNClassifier(feature_columns=cont_features,
                                    hidden_units=[10, 20, 10],
                                    n_classes=3)
   classifier.fit(iris.data, iris.target, max_steps=100)
Ejemplo n.º 21
0
 def testIrisClassWeight(self):
   iris = datasets.load_iris()
   # Note, class_weight are not supported anymore :( Use weight_column.
   with self.assertRaises(ValueError):
     classifier = learn.TensorFlowLinearClassifier(
         n_classes=3, class_weight=[0.1, 0.8, 0.1])
     classifier.fit(iris.data, iris.target)
     score = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertLess(score, 0.7, "Failed with score = {0}".format(score))
Ejemplo n.º 22
0
 def testDNNAutoencoder(self):
   import numpy as np
   iris = datasets.load_iris()
   autoencoder = learn.TensorFlowDNNAutoencoder(hidden_units=[10, 20])
   transformed = autoencoder.fit_transform(iris.data[1:2])
   expected = np.array([[ -3.57627869e-07, 1.17000043e+00, 1.01902664e+00, 1.19209290e-07,
                           0.00000000e+00, 1.19209290e-07, -5.96046448e-08, -2.38418579e-07,
                           9.74681854e-01, 1.19209290e-07]])
   self.assertAllClose(transformed, expected)
Ejemplo n.º 23
0
 def testDNNDropout0_1(self):
     # Dropping only a little.
     iris = datasets.load_iris()
     classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10],
                                                n_classes=3,
                                                dropout=0.1)
     classifier.fit(iris.data, iris.target)
     score = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertGreater(score, 0.9, "Failed with score = {0}".format(score))
Ejemplo n.º 24
0
 def testIris(self):
   path = tf.test.get_temp_dir() + '/tmp.saver'
   random.seed(42)
   iris = datasets.load_iris()
   cont_features = [
       tf.contrib.layers.real_valued_column('', dimension=4)]
   classifier = learn.LinearClassifier(
       feature_columns=cont_features, n_classes=3, model_dir=path)
   classifier.fit(iris.data, iris.target, steps=200)
Ejemplo n.º 25
0
 def testIris_proba(self):
   # If sklearn available.
   if log_loss:
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowClassifier(n_classes=3, steps=250)
     classifier.fit(iris.data, iris.target)
     score = log_loss(iris.target, classifier.predict_proba(iris.data))
     self.assertLess(score, 0.8, "Failed with score = {0}".format(score))
Ejemplo n.º 26
0
 def testIrisSummaries(self):
   iris = datasets.load_iris()
   output_dir = tempfile.mkdtemp() + "learn_tests/"
   classifier = learn.LinearClassifier(
       feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
       n_classes=3, model_dir=output_dir)
   classifier.fit(iris.data, iris.target, max_steps=100)
   score = accuracy_score(iris.target, classifier.predict(iris.data))
   self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 27
0
 def testIris(self):
     path = tf.test.get_temp_dir() + '/tmp.saver'
     random.seed(42)
     iris = datasets.load_iris()
     cont_features = [tf.contrib.layers.real_valued_column('', dimension=4)]
     classifier = learn.TensorFlowLinearClassifier(
         feature_columns=cont_features, n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
Ejemplo n.º 28
0
 def testIrisSummaries(self):
   iris = datasets.load_iris()
   output_dir = tempfile.mkdtemp() + "learn_tests/"
   classifier = learn.LinearClassifier(
       feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
       n_classes=3, model_dir=output_dir)
   classifier.fit(iris.data, iris.target, max_steps=100)
   score = accuracy_score(iris.target, list(classifier.predict(iris.data)))
   self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 29
0
 def test_pandas_series(self):
   if HAS_PANDAS:
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.Series(iris.target)
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(data, labels)
     score = accuracy_score(labels, classifier.predict(data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 30
0
 def testIrisContinueTraining(self):
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(n_classes=3,
         learning_rate=0.01, continue_training=True, steps=250)
     classifier.fit(iris.data, iris.target)
     score1 = accuracy_score(iris.target, classifier.predict(iris.data))
     classifier.fit(iris.data, iris.target)
     score2 = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertGreater(score2, score1,
                        "Failed with score = {0}".format(score2))
Ejemplo n.º 31
0
 def testNoCheckpoints(self):
     path = tf.test.get_temp_dir() + '/tmp/tmp.saver4'
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
     os.remove(os.path.join(path, 'checkpoint'))
     with self.assertRaises(ValueError):
         learn.TensorFlowEstimator.restore(path)
Ejemplo n.º 32
0
 def test_pandas_series(self):
   if HAS_PANDAS:
     import pandas as pd
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.Series(iris.target)
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(data, labels)
     score = accuracy_score(labels, classifier.predict(data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 33
0
  def testCustomModel(self):
    path = tf.test.get_temp_dir() + '/tmp.saver2'
    random.seed(42)
    iris = datasets.load_iris()

    def _custom_model(x, y):
      return learn.models.logistic_regression(x, y)

    classifier = learn.TensorFlowEstimator(model_fn=_custom_model, n_classes=3)
    classifier.fit(iris.data, iris.target)
    classifier.save(path)
Ejemplo n.º 34
0
 def testIris(self):
     path = tf.test.get_temp_dir() + '/tmp.saver'
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
     new_classifier = learn.TensorFlowEstimator.restore(path)
     self.assertEqual(type(new_classifier), type(classifier))
     score = accuracy_score(iris.target, new_classifier.predict(iris.data))
     self.assertGreater(score, 0.5, 'Failed with score = {0}'.format(score))
Ejemplo n.º 35
0
 def testDNN(self):
     path = tf.test.get_temp_dir() + '/tmp_saver3'
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
     new_classifier = learn.TensorFlowEstimator.restore(path)
     self.assertEqual(type(new_classifier), type(classifier))
     score = accuracy_score(iris.target, new_classifier.predict(iris.data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 36
0
 def testIris_proba(self):
   # If sklearn available.
   if log_loss:
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.LinearClassifier(
         feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
         n_classes=3)
     classifier.fit(iris.data, iris.target, max_steps=250)
     score = log_loss(iris.target, classifier.predict_proba(iris.data))
     self.assertLess(score, 0.8, "Failed with score = {0}".format(score))
Ejemplo n.º 37
0
 def testNoCheckpoints(self):
   path = tf.test.get_temp_dir() + '/tmp/tmp.saver4'
   random.seed(42)
   iris = datasets.load_iris()
   cont_features = [
       tf.contrib.layers.real_valued_column('', dimension=4)]
   classifier = learn.TensorFlowDNNClassifier(feature_columns=cont_features,
                                              hidden_units=[10, 20, 10],
                                              n_classes=3)
   classifier.fit(iris.data, iris.target)
   classifier.save(path)
Ejemplo n.º 38
0
 def testNoCheckpoints(self):
     path = tf.test.get_temp_dir() + '/tmp/tmp.saver4'
     random.seed(42)
     iris = datasets.load_iris()
     cont_features = [tf.contrib.layers.real_valued_column('', dimension=4)]
     classifier = learn.TensorFlowDNNClassifier(
         feature_columns=cont_features,
         hidden_units=[10, 20, 10],
         n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
Ejemplo n.º 39
0
 def testIris(self):
   path = tf.test.get_temp_dir() + '/tmp.saver'
   random.seed(42)
   iris = datasets.load_iris()
   classifier = learn.TensorFlowLinearClassifier(n_classes=3)
   classifier.fit(iris.data, iris.target)
   classifier.save(path)
   new_classifier = learn.TensorFlowEstimator.restore(path)
   self.assertEqual(type(new_classifier), type(classifier))
   score = accuracy_score(iris.target, new_classifier.predict(iris.data))
   self.assertGreater(score, 0.5, 'Failed with score = {0}'.format(score))
Ejemplo n.º 40
0
 def testIris_proba(self):
   # If sklearn available.
   if log_loss:
     random.seed(42)
     iris = datasets.load_iris()
     classifier = learn.LinearClassifier(
         feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
         n_classes=3)
     classifier.fit(iris.data, iris.target, max_steps=250)
     score = log_loss(iris.target, list(classifier.predict_proba(iris.data)))
     self.assertLess(score, 0.8, "Failed with score = {0}".format(score))
Ejemplo n.º 41
0
  def testCustomModel(self):
    path = tf.test.get_temp_dir() + '/tmp.saver2'
    random.seed(42)
    iris = datasets.load_iris()

    def _custom_model(x, y):
      return learn.models.logistic_regression(x, y)

    classifier = learn.TensorFlowEstimator(model_fn=_custom_model, n_classes=3)
    classifier.fit(iris.data, iris.target)
    classifier.save(path)
Ejemplo n.º 42
0
 def test_pandas_dataframe(self):
   if HAS_PANDAS:
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.DataFrame(iris.target)
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(data, labels)
     score = accuracy_score(labels[0], classifier.predict(data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
   else:
     print("No pandas installed. pandas-related tests are skipped.")
Ejemplo n.º 43
0
 def testIrisAllVariables(self):
   iris = datasets.load_iris()
   classifier = learn.TensorFlowLinearClassifier(n_classes=3)
   classifier.fit(iris.data, [float(x) for x in iris.target])
   self.assertEqual(
       classifier.get_variable_names(),
       ["global_step:0", "logistic_regression/weights:0",
        "logistic_regression/bias:0",
        "logistic_regression/softmax_classifier/softmax_cross_entropy_loss/"
        "value/avg:0",
        "learning_rate:0", "logistic_regression/weights/Adagrad:0",
        "logistic_regression/bias/Adagrad:0"])
Ejemplo n.º 44
0
 def testIrisContinueTraining(self):
   iris = datasets.load_iris()
   classifier = learn.LinearClassifier(
       feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
       n_classes=3)
   classifier.fit(iris.data, iris.target, steps=100)
   score1 = accuracy_score(iris.target, list(classifier.predict(iris.data)))
   classifier.fit(iris.data, iris.target, steps=500)
   score2 = accuracy_score(iris.target, list(classifier.predict(iris.data)))
   self.assertGreater(
       score2, score1,
       "Failed with score2 {0} <= score1 {1}".format(score2, score1))
Ejemplo n.º 45
0
 def testIrisContinueTraining(self):
   iris = datasets.load_iris()
   classifier = learn.LinearClassifier(
       feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
       n_classes=3)
   classifier.fit(iris.data, iris.target, steps=100)
   score1 = accuracy_score(iris.target, classifier.predict(iris.data))
   classifier.fit(iris.data, iris.target, steps=500)
   score2 = accuracy_score(iris.target, classifier.predict(iris.data))
   self.assertGreater(
       score2, score1,
       "Failed with score2 {0} <= score1 {1}".format(score2, score1))
Ejemplo n.º 46
0
 def testIrisContinueTraining(self):
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(n_classes=3,
                                                   learning_rate=0.01,
                                                   continue_training=True,
                                                   steps=250)
     classifier.fit(iris.data, iris.target)
     score1 = accuracy_score(iris.target, classifier.predict(iris.data))
     classifier.fit(iris.data, iris.target)
     score2 = accuracy_score(iris.target, classifier.predict(iris.data))
     self.assertGreater(score2, score1,
                        "Failed with score = {0}".format(score2))
Ejemplo n.º 47
0
 def testIrisAllVariables(self):
     iris = datasets.load_iris()
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(iris.data, [float(x) for x in iris.target])
     self.assertEqual(classifier.get_variable_names(), [
         "OptimizeLoss/learning_rate",
         "OptimizeLoss/logistic_regression/bias/Adagrad",
         "OptimizeLoss/logistic_regression/softmax_classifier/"
         "softmax_cross_entropy_loss/value/avg",
         "OptimizeLoss/logistic_regression/weights/Adagrad", "global_step",
         "logistic_regression/bias", "logistic_regression/weights"
     ])
Ejemplo n.º 48
0
    def testIrisES(self):
        random.seed(42)

        iris = datasets.load_iris()
        x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

        x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size=0.2, random_state=42)
        val_monitor = learn.monitors.ValidationMonitor(
            x_val,
            y_val,
            every_n_steps=50,
            early_stopping_rounds=100,
            early_stopping_metric="accuracy",
            early_stopping_metric_minimize=False,
        )

        # classifier without early stopping - overfitting
        classifier1 = learn.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=3, steps=1000)
        classifier1.fit(x_train, y_train)
        _ = accuracy_score(y_test, classifier1.predict(x_test))

        # Full 1000 steps, 12 summaries and no evaluation summary.
        # 12 summaries = global_step + first + every 100 out of 1000 steps.
        self.assertEqual(12, len(_get_summary_events(classifier1.model_dir)))
        with self.assertRaises(ValueError):
            _get_summary_events(classifier1.model_dir + "/eval")

        # classifier with early stopping - improved accuracy on testing set
        classifier2 = learn.TensorFlowDNNClassifier(
            hidden_units=[10, 20, 10],
            n_classes=3,
            steps=2000,
            config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1),
        )

        classifier2.fit(x_train, y_train, monitors=[val_monitor])
        _ = accuracy_score(y_val, classifier2.predict(x_val))
        _ = accuracy_score(y_test, classifier2.predict(x_test))

        # Note, this test is unstable, so not checking for equality.
        # See stability_test for examples of stability issues.
        if val_monitor.early_stopped:
            self.assertLess(val_monitor.best_step, 2000)
            # Note, due to validation monitor stopping after the best score occur,
            # the accuracy at current checkpoint is less.
            # TODO(ipolosukhin): Time machine for restoring old checkpoints?
            # flaky, still not always best_value better then score2 value.
            # self.assertGreater(val_monitor.best_value, score2_val)

            # Early stopped, unstable so checking only < then max.
            self.assertLess(len(_get_summary_events(classifier2.model_dir)), 21)
            # Eval typically has ~6 events, but it varies based on the run.
            self.assertLess(len(_get_summary_events(classifier2.model_dir + "/eval")), 8)
Ejemplo n.º 49
0
 def test_pandas_dataframe(self):
   if HAS_PANDAS:
     import pandas as pd
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.DataFrame(iris.target)
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(data, labels)
     score = accuracy_score(labels[0], classifier.predict(data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
   else:
     print("No pandas installed. pandas-related tests are skipped.")
Ejemplo n.º 50
0
 def test_dask_iris_classification(self):
   if HAS_DASK and HAS_PANDAS:
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     data = dd.from_pandas(data, npartitions=2)
     labels = pd.DataFrame(iris.target)
     labels = dd.from_pandas(labels, npartitions=2)
     classifier = learn.TensorFlowLinearClassifier(n_classes=3)
     classifier.fit(data, labels)
     predictions = data.map_partitions(classifier.predict).compute()
     score = accuracy_score(labels.compute(), predictions)
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 51
0
 def test_pandas_series(self):
   if HAS_PANDAS:
     import pandas as pd  # pylint: disable=g-import-not-at-top
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.Series(iris.target)
     classifier = learn.TensorFlowLinearClassifier(
         feature_columns=learn.infer_real_valued_columns_from_input(data),
         n_classes=3)
     classifier.fit(data, labels)
     score = accuracy_score(labels, classifier.predict(data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 52
0
 def test_pandas_series(self):
   if HAS_PANDAS:
     import pandas as pd  # pylint: disable=g-import-not-at-top
     random.seed(42)
     iris = datasets.load_iris()
     data = pd.DataFrame(iris.data)
     labels = pd.Series(iris.target)
     classifier = learn.LinearClassifier(
         feature_columns=learn.infer_real_valued_columns_from_input(data),
         n_classes=3)
     classifier.fit(data, labels, steps=100)
     score = accuracy_score(labels, list(classifier.predict(data)))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
Ejemplo n.º 53
0
 def test_dask_iris_classification(self):
     if HAS_DASK and HAS_PANDAS:
         random.seed(42)
         iris = datasets.load_iris()
         data = pd.DataFrame(iris.data)
         data = dd.from_pandas(data, npartitions=2)
         labels = pd.DataFrame(iris.target)
         labels = dd.from_pandas(labels, npartitions=2)
         classifier = learn.TensorFlowLinearClassifier(n_classes=3)
         classifier.fit(data, labels)
         predictions = data.map_partitions(classifier.predict).compute()
         score = accuracy_score(labels.compute(), predictions)
         self.assertGreater(score, 0.5,
                            "Failed with score = {0}".format(score))
Ejemplo n.º 54
0
 def testCustomModel(self):
     path = tf.test.get_temp_dir() + '/tmp.saver2'
     random.seed(42)
     iris = datasets.load_iris()
     def custom_model(X, y):
         return learn.models.logistic_regression(X, y)
     classifier = learn.TensorFlowEstimator(model_fn=custom_model,
         n_classes=3)
     classifier.fit(iris.data, iris.target)
     classifier.save(path)
     new_classifier = learn.TensorFlowEstimator.restore(path)
     self.assertEqual(type(new_classifier), type(classifier))
     score = accuracy_score(iris.target, new_classifier.predict(iris.data))
     self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))