C.W.K.
Stream
Lesson 01 of 08 · published

How Keras 3 Decouples API from Engine

~10 min · backend

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Keras 3's headline trick: pick your backend at runtime via one env var. TensorFlow, PyTorch, or JAX — same model = keras.Sequential(...) line, same .fit(). This track teaches the mental model behind that, the gotchas (mixing tensors across backends), and the keras.ops namespace that lets you write backend-agnostic custom math.

Description vs. execution

The key insight behind Keras 3 is separating model description from execution. When you define layers, connect them, and specify training — you're describing what to compute. The backend decides how to compute it. That line — between intent and implementation — is the whole reason one model file can run on three engines.

This isn't a new idea in software, but it's unusually hard to pull off for deep learning, where the engine owns autodiff, device placement, kernel selection, and graph compilation. Most "portable" frameworks fake it by reimplementing the engine; Keras 3 instead routes every primitive op down to whichever engine is loaded.

The dispatch layer

That routing happens through keras.ops, which dispatches each operation to the active backend. Under the hood:

  • keras.ops.matmul(a, b)jax.numpy.matmul / tf.matmul / torch.matmul
  • keras.ops.nn.softmax(x)jax.nn.softmax / tf.nn.softmax / torch.nn.functional.softmax

Because every built-in layer, loss, metric, and optimizer is written against keras.ops, they inherit portability for free — you never opt into it.

Why the saved file is engine-free

KerasTensors are backend-agnostic symbolic tensors used during model construction. They describe the computation graph without committing to any framework — which is exactly why a saved .keras file contains no backend-specific operations. Train under JAX today, hand the file to a teammate running TensorFlow tomorrow, and it just loads. That's the payoff the next lessons build on.

External links

Exercise

Read keras.io's 'Keras 3 announcement' page. Identify the three things Chollet says are decoupled. Write them in your own words.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.