C.W.K.
Stream
Lesson 02 of 07 · published

Mixed Precision Training

~9 min · advanced

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

Two number formats, one training loop

Mixed precision does exactly what the name says: it mixes float16 (or bfloat16) for the heavy compute with float32 for the parts that need the range. The matmuls and convolutions — where almost all the FLOPs live — run in 16-bit, which is where the speedup comes from. But the master copy of the weights, and the gradient accumulation, stay in 32-bit so small updates don't vanish into rounding error.

One line to turn it on

You set a global policy with set_global_policy("mixed_float16") before you build the model, then build and train exactly as you normally would — Keras inserts the casts at the right boundaries for you. One detail earns its keep: keep your final Dense layer producing raw logits (no softmax) and pair it with from_logits=True, so the loss is computed in a numerically stable way instead of through a float16 softmax that can overflow.

The free-speed catch

The 2-3× is only free if the hardware has the matching tensor units. On a GPU without Tensor Cores (or on CPU) you get the memory savings but little or no speedup, and you've added casting overhead for nothing — so this is a measure-it, not assume-it, win.

Code

Enable mixed_float16 and keep logits in float32·python
# Enable mixed precision globally
keras.mixed_precision.set_global_policy("mixed_float16")

# Build and train normally — Keras handles the casting
model = keras.Sequential([
    keras.Input(shape=(784,)),
    layers.Dense(256, activation="relu"),  # Computes in float16
    layers.Dense(10),                        # Raw logits (no activation)
])

# Use from_logits for numerical stability with mixed precision
model.compile(
    optimizer="adam",
    loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
)

External links

Exercise

Train CIFAR-10 with mixed_float16 and without. Compare epoch time (should drop ~30-50%) and final accuracy (should be within 0.5%).

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.