As we saw previously how to build a full Multi-Layer Perceptron model with full Sessions in Tensorflow. Unfortunately this was an extremely involved process. However developers have created ContribLearn (previously known as TKFlow or SciKit-Flow) which provides a SciKit Learn like interface for Tensorflow!
It is much easier to use, but you sacrifice some level of customization of your model. Let's go ahead and explore it!
In [1]:
from sklearn.datasets import load_iris
In [2]:
iris = load_iris()
In [3]:
X = iris['data']
In [4]:
y = iris['target']
In [5]:
y
Out[5]:
In [6]:
y.dtype
Out[6]:
In [7]:
from sklearn.cross_validation import train_test_split
In [8]:
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
In [9]:
import tensorflow.contrib.learn.python.learn as learn
There are several high level abstraction calls to models in learn, you can explore them with Tab, but we will use DNNClassifier, which stands for Deep Neural Network:
In [10]:
classifier = learn.DNNClassifier(hidden_units=[10, 20, 10], n_classes=3)#,feature_columns=feature_columns)
classifier.fit(X_train, y_train, steps=200, batch_size=32)
Out[10]:
In [11]:
iris_predictions = classifier.predict(X_test)
In [12]:
from sklearn.metrics import classification_report,confusion_matrix
In [14]:
print(classification_report(y_test,iris_predictions))