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

model.compile()

~9 min · training

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

compile + fit + callbacks. Three lines that hide a state machine, an optimizer step, a loss reduction, a metrics accumulator, and a callback bus. The track unpacks each piece so when fit() is doing something surprising, you know exactly which knob is loose.

What compile actually wires

model.compile() doesn't touch your weights or run a single forward pass. It's pure configuration: it binds three components onto the model so that fit() knows how to learn. The optimizer decides how a gradient becomes a weight update. The loss is the scalar the optimizer drives toward zero. The metrics are read-only observers that report progress without ever touching the gradient.

The Code section shows the two shapes you'll write constantly — a classification compile and a regression compile. Notice that the only things that change between them are loss and metrics; the call structure is identical.

Strings or instances — and why it matters later

You can pass string shortcuts ("adam", "mse") for speed, or class instances when you need real control. The string "adam" gives you Adam at its default learning rate; keras.optimizers.Adam(learning_rate=1e-3) lets you set it. Reach for instances the moment you want to tune a hyperparameter, attach a learning-rate schedule, or guarantee the exact config is the one that gets saved with the model. The one hard rule: always call compile() before fit() — an uncompiled model has no idea what "better" means.

Code

compile() for classification and regression·python
model.compile(
    optimizer="adam",                            # or keras.optimizers.Adam(1e-3)
    loss="sparse_categorical_crossentropy",      # or Loss instance
    metrics=["accuracy"],                        # list of metric names/instances
)

# For regression:
model.compile(
    optimizer=keras.optimizers.Adam(learning_rate=1e-3),
    loss="mse",
    metrics=["mae"],
)

External links

Exercise

Compile the same MNIST model two ways: once with optimizer='adam', once with keras.optimizers.Adam(learning_rate=1e-3). Train both for 5 epochs and verify identical curves.

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.