0% found this document useful (0 votes)
44 views12 pages

Keras

This document provides an overview of basic concepts for defining and training models with Keras, including: - Specifying model architecture using Sequential models and stacking layers like Dense and InputLayer - Compiling models by setting the optimizer, loss function, and metrics - Fitting models by passing training data, validation data, number of epochs, and batch size - Using callbacks like EarlyStopping and ModelCheckpoint to interact with and monitor training

Uploaded by

Achraf Boura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views12 pages

Keras

This document provides an overview of basic concepts for defining and training models with Keras, including: - Specifying model architecture using Sequential models and stacking layers like Dense and InputLayer - Compiling models by setting the optimizer, loss function, and metrics - Fitting models by passing training data, validation data, number of epochs, and batch size - Using callbacks like EarlyStopping and ModelCheckpoint to interact with and monitor training

Uploaded by

Achraf Boura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Basics of keras

Romain Tavenard
Specifying model architecture
The Sequential model class

I Sequential: easiest way to code a simple model in keras


I Requirements
I Single input (can be multidimensional)
I Single output (can be multidimensional)
I model is just a stack of layers (no loop in the model graph)

from tensorflow.keras.models import Sequential

model = Sequential(
[
# Here, we will put a list of layers
]
)
Layers used to define an MLP
I InputLayer (not mandatory, link to docs)
I Dense (link to docs)
I Can be used for both internal and output layers
I activation is the activation function (str)

from tensorflow.keras.models import Sequential


from tensorflow.keras.layers import InputLayer, Dense

model = Sequential(
[
# Input layer (16-dimensional)
InputLayer(input_shape=(16, )),
# Hidden layer (256 neurons, ReLU activation)
Dense(units=256, activation="relu"),
# Output layer (1 neuron, sigmoid activation)
Dense(units=1, activation="sigmoid")
]
)
Fitting a model
Setting optimization strategy (1/2)

I compile step
I optimizer (required)
I can be a string ("sgd", "adam", . . . )
I can be a keras optimizer instance (allows to set learning rate,
...)
I loss (required) can be a string
I "mse": Mean Squared Error
I "binary_crossentropy": Cross-entropy in the binary case
(single output neuron)
I "categorical_crossentropy": Cross-entropy in the
multiclass case (multiple output neurons)
I metrics (optional) is a list
I additional metrics to be reported during model training (eg.
"accuracy" for classification tasks)
Setting optimization strategy (2/2)

model = Sequential(
[
# Layers here
]
)

model.compile(
optimizer="sgd",
loss="mse",
metrics=[]
)
Setting fit options (1/2)

I fit step
I First 2 arguments: training data (required)
I validation_data (optional) is a tuple
I validation set organized as a pair (X , y )
I epochs: number of epochs (required)
I batch_size: number of samples per batch (strongly
recommended)
Setting fit options (2/2)

model = Sequential(
[
# Layers here
]
)

model.compile(
# Compile options
)
model.fit(X, y,
validation_data=(X_val, y_val),
epochs=10,
batch_size=64)
Callbacks
Callbacks in general

I Callbacks are ways to “interact” during training


I eg. report statistics, early stopping, save models
I at each minibatch or at each epoch

I A list of callbacks can be passed at fit time:


model.fit(X, y,
validation_data=(X_val, y_val),
epochs=10,
batch_size=64,
callbacks=[cb1, cb2, ...])

I See docs for usage (link)


I EarlyStopping
I ModelCheckpoint
I TensorBoard
History callback

I Returned by fit by default (no need to set up)


I has a history attribute
I dict
I losses and metrics recorded during fit at each epoch
h = model.fit(X, y,
validation_data=(X_val, y_val),
epochs=10,
batch_size=64)
print(h.history)
# {
# "loss": [...],
# "val_loss": [...],
# "accuracy": [...],
# "val_accuracy": [...]
# }

You might also like