From Scikit-learn to TensorFlow : Part 1 | by Karthik M Swamy | Towards Data Science

Over the past year and a half, TensorFlow has grown at a tremendous pace, both in terms of adoption rate and in compute speed.
TensorFlow has established itself as the most sought after library for machine learning (ML) algorithm development. However, it seems to have also established itself as a library requiring definitions for sessions and graphs that are rather tedious and time consuming. Although it does a great job at efficiently computing gradients to train CNNs, RNNs and LSTMs, that’s not all.
In this series of posts, I want to discuss how TensorFlow can be used as a general purpose ML library. More specifically, we will discuss how it is similar to scikit-learn, another ML library immensely popular among data scientists and developers alike.
While scikit-learn has highly-optimised algorithms in its armoury, it lacks the ability to scale-up when faced with a large number of data points. However, TensorFlow provides quite a number of advantages over scikit-learn:
High performance ML modules
Customisability
Scaling-up to cater to large data points
Ability to utilise GPUs and train across geographically distributed GPU devices
Device agnostic compute
Leverage Google Cloud to make inferences on a trained ML model
Highly flexible Apache 2.0 license while scikit-learn is on BSD license, (although both are commercially usable, Apache 2.0 is less prone to patent litigations)
Understand functionalities that are similar between scikit-learn and TensorFlow which will allow scikit-learn users to seamlessly use TensorFlow.
Develop a program to classify flower varieties from the Iris flowers dataset using scikit-learn and TensorFlow to understand the effort required to build such a system.
Showcase how easy TensorFlow could be for prototyping new ideas.
One of the reason for scikit-learn’s popularity is owing to its simple
classifier.fit() / classifier.predict() 
methods that remain the same for any classifier that is used. This simple user experience has allowed developers to concentrate on the algorithm and its parameters rather than worry about the APIs that need to be called to accomplish the task at hand.
On the other hand, we have High-level API in TensorFlow that was inspired by scikit-learn. These functions in TensorFlow work very similar to scikit-learn, with similar fit and predict methods along with other functionalities that allow further fine-tuning.
Before we delve into developing our classification framework using TensorFlow’s High-level API calls, let’s discuss TensorFlow’s low-level computation framework. TensorFlow uses computation graphs to perform all its computations. Computations are represented as an instance of the tf.Graph object where the data is represented as tf.Tensor object and operations on such tensor objects using the tf.Operation object. The graph is then executed in a session using the tf.Session object. As it is evident, creating a classification framework using TensorFlow’s low-level API would require an effort to test out a simple ML prototype. This is one of the reasons why we discuss TensorFlow’s high-level API to compare with scikit-learn’s API. We will discuss about the low-level APIs and its usage in later posts.
We build a classifier using scikit-learn’s SVM module and TensorFlow’s High-Level API to classify flowers based on features of the flower. In this case, the dataset provides 4 different features such as sepal width, sepal length, petal width and petal length to classify the flower into one of the three flower species (Iris setosa, Iris versicolor, and Iris virginica.)
The code for this project is available on my GitHub page.
If we look at the notebook on the GitHub link above, we can see that the data load and split functionalities are shared by both the frameworks. We define an SVM classifier in scikit-learn as follows:
# ------------------------------------------# Scikit Learn Implementation# ------------------------------------------# Use support vector classificationclassifier_sk = svm.SVC()# Use the train data to train this classifierclassifier_sk.fit(x_train, y_train)# Use the trained model to predict on the test datapredictions = classifier_sk.predict(x_test)score = metrics.accuracy_score(y_test, predictions)
In the code snippet above, we simply define a support vector classifier svm.SVC(), the object of which is used for training and prediction. Training is achieved using the fit() while prediction is achieved using the predict() method call. We finally calculate the accuracy value in the final line of this four line code snippet.
On the other hand, in TensorFlow we can use a deep neural network (DNN) classifier for the same task. We use the DNNClassifier available under TensorFlow’s contrib module as follows:
# ------------------------------------------# TensorFlow Implementation# ------------------------------------------# Extract the features from the training datafeats = tf.contrib.learn.infer_real_valued_columns_from_input(x_train)# Building a 3-layer DNN with 50 units each.classifier_tf = tf.contrib.learn.DNNClassifier(feature_columns=feats,                                                hidden_units=[50, 50, 50],                                                n_classes=3)# Use the train data to train this classifierclassifier_tf.fit(x_train, y_train, steps=5000)# Use the trained model to predict on the test datapredictions = list(classifier_tf.predict(x_test, as_iterable=True))score = metrics.accuracy_score(y_test, predictions)
In the code snippet above, we can see how we can build a DNN for the almost the same number of lines (except for the additional line for converting the input data into features.) This additional line accomplishes the task of converting the input data into dense, fixed-length float values.
While the task of moving from one framework to another is often daunting and at times frustrating, TensorFlow seems to have a bridge that allows developers to transition smoothly move in from a different framework. The contrib.learn module provides such a bridge that allows a familiar path from scikit-learn or Keras, into TensorFlow.
TensorFlow is also accommodating enough to include readers that are highly optimised for production quality usage while at the same time supports most file formats that the ML community uses. This allows for developers to avoid depending on other frameworks such as Pandas (which does the heavy-lifting of reading files but still requires manually feeding the data into the ML framework.) TensorFlow readers were also shown to be high performance at the TensorFlow Dev Summit held earlier this year.
In this post, we began our exploration into developing a classifier using scikit-learn and TensorFlow for accomplishing a simple task. We discussed how the High-Level TensorFlow API is similar to scikit-learn’s API. You can access the code discussed in this blog at my GitHub page.
In the next post, I’m planning to introduce some more scikit modules and its TensorFlow counterparts. Do you have anything specific in mind? Tweet to me @krtk or connect with me on LinkedIn.