100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Keras Cheat Sheet

Keras Cheat Sheet

High-level Keras API reference covering Sequential and Functional model building, compiling, training with callbacks, and common layer types.

2 PagesBeginnerApr 8, 2026

Sequential API

Stack layers linearly and compile.

python
from tensorflow import kerasfrom tensorflow.keras import layersmodel = keras.Sequential([    layers.Dense(128, activation="relu", input_shape=(784,)),    layers.Dropout(0.3),    layers.Dense(10, activation="softmax"),])model.compile(    optimizer="adam",    loss="sparse_categorical_crossentropy",    metrics=["accuracy"],)

Functional API

Build models with non-linear topology.

python
inputs = keras.Input(shape=(784,))x = layers.Dense(128, activation="relu")(inputs)x = layers.Dense(64, activation="relu")(x)outputs = layers.Dense(10, activation="softmax")(x)model = keras.Model(inputs=inputs, outputs=outputs)

Train, Evaluate & Save

Fit the model and persist it to disk.

python
history = model.fit(    X_train, y_train,    validation_split=0.2,    epochs=20,    batch_size=32,    callbacks=[keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)],)loss, acc = model.evaluate(X_test, y_test)model.save("my_model.keras")model2 = keras.models.load_model("my_model.keras")

Common Layers

Building blocks used across most models.

  • Dense- fully connected layer
  • Conv2D- 2D convolution for image inputs
  • MaxPooling2D- downsamples feature maps
  • LSTM / GRU- recurrent layers for sequence data
  • Dropout- randomly zeroes inputs for regularization
  • BatchNormalization- normalizes layer inputs for stable training
  • Embedding- maps integer tokens to dense vectors
  • Flatten- reshapes multi-dimensional input to 1D
Pro Tip

Combine EarlyStopping with ModelCheckpoint so you persist the best-performing weights seen during training, not just whatever the final epoch happens to produce.

Was this cheat sheet helpful?

Explore Topics

#Keras#KerasCheatSheet#DataScience#Beginner#SequentialAPI#FunctionalAPI#TrainEvaluateSave#CommonLayers#MachineLearning#APIs#CheatSheet#SkillVeris