Skip to content
Tutorials Center
My learning

Topics

Lesson 51 of 73 ยท Frameworks and Production

TensorFlow and Keras Essentials

Learn the Keras workflow (Sequential, functional API, compile, fit, callbacks, saving) by mapping it onto a mini framework you can run in the browser.

TensorFlow is Google's deep-learning framework, and Keras is its high-level API: a friendly layer on top that lets you describe a network in a few lines, train it with one call and save it with another. Keras 3 is a multi-backend library, so the same code can run on TensorFlow, JAX or PyTorch. Where PyTorch makes you write the training loop yourself, Keras hides it behind compile and fit.

Keras is not available in the in-browser runner, so every Keras snippet here is read-only. To make the workflow concrete, the runnable examples are a small Keras-shaped framework written in NumPy, with the same method names (compile, fit, predict, evaluate, callbacks, get_weights). When you later read real Keras code, each call will map to something you have watched happen.

The Keras workflow

Define Stack layers with Sequential or the functional API.
Compile Pick optimizer, loss and metrics.
Fit Train for N epochs, with validation.
Save Store the model, then load it anywhere.

Four calls cover most projects.

1 / 2

Defining a model

The simplest way to build a network is Sequential: a plain stack of layers where each layer feeds the next.

Python
import keras
from keras import layers

model = keras.Sequential([
    keras.Input(shape=(2,)),
    layers.Dense(16, activation="relu"),
    layers.Dense(16, activation="relu"),
    layers.Dense(2, activation="softmax"),
])
model.summary()

Dense(16) is a fully connected layer with 16 units, the same weight-matrix-plus-bias layer from Layers and the Forward Pass. Keras infers the input size of every layer after the first, so you only state the units. summary() prints each layer's output shape and parameter count, which is the quickest sanity check on a new architecture.

When the network is not a straight line (two inputs, a skip connection, several outputs), use the functional API: you call layers on tensors and connect them like a graph.

Python
inputs = keras.Input(shape=(64,))
h = layers.Dense(32, activation="relu")(inputs)
h2 = layers.Dense(32, activation="relu")(h)
merged = layers.Add()([h, h2])                 # a skip connection
outputs = layers.Dense(10, activation="softmax")(merged)
model = keras.Model(inputs, outputs)

A third style, subclassing keras.Model and writing call, gives full control and looks like a PyTorch module. Start with Sequential, move to the functional API when you need branches.

compile and fit

compile attaches the three things training needs and does no training itself:

Python
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss="sparse_categorical_crossentropy",   # integer labels
    metrics=["accuracy"],
)
history = model.fit(
    X_train, y_train,
    epochs=50,
    batch_size=32,
    validation_split=0.2,
    callbacks=[keras.callbacks.EarlyStopping(patience=5, restore_best_weights=True)],
)

Use sparse_categorical_crossentropy when labels are integers like 3, and categorical_crossentropy when they are one-hot vectors. If the last layer has no softmax and outputs raw logits, pass keras.losses.SparseCategoricalCrossentropy(from_logits=True) instead; the numerical-stability reasons are in Loss Functions.

fit runs the loop you know: shuffle, split into mini-batches, forward pass, loss, backward pass, optimizer update, repeated for every batch of every epoch (see Mini-Batches, Epochs and Data Loading). It returns a History object whose .history dictionary holds a list per metric, such as loss, val_loss, accuracy and val_accuracy. model.evaluate(X_test, y_test) returns the loss and metrics on new data, and model.predict(X) returns the outputs.

Here is the same workflow in the NumPy stand-in. It implements Dense layers, an Adam optimizer, compile, fit with a validation split and a summary that looks like the real thing.

Python A Keras-shaped framework: define, compile, fit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The summary shows the same bookkeeping as Keras: 48 + 272 + 34 = 354 parameters, from (2 x 16 + 16) + (16 x 16 + 16) + (16 x 2 + 2). Validation accuracy ends near 0.94, and the validation loss stops improving well before the training loss does. The history dictionary is exactly what you would plot after a real fit.

Callbacks

