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)
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))
def testUnfitted(self): estimator = learn.TensorFlowEstimator(model_fn=None, n_classes=1) with self.assertRaises(base.NotFittedError): estimator.predict([1, 2, 3]) with self.assertRaises(base.NotFittedError): estimator.save("/tmp/path")
# 3 layer neural network with rectified linear activation. if reset_seed: random.seed(42) classifier = skflow.TensorFlowDNNClassifier(hidden_units=[10, 20, 10], n_classes=2, batch_size=128, steps=500, learning_rate=learning_rate) classifier.fit(X_train, y_train) print(accuracy_score(classifier.predict(X_test), y_test)) # 3 layer neural network with hyperbolic tangent activation. def dnn_tanh(X, y): layers = skflow.ops.dnn(X, [20, 20, 20], tf.tanh) return skflow.models.logistic_regression(layers, y) if reset_seed: random.seed(42) classifier = skflow.TensorFlowEstimator(model_fn=dnn_tanh, n_classes=2, batch_size=128, steps=500, learning_rate=learning_rate) classifier.fit(X_train, y_train) print(accuracy_score(classifier.predict(X_test), y_test))