Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 40 ยท Getting Started

The Keras Workflow and Backends

Learn the end-to-end Keras workflow, how to pick the TensorFlow, JAX or PyTorch backend, what keras.ops is, and which model-building style to use.

Keras is deliberately small. You build almost everything from four kinds of objects (layers, models, losses and optimizers) and drive them with three verbs (compile, fit, evaluate). This lesson gives you the whole workflow in one place, explains how Keras 3 chooses a backend, introduces keras.ops (backend-independent math) and compares the three ways to build a model. The NumPy stand-ins show how thin the "framework" layer really is.

The workflow at a glance

flowchart TB
    A["1. Prepare data<br/>arrays, tf.data, images, text"] --> B["2. Define model<br/>Sequential, Functional, subclass"]
    B --> C["3. Compile<br/>optimizer, loss, metrics"]
    C --> D["4. Fit<br/>train with validation"]
    D --> E["5. Evaluate on test data"]
    E --> F["6. Predict, save, deploy"]
    D -.->|"tune and repeat"| B

Real projects loop between steps 2 and 4 many times.

Each step maps to a chapter of this course: data and tensors (Chapter 2 and 7), model building (Chapter 3), compile/fit/evaluate (Chapter 4) and saving (Chapter 7).

Choosing a backend

Keras 3 can execute your model on TensorFlow, JAX or PyTorch. The backend is chosen when Keras is first imported, from the KERAS_BACKEND environment variable or a config file. This course uses TensorFlow.

Python
import os
os.environ["KERAS_BACKEND"] = "tensorflow"   # or "jax", "torch"

import keras
print(keras.config.backend())   # tensorflow

Set the variable before import keras; changing it afterwards has no effect in that process. The default config file is ~/.keras/keras.json, which holds the default backend, float type ("float32") and image data format ("channels_last").

Why care about backends? A model written with built-in layers runs unchanged on any of them. TensorFlow offers the richest deployment tools (SavedModel, TFLite, TF.js, TF Serving). JAX often wins on TPUs and large training jobs. PyTorch integrates with the PyTorch ecosystem. If you write a custom layer, you can stay portable by using keras.ops inside it.

Some features are TensorFlow-specific by design: tf.data pipelines work with every backend as an input source, but exporting a TF SavedModel or TFLite file requires the TensorFlow backend.

keras.ops: backend-independent math

keras.ops mirrors NumPy and the core of tf.math. Code written with it runs on every backend. Here is a sketch:

Python
from keras import ops

x = ops.convert_to_tensor([[1.0, 2.0], [3.0, 4.0]])
w = ops.ones((2, 3))

y = ops.matmul(x, w)          # (2, 3)
y = ops.relu(y)               # activation
print(ops.shape(y))           # (2, 3)
print(ops.mean(y, axis=0))    # one mean per column

The names are NumPy's (ops.sum, ops.mean, ops.reshape, ops.transpose, ops.concatenate) plus neural-network helpers such as ops.relu, ops.softmax and ops.conv. You can also call tf.matmul directly when you commit to TensorFlow, as we do in Chapter 2.

NumPy is a faithful stand-in for what these operations do, so here is the same computation you can run:

Python Stand-in: the keras.ops example in NumPy

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.

Three ways to build a model

Keras gives you three styles, from simplest to most flexible. Each has its own lesson later in this chapter.

Style Use when Lesson
Sequential A plain stack: one input, one output, layers in a line Sequential API
Functional Branches, merges, multiple inputs or outputs, shared layers Functional API
Subclassing Fully custom forward logic written as Python code Chapter 8

Rule of thumb: start with Sequential; move to Functional when the architecture is not a straight line; subclass only when you truly need control that the first two cannot give.

What "framework" means: a tiny Sequential

To appreciate what Sequential does, here is a miniature version. It only stores layers and runs them in order. The real class adds shape inference, weight management, saving and the training API, but the core idea is this small.

Python Stand-in: a 15-line Sequential (NumPy)

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.

Calling model(x) pushes the batch through each layer in order, which is exactly what model(x) and model.predict(x) do in Keras (predict adds batching and a progress bar).

Layers, models and the Layer base class

In Keras, a layer is an object that holds some state (its weights) and a computation (its call method). A model is a layer that adds training and evaluation methods. This means a model can be used as a layer inside another model, which makes nesting and reuse natural. We build our own layers in Chapter 8.

Weights, trainable and non-trainable

Every layer lists its weights. Weights are trainable when the optimizer updates them (Dense kernels and biases) and non-trainable when something else maintains them (for example the running averages in batch normalization). Setting layer.trainable = False freezes a layer, which is the key to transfer learning in Chapter 5.

Python
layer = keras.layers.Dense(3)
layer.build((None, 4))                  # create weights for 4 inputs
print([w.shape for w in layer.weights]) # [(4, 3), (3,)]
print(layer.count_params())             # 15

Data types

Keras uses float32 by default: it halves memory against float64 and is what GPUs are fastest at. Change it globally with keras.config.set_floatx("float16") if you know you need to. Mixed precision (compute in float16, keep weights in float32) is covered in Chapter 8.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When must the KERAS_BACKEND environment variable be set?

    Keras reads the backend setting when it is first imported.
  2. 2. What is keras.ops for?

    keras.ops provides NumPy-like and neural-network operations that run on every backend.
  3. 3. Which model-building style should you try first?

    Sequential is the simplest. Move to Functional only when the architecture is not a straight stack.
  4. 4. What does layer.trainable = False do?

    Frozen layers keep their weights during training, which is how transfer learning keeps pretrained features.

Summary

  • The workflow: prepare data, define model, compile, fit, evaluate, predict/save.
  • Keras 3 runs on TensorFlow, JAX or PyTorch; set KERAS_BACKEND before importing.
  • keras.ops gives portable NumPy-like math.
  • Sequential, Functional and subclassing trade simplicity for flexibility.
  • A framework "Sequential" is just layers called in order, which the stand-in demonstrates.

Next: Data Basics for Keras