compile() must be called before fit(), since it's where the model is told how to learn — which optimizer will update its weights, which loss function measures how wrong its predictions are, and which additional metrics, like accuracy, should be tracked and reported during training without directly influencing the weight updates. Optimizer and loss can be passed either as string shortcuts, like 'adam' and 'sparse_categorical_crossentropy', or as actual configured objects, like tf.keras.optimizers.Adam(learning_rate=0.001), when you need to customize their settings.
1Understanding model.compile()
compile() must be called before fit(), since it's where the model is told how to learn — which optimizer will update its weights, which loss function measures how wrong its predictions are, and which additional metrics, like accuracy, should be tracked and reported during training without directly influencing the weight updates. Optimizer and loss can be passed either as string shortcuts, like 'adam' and 'sparse_categorical_crossentropy', or as actual configured objects, like tf.keras.optimizers.Adam(learning_rate=0.001), when you need to customize their settings.
Pass an actual optimizer object like tf.keras.optimizers.Adam(learning_rate=0.001) instead of the string shortcut 'adam' whenever you need to customize settings like the learning rate — the string shortcut always uses that optimizer's default configuration.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(10, activation='softmax', input_shape=(32,))])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
print(model.optimizer.__class__.__name__)2Practical Example
Here is a real-world application of model.compile() showing how it is used in production TensorFlow code.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(10, activation='softmax', input_shape=(32,))])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=0.0005),
loss='sparse_categorical_crossentropy'
)
print(model.optimizer.learning_rate.numpy())3Best Practices
Follow these guidelines when working with model.compile():
1. Match the loss function to the label format: sparse_categorical_crossentropy for integer class labels, categorical_crossentropy for one-hot encoded labels
2. Pass a configured optimizer object instead of a string shortcut whenever you need a non-default learning rate or other optimizer setting
3. Include metrics like 'accuracy' in compile() to get them tracked and reported automatically during fit(), instead of computing them manually after training
Tip: Pass an actual optimizer object like tf.keras.optimizers.Adam(learning_rate=0.001) instead of the string shortcut 'adam' whenever you need to customize settings like the learning rate — the string shortcut always uses that optimizer's default configuration.
import tensorflow as tf
from tensorflow.keras import layers, Sequential
model = Sequential([layers.Dense(10, activation='softmax', input_shape=(32,))])
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
print(model.optimizer.__class__.__name__)