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')
])
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)
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'])
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)
Out[48]:
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)
Out[49]:
In [ ]: