Professional Documents
Culture Documents
Keras
Keras
Alvaro Soto
A. Soto DCC 1 / 15
Deep Learning Software Tools
A. Soto DCC 2 / 15
Keras
A. Soto DCC 3 / 15
Keras Modularity
A. Soto DCC 4 / 15
Keras Models
A. Soto DCC 5 / 15
Keras Python Style Calls
A. Soto DCC 6 / 15
Example Sequential Model
A. Soto DCC 7 / 15
Example Sequential Model
Add a module that implements a dense connected layer with 100 inputs and
100 outputs.
from keras.models import Sequential
model = Sequential()
model.add(Dense(output_dim=100, input_dim=100,
init="glorot_uniform"))
A. Soto DCC 8 / 15
Example Sequential Model
A. Soto DCC 9 / 15
Example Sequential Model
A. Soto DCC 10 / 15
Case example: Recall from first classes MNIST dataset
A. Soto DCC 11 / 15
A More Complex Example Using MNIST Dataset
Let’s first define house keeping code to load and pre-process input data. You
DO NOT NEED to understand this code:
from __future__ import print_function
import numpy as np
np.random.seed(1337) # for reproducibility
batch_size = 128
nb_classes = 10
nb_epoch = 20
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
A. Soto DCC 12 / 15
Now let’s define the deep learning model. You DO NEED to understand this
code (model definition):
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation(’relu’))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation(’relu’))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation(’softmax’))
model.summary()
A. Soto DCC 13 / 15
Finally, let’s run the model. You should know the basic commands:
#This function define the loss function, SGD algorithm, and metric to measure model performance.
model.compile(loss=’categorical_crossentropy’,
optimizer=RMSprop(),
metrics=[’accuracy’])
#This function execute the learning process. ’history’ stores learning evolution.
history = model.fit(X_train, Y_train,
batch_size=batchSize, nb_epoch=nEpochs,
verbose=1, validation_data=(X_test, Y_test))
#After training is done, we can evaluate the performance of the model using a test set.
score = model.evaluate(X_test, Y_test, verbose=0)
A. Soto DCC 14 / 15
Results
A. Soto DCC 15 / 15