Keras Backend – Tensorflow and Theano
Free Keras course with real-time projects Start Now!!
Keras supports multiple backends, although the performance of your neural network may vary for different Keras backends. In this article, we will study two of the most commonly used Keras backends i.e TensorFlow and theano. This article will explain how to change the backend of Keras.We will also create a demo neural network model and test its performance on both the backends.
Keras backends
Keras, being a high-level API for developing neural networks, does not handle low-level computations. For these low-level tasks, Keras relies on “backend engines”. Keras provides this backend support in a modular way, i.e. we can attach multiple backends with Keras.
Tensorflow and Theano are commonly used Keras backends.
1. Tensorflow
It is an open-source machine learning platform developed by Google and released in November 2015.
2. Theano
It is a Python library used for manipulating and evaluating a mathematical expression, developed at the University of Montreal and released in 2007.
Changing Keras backends
By default, Keras contains a TensorFlow backend. If you want to check the backend, go to Keras configuration file at :
$HOME/.keras/keras.json
It looks like:
{ "image_data_format": "channels_last", "epsilon": 1e-07, "floatx": "float32", "backend": "tensorflow" }
Or, import keras and type:
keras.backend.backend()
To change the Keras backend, follow the below steps:
- Open the configuration file in any text editor, I prefer sublime text.
subl $HOME/.keras/keras.json
- Edit the backend to “theano”
- Check the backend again using
keras.backend.backend()
It takes one step to change the backend. You do not have to change any line of code of your model, and you can run or test your Keras model on different backends, which we will do next.
Test your model on different Keras backends
Here, we will create a multi-level perceptron neural network for binary classification. We will test the performance of our model on the basis of model training time and the accuracy of the model.
Firstly, we will use the TensorFlow backend and test the model performance.
For this case study, I am using
Intel core i5 7 th gen cpu processor ,
keras version 2.3.1 ,
python 3.6.0 ,
tensorflow 1.14.0
and theano version 1.0.4.
The results are relative and may vary in accordance with the systems configuration and different versions of the above libraries.
Create the neural network:
import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout
# generate random input for testing purpose
x_train=np.random.random((1000,20)) y_train=np.random.randint(2,size=(100,1)) x_test=np.random.random((100,20)) y_test=np.random.randint(2,size=(100,1))
# create model
model=Sequential() model.add(Dense(64, input_dim=20,activation=’relu’)) model.add(Dropout(0.5)) model.add(Dense(64, activation=’relu’)) model.add(Dropout(0.5)) model.add(Dense(1,activation=’sigmoid’))
#compile your model
model.compile(loss=’binary_crossentropy’, optimizer=’rmsprop’,metrics=[‘accuracy’])
# to calculate the training time , use python time module and calculate the difference of the time instances after and before training the model
import time start=time.time() model.fit(x_train,y_tarin,epochs=20,batch_size=128) end=time.time() print(“running time: ”,end-start)
# calculate the loss value and accuracy of model
score=model.evaluate(x_test,y_test,batch_size=128) print(“score: ”,score)
You can see model took, 0.5446 seconds for training, has a loss of 0.6998 and accuracy of 0.4199
Now switch to theano backend, and run the same program again and check its training time and accuracy,
The model took a training time of 1.066 seconds, has a loss value of 0.6747 and accuracy of 0.6700
So, on my system, TensorFlow takes less time in training the model but theano has better accuracy.
Summary
This article explains how Keras support different backend engines and how to switch among these backends. Here we studied TensorFlow and theano as Keras backend engines. It also shows a simple comparison between the neural network model trained on both the backends.
Hope you are enjoying Keras tutorial series. If you are facing issues with Python, check DataFlair Python course to learn complete python.
Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google