Keras vs TensorFlow

Anais Dotis
5 min readJun 5, 2018

A comparison of the two.

In this post I intend to answer the following questions about Keras and TensorFlow:

I. What are they?

II. How do they work?

III. What are the advantages and disadvantages?

IV. When should I use them?

I. What are they?

Keras is an open source neural network API written in Python. It can run on top of several deep learning and machine learning frameworks, including TensorFlow (TF), Microsoft Cognitive Tool (CNTK), and Theano.

TensorFlow is an open source software library for high performance symbolic-numeric computation. It is also used for deep learning applications like neural networks. Tensor flow was developed by Google Brain team and is written in Python, C++, and CUDA (Nvidia’s language for programming GPUs). It can be deployed across a wide variety of platforms (CPUs, GPUs, TPUs).

II. How do they work?

Keras is an interface rather than a standalone framework, like TensorFlow. It offers high-level, intuitive abstractions that enable fast experimentation.

TensorFlow gets its name from its functionality. It allows users to define how data, tensors, flow through a system.

The following analogies help to illustrate Keras’ function and purpose. If you have used seaborn, matplotlip, plotly.js, or d3.js this section is for you. Otherwise, skip to part III.

Analogy One: Keras is to TensorFlow as seaborn is to matplotlib

Seaborn is a high-level interface based off of matplotlib. If I wanted to make a scatterplot with a linear regression fit with x and y labels using seaborn, I would only need 3 lines:

import seaborn as snsdf = sns.load_dataset('dataset')
sns.regplot(x=df["independent"], y=df["dependent"])
sns.plt.show()

However, if I wanted to create the same plot in matplotlib I would have do something like:

import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(10,10))
plt.xlabel("independent")
plt.ylabel("dependent")
plt.grid()
m, b = np.polyfit(x, y, 1)
plt.plot(x, y, '.')
plt.plot(x, m*x + b, '-')

Seaborn is the simpler and efficient way to produce a standard scatterplot graph with all of the expected features. However, if I wanted to make a scatterplot without a fit or labels matplotlib would be more efficient. Likewise, Keras is the way to go if you want to produce a functional model with tried and true methodology.

Analogy Two: Keras is to TensorFlow as plotly.js is to d3.js

If you wanted to build a simple bar graph comparing the “amount” in each “category” using d3.js, your js file would probably look something like this:

var svgWidth = 960;
var svgHeight = 500;
var chartMargin = {
top: 30,
right: 30,
bottom: 30,
left: 30
};
var chartWidth = svgWidth - chartMargin.left - chartMargin.right;
var chartHeight = svgHeight - chartMargin.top - chartMargin.bottom;
var svg = d3.select("body")
.append("svg")
.attr("height", svgHeight)
.attr("width", svgWidth)
var chartGroup = svg.append("g")
.attr("transform", "translate(" + chartMargin.right + ", " + chartMargin.top + ")");
var xBandScale = d3.scaleBand().range([0, chartWidth]).padding(0.1);
var yLinearScale = d3.scaleLinear().range([chartHeight, 0]);
d3.csv("data.csv", function (error, Data) {
if (error) throw error;
console.log(tvData);
Data.forEach(function (data) {
data.amount = +data.amount;
});
xBandScale.domain(Data.map(function (data) {
return data.category;
}));
yLinearScale.domain([0, d3.max(Data, function (data) {
return data.y;
})]);
var bottomAxis = d3.axisBottom(xBandScale);
var leftAxis = d3.axisLeft(yLinearScale).ticks(10);
chartGroup.selectAll(".bar")
.data(Data)
.enter()
.append("rect")
.attr("class", "bar")
.attr("x", function (data) {
return xBandScale(data.category);
})
.attr("y", function (data) {
return yLinearScale(data.amount);
})
.attr("width", xBandScale.bandwidth())
.attr("height", function (data) {
return chartHeight - yLinearScale(data.amount);
});
chartGroup.append("g")
.call(leftAxis);
chartGroup.append("g")
.attr("transform", "translate(0, " + chartHeight + ")")
.call(bottomAxis);
});

Using plotly.js is much simpler. It would look like:

var data = [{
x: ['category one', 'category two', 'category three'],
y: [5, 14, 23],
type: 'bar'
}];

Plotly.newPlot('myDiv', data);

Plotly is obviously easier, but it’s limited. If you want to create stunning visualizations, you have to use d3. If you want to create perfectly functional, simple, user friendly graphs, use plotly. Similarly, if you want complete creative control, use TensorFlow. If you’re looking for a quick and easy route to a functional product, take Keras.

III. What are the advantages and disadvantages?

As described by Keras’ skilled marketing team, the advantages of Keras include:

  1. User friendliness

It…

Prioritizes user experience. Minimizes cognitive load. Constant. Simple. Reduces the number of user actions. Popular. Backed by a large and active community.

2. Modularity and Easy extensibility

Neural layers, functions, schemes, and optimizers, are all standalone modules that you can combine to create new models if you please.

3. Work with Python

Pretty self explanatory. Python code = compact, easier to debug, and allows for ease of extensibility.

Disadvantages of Keras:

  1. Limited control

Keras is a beautifully written API that doesn’t block access to lower level frameworks. That being said, Keras isn’t really designed for you to change the underlying architecture of your model. You can customize your layers in Keras (here’s a great example), but it might make sense for you to just use TensorFlow. Depending on what you’re trying to do, you might have implement your training routine outside of Keras.

Advantages of TensorFlow:

  1. Powerful

TensorFlow can be applied toward a variety of use cases. It can be used to build nearly anything that involves processing data by running a series of mathematical operations on it. However, TensorFlow is most commonly used to build neural nets, and it takes the lead in deep learning. If you’re trying to do simple machine learning, you might just want to stick to scikit-learn. If you’re working with small datasets, TensorFlow might be overkill.

The Great Wave off Kanagaw as interpreted by Google’s famous DeepDream which employs TensorFlow

2. Versitile

TensorFlow not only gives you complete control over your model, but also over your preprocessing logic. Last year, Google announced TensorFlow Transform which allows the user to define preprocessing pipelines which support full passes over the data for large-scale, efficient, distributed data processing. You can learn more here.

Disadvantages of TensorFlow:

  1. Harder

With great power comes great frustration. Expect more syntactical errors, blank stares, stack overflow time, and utter confusion.

IV. When should I use them?

If you’re a researcher or a deep learning guru, then you might use Keras for a first run because it enables fast execution of prototypes and POC experiments. Then you might move on to TensorFlow to gain complete control over the steps and layers in your network. You can also gain real time insights into the structure using the debugger.

If you’re looking to enter a Kaggle competition or tackle a hackathon, Keras is probably your best bet. If you’re a newbie looking to have fun with deep learning who’s only equipped with a superficial understanding of neural nets, Keras is right for you.

“Magical and absurd TensorFlow contained by beautiful Keras wrapping” — Nathan Newbie, local newb

I hope this helped illustrate the some of the differences between Keras and TensorFlow. If you have any objections or questions, please comment. I also recommend checking out this blog for more info.

--

--