Getting Started with Keras


In [1]:
#### from: https://keras.io/getting-started/sequential-model-guide/

In [41]:
from keras.models import Sequential
from keras.layers import Dense, Activation

In [42]:
model = Sequential([
    Dense(32, input_dim=784), # specify input shape; other layers' shapes configure themselves
    Activation('relu'),
    Dense(10),
    Activation('softmax')
])

The Merge Layer


In [43]:
# multiple sequential instances can be merged into a single output via merge, e.g.: 
from keras.layers import Merge

left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))

right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))

merged = Merge([left_branch, right_branch], mode='concat')

final_model = Sequential()
final_model.add(merged)
final_model.add(Dense(10, activation='softmax'))

In [45]:
# such a two-branch model can be trained via, e.g.:
final_model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
# final_model.fit([input_data_1, input_data_2], targets)

Compilation


In [46]:
# before training a moel, need to configure learning process with 'compile' method, which has three arguments:
# (1.) optimizer (e.g., adagrad)
# (2.) loss function (e.g., mean squared error)
# (3.) list of metrics (e.g., accuracy)

model.compile(optimizer='rmsprop',
             loss='categorical_crossentropy',
             metrics=['accuracy'])

Training


In [48]:
# Keras models are trained on Numpy arrays of input data and labels, typically with the 'fit' function. 
# Here is a standalone model with a binary output: 

model = Sequential()
model.add(Dense(1, input_dim=784, activation='sigmoid'))
model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

# generate dummy data
import numpy as np
data = np.random.random((1000, 784))
labels = np.random.randint(2, size=(1000,1))

# train the model, iterating on the data in batches of 32 samples
model.fit(data, labels, nb_epoch=10, batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 0.7302 - acc: 0.4960     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 0.7243 - acc: 0.5040     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 0.7073 - acc: 0.5300     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 0.7036 - acc: 0.5310     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 0.6897 - acc: 0.5530     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 0.6851 - acc: 0.5590     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 0.6786 - acc: 0.5890     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 0.6713 - acc: 0.5690     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 0.6684 - acc: 0.5900     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 0.6573 - acc: 0.5920     
Out[48]:
<keras.callbacks.History at 0x11c08e750>

In [49]:
# for a multi-input model with 10 classes:

left_branch = Sequential()
left_branch.add(Dense(32, input_dim=784))

right_branch = Sequential()
right_branch.add(Dense(32, input_dim=784))

merged = Merge([left_branch, right_branch], mode='concat')

model = Sequential()
model.add(merged)
model.add(Dense(10, activation='softmax'))

model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# generate dummy data
import numpy as np
from keras.utils.np_utils import to_categorical
data_1 = np.random.random((1000, 784))
data_2 = np.random.random((1000, 784))

# these are integers between 0 and 9
labels = np.random.randint(10, size=(1000, 1))
# we convert the labels to a binary matrix of size (1000, 10)
# for use with categorical_crossentropy
labels = to_categorical(labels, 10)

# train the model
# note that we are passing a list of Numpy arrays as training data
# since the model has 2 inputs
model.fit([data_1, data_2], labels, nb_epoch=10, batch_size=32)


Epoch 1/10
1000/1000 [==============================] - 0s - loss: 2.9704 - acc: 0.0980     
Epoch 2/10
1000/1000 [==============================] - 0s - loss: 2.6440 - acc: 0.1120     
Epoch 3/10
1000/1000 [==============================] - 0s - loss: 2.4948 - acc: 0.1370     
Epoch 4/10
1000/1000 [==============================] - 0s - loss: 2.3542 - acc: 0.1890     
Epoch 5/10
1000/1000 [==============================] - 0s - loss: 2.2657 - acc: 0.2010     
Epoch 6/10
1000/1000 [==============================] - 0s - loss: 2.1394 - acc: 0.2280     
Epoch 7/10
1000/1000 [==============================] - 0s - loss: 2.0296 - acc: 0.2820     
Epoch 8/10
1000/1000 [==============================] - 0s - loss: 1.9632 - acc: 0.3130     
Epoch 9/10
1000/1000 [==============================] - 0s - loss: 1.8570 - acc: 0.3610     
Epoch 10/10
1000/1000 [==============================] - 0s - loss: 1.7846 - acc: 0.3850     
Out[49]:
<keras.callbacks.History at 0x11d874f50>

In [ ]: