Keras Cheatsheet [2025 Updated] - Download pdf

Last Updated : 23 Jul, 2025

Deep Learning is changing many industries by helping systems learn from data and make smart decisions without being directly programmed. It is changing how companies operate in industries including healthcare, entertainment, and more, resulting in innovative solutions and automation. Having the appropriate tools is crucial for building robust models in this rapidly expanding industry. Keras is one of the most widely used and user-friendly deep learning technologies in Python. Keras is popular among both novices and experts due to its ease of use and flexibility in creating, training, and utilizing robust neural networks.

Keras-Cheat-Sheet
Keras Cheat-Sheet

In this article, we’ll provide a Keras Cheat-Sheet that highlights the library's key features and functions. This cheat sheet will be a useful guide to help you easily build deep learning models, covering everything from setting up models to training and improving them.

What is Keras?

Keras is an easy-to-use library for building and training deep learning models. It provides a simple way to create complex neural networks without dealing with complicated details. Keras works with TensorFlow, which helps to run the models. You can use Keras to build different types of models, like those for image recognition or analyzing text. Its clear and straightforward design makes it a popular choice for beginners and experts who want to quickly try out new ideas in deep learning.

Keras Cheat-Sheet

Keras is an easy-to-use library for building and training neural networks. It helps create complex models for tasks like image recognition, language processing, and more. Keras provides simple ways to design models using two main types of tools: Sequential (for basic models) and Functional (for more complex ones). It works well with TensorFlow, which helps run the models efficiently. Keras is popular because it’s simple to learn yet powerful enough for experts to use for serious deep-learning projects.

Download the Cheat-Sheet here: Keras Cheat-Sheet

Model Creation

Function

Description

Sequential()

Creates a linear stack of layers, useful for simple models.

Model()

Used to create a more flexible model by specifying inputs and outputs.

Layer Types

Function

Description

Dense()

Creates a fully connected layer, where each neuron is connected to every neuron in the previous layer.

Conv2D()

Creates a 2D convolutional layer, used for processing images.

MaxPooling2D()

Performs max pooling operation, reducing the size of the data.

LSTM()

Creates a Long Short-Term Memory (LSTM) layer, useful for sequential data like text.

Dropout()

Adds dropout regularization to prevent overfitting by randomly setting a fraction of inputs to zero.

Embedding()

Creates an embedding layer, useful for working with categorical or sequential data, such as words.

Flatten()

Flattens the input data, often used to convert the output of convolutional layers to a 1D array for fully connected layers.

GlobalAveragePooling2D()

Applies global average pooling to 2D data, reducing the size by averaging across all the spatial dimensions.

Concatenate()

Merges multiple models or layers into one layer.

Model Training

Function

Description

compile()

Configures the model for training by specifying the optimizer, loss function, and metrics.

fit()

Trains the model on the input data for a specified number of epochs.

fit_generator()

Trains the model on data generated in real time by a Python generator.

evaluate()

Evaluates the performance of the model on a dataset and returns the loss and metrics.

evaluate_generator()

Evaluates the model using a data generator, useful for large datasets that don't fit in memory.

predict()

Uses a trained model to make predictions on new data.

train_on_batch()

Trains the model on a single batch of data at a time.

test_on_batch()

Tests the model on a single batch of data.

Callbacks

Function

Description

EarlyStopping()

Stops training early when a monitored metric stops improving.

ModelCheckpoint()

Saves the model after each epoch if it has improved, useful for continuing training later.

ReduceLROnPlateau()

Reduces the learning rate when a metric has stopped improving.

TensorBoard()

Logs training metrics to visualize in TensorBoard, useful for tracking progress.

Loss Functions

Function

Description

binary_crossentropy()

Computes binary cross-entropy loss, used for binary classification tasks.

categorical_crossentropy()

Computes categorical cross-entropy loss, used for multi-class classification tasks.

mean_squared_error()

Computes the mean squared error loss, used for regression tasks.

Metrics

Function

Description

accuracy()

Computes the accuracy of the model, often used for classification tasks.

AUC()

Computes the area under the ROC curve, used for binary classification tasks.

Hands-on Practice with Keras

Importing Keras

Import the essential modules to work with Keras for building models, adding layers, optimizing, and more.

Python
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Activation, Flatten, Conv2D, MaxPooling2D, LSTM
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.preprocessing.image import ImageDataGenerator


Model Creation

a. Sequential Model: Used for simple, linear stacks of layers where each layer has exactly one input and one output.

Python
model = Sequential()


b. Functional Model: Allows for more complex architectures, such as multiple inputs or outputs, or shared layers across the network.

Python
input = Input(shape=(input_shape))
x = Dense(64)(input)
output = Dense(1)(x)
model = Model(inputs=input, outputs=output)


Adding Layers

a. Dense Layer: A fully connected layer where each neuron is connected to every neuron in the previous layer. This is typically used in feedforward neural networks.

Python
model.add(Dense(units=64, activation='relu', input_shape=(input_size,)))


b. Convolutional Layer (Conv2D): Used in convolutional neural networks (CNN) for processing image data by applying filters that detect features like edges, textures, etc.

Python
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(height, width, channels)))


c. Max Pooling Layer (MaxPooling2D): Reduces the spatial dimensions (height, width) of the image, keeping the most important information and reducing computation time.

Python
model.add(MaxPooling2D(pool_size=(2, 2)))


d. Flatten Layer: Converts multi-dimensional data into a one-dimensional vector, often used before the final dense layer in CNNs.

Python
model.add(Flatten())


Learn more about the layers in a neural netrowk in this article: Layers in Deep Learning

Model Compilation

This step configures the model for training by specifying the optimizer (which adjusts weights), the loss function (which calculates how far off the predictions are), and the metrics (which track the model's performance during training).

Python
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])


Model Training

This trains the model using the input data (X_train) and corresponding labels (y_train). The model is evaluated after each epoch on the validation data (X_val and y_val) to see how it generalizes.

Python
history = model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))


Model Evaluation

After training, this step measures how well the model performs on test data (X_test, y_test) to determine its accuracy and loss.

Python
loss, accuracy = model.evaluate(X_test, y_test)


Model Prediction

Once the model is trained, use it to predict the output for new or unseen data (X_input), such as classifying an image or generating a recommendation.

Python
predictions = model.predict(X_input)


Callbacks

Early Stopping: Automatically stops the training if the model's performance on the validation set stops improving. This prevents overfitting and saves computation time.

Python
early_stop = EarlyStopping(monitor='val_loss', patience=5)


Data Augmentation

ImageDataGenerator: This technique artificially increases the amount of data by applying random transformations (like rotation or shifting) to the original images, helping the model generalize better.

Python
datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2)
datagen.fit(X_train)


Saving and Loading Models

a. Save Model: This saves the entire model (including architecture, weights, and training configuration) to a file so it can be reloaded later for use or continued training.

Python
model.save('model.h5')


b. Load Model: This loads a saved model from a file, so you can use it for prediction or further training without retraining from scratch.

Python
from tensorflow.keras.models import load_model
model = load_model('model.h5')


Custom Loss Function

A custom loss function can be created to calculate the difference between the true and predicted values in a way that suits your specific model or data.

Python
import tensorflow as tf

def custom_loss(y_true, y_pred):
    return tf.reduce_mean(tf.square(y_true - y_pred))


Custom Layer

If you need a unique operation in your network, you can define a custom layer that performs specific functions during training or inference.

Python
from tensorflow.keras import layers

class CustomLayer(layers.Layer):
    def __init__(self):
        super(CustomLayer, self).__init__()

    def call(self, inputs):
        return inputs * 2


Saving Model Weights

This saves only the weights of the trained model to a file. This is useful if you want to preserve the model parameters but modify the architecture later.

Python
model.save_weights('model_weights.h5')


Load Model Weights

This loads the saved weights into a model. You need to have the same architecture when loading the weights.

Python
model.load_weights('model_weights.h5')


Optimizers

a. Adam Optimizer: A popular optimizer that combines the advantages of other optimizers like AdaGrad and RMSProp, adjusting the learning rate during training for faster convergence.

Python
optimizer = Adam(learning_rate=0.001)


b. SGD Optimizer: A traditional optimizer where the model's weights are updated after each mini-batch of data. Momentum can be added to help accelerate convergence.

Python
optimizer = SGD(learning_rate=0.01, momentum=0.9)


Conclusion

In conclusion, Keras is a user-friendly and powerful tool for building and training deep learning models. It makes complex tasks like creating, training, and improving models much easier. Whether you're just starting or already experienced, Keras gives you many options with different types of layers, optimization methods, and tools to make your models better. By working well with TensorFlow, Keras helps you quickly deploy your models and use them in real-world applications. Whether you're working with images, text, or other data, Keras lets you build and test models quickly, making it a popular choice for deep learning projects.

Comment