A callback is an object whose methods Keras calls at fixed moments: start and end of training, of each epoch, of each batch. They let you add behavior without touching the loop.

  • EarlyStopping(monitor="val_loss", patience=5, restore_best_weights=True) ends training when the monitored value has not improved for patience epochs, and rolls the weights back to the best epoch. This is the early stopping described in Overfitting, Underfitting and Regularization.
  • ModelCheckpoint("best.keras", save_best_only=True) writes the model to disk whenever it improves.
  • ReduceLROnPlateau(factor=0.5, patience=3) halves the learning rate when progress stalls, a simple alternative to the schedules.
  • TensorBoard(log_dir="logs") records metrics for the TensorBoard dashboard.
  • A custom subclass of keras.callbacks.Callback can implement on_epoch_end(self, epoch, logs) to print, log or stop.

Saving and loading

Keras offers two things to save. The full model, in the native .keras format, stores the architecture, the weights, and the compile settings in one file. The weights alone go into a file ending in .weights.h5.

Python
model.save("classifier.keras")
restored = keras.models.load_model("classifier.keras")

model.save_weights("classifier.weights.h5")
model.load_weights("classifier.weights.h5")      # needs the same architecture

For serving, model.export("serving_dir") writes a TensorFlow SavedModel, the format used by TensorFlow Serving and TFLite converters; see Deploying and Serving Models.

The stand-in does the same job with get_weights and set_weights, and an EarlyStopping callback. NumPy's savez writes the arrays to an in-memory file, and loading them into a fresh model gives identical predictions.

Python Early stopping, then save and restore weights

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The run stops at epoch 33, fifteen epochs after the best validation loss at epoch 18, and the restored weights give back that best value. Real Keras does the same in three lines, but the shape of the logic is identical: the callback watches a logged value, stores the best weights and flips a "stop" flag, and saving is nothing more than writing the weight arrays (plus a description of the architecture) to a file.

Keras and PyTorch side by side

Task Keras PyTorch
Stack layers keras.Sequential([...]) nn.Sequential(...)
Training model.fit(...) your own loop
Optimizer and loss given to compile created separately
Dropout / batch norm modes chosen automatically in fit and predict model.train() / model.eval()
Custom behavior callbacks edit the loop

Keras is quicker for standard supervised problems; PyTorch gives more control for unusual training procedures. The ideas underneath are the same, and the mini framework you can build in NumPy sits somewhere between them.

A common surprise in Keras: validation metrics reported by fit are computed with layers such as dropout in inference mode, while training metrics are averaged over the epoch while the weights are still changing. A validation loss that looks lower than the training loss is therefore often normal.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does model.compile(...) do in Keras?

    compile only configures training. The actual loop runs when you call fit.
  2. 2. When would you choose the functional API instead of Sequential?

    Sequential is a single chain of layers. Anything with branches or merges needs layers wired together explicitly.
  3. 3. What does EarlyStopping(patience=5, restore_best_weights=True) do?

    It monitors a value such as val_loss, counts epochs with no improvement, then halts and rolls back to the best epoch.
  4. 4. Your labels are integers 0 to 9 and the last layer is Dense(10, activation='softmax'). Which loss string fits?

    Integer class labels use the sparse variant. The non-sparse one expects one-hot vectors.
  5. 5. What is the difference between model.save('m.keras') and model.save_weights('m.weights.h5')?

    The .keras file can be loaded on its own. Weight files need a model with the same architecture built first.

Summary

  • Keras is a high-level API: define a model, compile it, fit it, then save it.
  • Sequential is a straight stack of layers; the functional API handles branches, merges and several inputs or outputs.
  • compile chooses the optimizer, loss and metrics; fit runs mini-batch training and returns a history of per-epoch metrics.
  • Callbacks such as EarlyStopping, ModelCheckpoint and ReduceLROnPlateau add behavior without changing the loop.
  • .keras files hold the whole model; .weights.h5 files hold only the weights; export produces a serving format.
  • Keras trades control for convenience; the training step underneath is the one you built by hand.

Next: Hugging Face Transformers Pipelines.