Sequential
Linear stack of layers.
model = keras.models.Sequential()
- Methods:
- add(layer): Add a layer to the model.
- compile(optimizer, loss, class_mode="categorical"):
- Arguments:
- optimizer: str (name of optimizer) or optimizer object. See optimizers.
- loss: str (name of objective function) or objective function. See objectives.
- class_mode: one of "categorical", "binary". This is only used for computing classification accuracy or using the predict_classes method.
- theano_mode: A
theano.compile.mode.Mode
(reference) instance controlling specifying compilation options.
- fit(X, y, batch_size=128, nb_epoch=100, verbose=1, validation_split=0., validation_data=None, shuffle=True, show_accuracy=False, callbacks=[]): Train a model for a fixed number of epochs.
- Return: a history dictionary with a record of training loss values at successive epochs, as well as validation loss values (if applicable), accuracy (if applicable), etc.
- Arguments:
- X: data.
- y: labels.
- batch_size: int. Number of samples per gradient update.
- nb_epoch: int.
- verbose: 0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch.
- validation_split: float (0. < x < 1). Fraction of the data to use as held-out validation data.
- validation_data: tuple (X, y) to be used as held-out validation data. Will override validation_split.
- shuffle: boolean. Whether to shuffle the samples at each epoch.
- show_accuracy: boolean. Whether to display class accuracy in the logs to stdout at each epoch.
- callbacks:
keras.callbacks.Callback
list. List of callbacks to apply during training. See callbacks.
- evaluate(X, y, batch_size=128, show_accuracy=False, verbose=1): Show performance of the model over some validation data.
- Return: The loss score over the data.
- Arguments: Same meaning as fit method above. verbose is used as a binary flag (progress bar or nothing).
- predict(X, batch_size=128, verbose=1):
- Return: An array of predictions for some test data.
- Arguments: Same meaning as fit method above.
- predict_classes(X, batch_size=128, verbose=1): Return an array of class predictions for some test data.
- Return: An array of labels for some test data.
- Arguments: Same meaning as fit method above. verbose is used as a binary flag (progress bar or nothing).
- train(X, y, accuracy=False): Single gradient update on one batch. if accuracy==False, return tuple (loss_on_batch, accuracy_on_batch). Else, return loss_on_batch.
- Return: loss over the data, or tuple
(loss, accuracy)
if accuracy=True
.
- test(X, y, accuracy=False): Single performance evaluation on one batch. if accuracy==False, return tuple (loss_on_batch, accuracy_on_batch). Else, return loss_on_batch.
- Return: loss over the data, or tuple
(loss, accuracy)
if accuracy=True
.
- save_weights(fname, overwrite=False): Store the weights of all layers to a HDF5 file. If overwrite==False and the file already exists, an exception will be thrown.
- load_weights(fname): Sets the weights of a model, based to weights stored by saveweights. You can only loadweights on a savefile from a model with an identical architecture. load_weights can be called either before or after the compile step.
Examples:
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD
model = Sequential()
model.add(Dense(64, 2, init='uniform'))
model.add(Activation('softmax'))
model.compile(loss='mse', optimizer='sgd')
'''
Demonstration of verbose modes 1 and 2
'''
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=1)
'''
Train on 37800 samples, validate on 4200 samples
Epoch 0
37800/37800 [==============================] - 7s - loss: 0.0385
Epoch 1
37800/37800 [==============================] - 8s - loss: 0.0140
Epoch 2
10960/37800 [=======>......................] - ETA: 4s - loss: 0.0109
'''
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=2)
'''
Train on 37800 samples, validate on 4200 samples
Epoch 0
loss: 0.0190
Epoch 1
loss: 0.0146
Epoch 2
loss: 0.0049
'''
'''
Demonstration of show_accuracy
'''
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, verbose=2, show_accuracy=True)
'''
Train on 37800 samples, validate on 4200 samples
Epoch 0
loss: 0.0190 - acc.: 0.8750
Epoch 1
loss: 0.0146 - acc.: 0.8750
Epoch 2
loss: 0.0049 - acc.: 1.0000
'''
'''
Demonstration of validation_split
'''
model.fit(X_train, y_train, nb_epoch=3, batch_size=16, validation_split=0.1, show_accuracy=True, verbose=1)
'''
Train on 37800 samples, validate on 4200 samples
Epoch 0
37800/37800 [==============================] - 7s - loss: 0.0385 - acc.: 0.7258 - val. loss: 0.0160 - val. acc.: 0.9136
Epoch 1
37800/37800 [==============================] - 8s - loss: 0.0140 - acc.: 0.9265 - val. loss: 0.0109 - val. acc.: 0.9383
Epoch 2
10960/37800 [=======>......................] - ETA: 4s - loss: 0.0109 - acc.: 0.9420
'''