Monday, May 22, 2017

Deep Learning and Keras

Ok, let's talk about one wonderful subject: Deep Learning.
Every time you go to Google Translate you use Deep Learning. Every time you use technology, like the Chatbot I was talking about in my previous post, you use Deep Learning.
The promises of AI are coming back. Now almost reality. Exciting.
 
In one of my previous posts I talked about one of the most promising framework for Deep Learning in Python: TensorFlow.

TensorFlow is powerful, but it is a little bit low-level. Good if you want to enter in details and control, efficiently, all the aspects.

A really interesting "Frond-End" framework, born to simplify usage of Deep Models is:


Keras normally use TensorFlow as backend, but it can be configured to use Theano.
With Keras it is easier to design, compile and run a model. It is easy to switch from CPU to GPU.
 In the following gist, with code taken from Keras public documentation, you see how easy is to define, train, test a Neural Netwok Model.
In the documentation you find also full fledged examples, with data (for example MNIST).
One word of caution: if you try more complicated models, with many layers, the training can take too long on CPU. I'll return back.

from keras.models import Sequential
from keras.layers import Dense, Activation
model = Sequential()
# this way you define the architecture of your Neural Network
# adding layers and activation functions
model.add(Dense(units=64, input_dim=100))
model.add(Activation('relu'))
model.add(Dense(units=10))
model.add(Activation('softmax'))
# In Keras you have to compile the model, before the actual training
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
# training of the model
# x_train and y_train are Numpy arrays --just like in the Scikit-Learn API.
model.fit(x_train, y_train, epochs=5, batch_size=32)
# evaluation of Model's performances
loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128)
view raw KerasFirts hosted with ❤ by GitHub

No comments:

Post a Comment