Wednesday, September 27, 2017

Migrating a Model to Keras 2.0

Keras v2.0 has been released for a couple of months now - v2.0.0 released on 5th May, 2017, while the latest version is 2.0.8 at the time of this writing. It brought in a lot of new features and improvements, but also made some syntax changes. Trying to run a code with the old syntax may result in anything from a flood of deprecation warnings, to not being able to run the code at all. Since there are many code examples online which uses the older syntax - including some older posts in Codes of Interest - it's better to know how to get such older syntax model to work on the 2.0 API.

The complete list of changes in Keras v2.0 was extensive, but the following list would help you to narrow down majority of the changes.

The most prominent change is the changing of image_dim_ordering parameter to image_data_format, and its associated values from "tf", and "th" to "channels_last" and "channels_first". We talked about this change in detail in our earlier post "What is the image_data_format parameter in Keras, and why is it important".

Likewise, in all the places where "dim_ordering" argument/parameter was used, it has been changed to "data_format".

All of the Convolution* layers have now need renamed to Conv*.
E.g. Convolution2D is renamed to Conv2D

Make sure you update your imports as well,
E.g. If the older code was
 from keras.layers.convolutional import Convolution2D  
 from keras.layers.convolutional import MaxPooling2D  

Change it to,
 from keras.layers.convolutional import Conv2D  
 from keras.layers.convolutional import MaxPooling2D  

The filter dimensions parameter is now a single tuple.
 model.add(Convolution2D(20, 5, 5,  
changed to,
 model.add(Conv2D(20, (5, 5),  

The border_mode parameter has been changed to padding.
E.g. Change
 model.add(Convolution2D(20, 5, 5, border_mode="same",  
 model.add(Conv2D(20, (5, 5), padding="same",  

In the nb_epoch parameter has been changed to epochs.
E.g.Change, trainLabels, batch_size=128, nb_epoch=20, verbose=1)  
to, trainLabels, batch_size=128, epochs=20, verbose=1)  

These changes would set you up to solve most of the issues encountered when migrating to Keras 2.0.
For a full list of changes, you can check the Keras 2.0.0 Release Notes.

Build Deeper: Deep Learning Beginners' Guide is the ultimate guide for anyone taking their first step into Deep Learning.

Get your copy now!


  1. I was searching for the program through which I could be able to migrate the model to keras and you have just solved my problem by providing the programing of it.

  2. This comment has been removed by the author.

  3. This comment has been removed by the author.

  4. I’m not so good at all this neural networks things, but I’m trying to understand it. Because neural networks and cryptocurrency is two most disputed things in the world of technology now. As we know, market of cryptocurrency is becoming larger and larger every month. Because it is endless field for financial frauds. In difference neural networks are using only by intelligent professionals and for projects with more purpose than only “have more money faster”. That is why your website like Mecca to me and updates like this making me feel that you care about your readers. As I care about people who need papers, that’s why I recommend read the rating of carefully.