Set the training objective
Supervised classification or regression
TensorFlow · Module
Configures a Keras model for training by specifying its optimizer, loss, and metrics. Optional settings control weighting, execution mode, batches per function call, XLA compilation, and mixed-precision loss scaling.
Calling compile configures a Keras model for training by selecting an optimizer, loss calculation, and optional metrics. It can also control weighted metrics, eager execution, batches processed per traced call, XLA compilation, and automatic loss scaling before fit is used.
compile(optimizer='rmsprop', loss=None, loss_weights=None, metrics=None, weighted_metrics=None, run_eagerly=False, steps_per_execution=1, jit_compile='auto', auto_scale_loss=True)| Name | Type | Description |
|---|---|---|
optimizer | str or keras.optimizers.Optimizer | Optimizer name or optimizer instance used for training. |
loss | str, keras.losses.Loss, or callable | Loss name, Loss instance, or callable taking y_true and y_pred. |
loss_weights | list, dict, or None | Optional scalar or per-output loss weighting. |
metrics | list, dict, or None | Metrics reported during training and evaluation. |
weighted_metrics | list, dict, or None | Metrics calculated with sample weighting. |
run_eagerly | bool | Whether to execute the training step eagerly. |
steps_per_execution | int | Number of batches processed per tf.function call. |
jit_compile | bool or str | Whether to use XLA compilation; auto chooses based on support. |
auto_scale_loss | bool | Whether to wrap the optimizer with loss scaling for mixed precision. |
None; configures the model in place.
import tensorflow as tf
model = tf.keras.Sequential([
tf.keras.layers.Dense(8, activation="relu"),
tf.keras.layers.Dense(1, activation="sigmoid"),
])
model.compile(
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
loss=tf.keras.losses.BinaryCrossentropy(),
metrics=[tf.keras.metrics.BinaryAccuracy()],
)Creates a binary-classification model and configures its optimizer, loss, and accuracy metric.
Supervised classification or regression
Multi-output networks
Mixed-precision training
Performance experimentation
compile configures a model for training. Its main choices are the optimizer, loss, and metrics; after configuration, training is typically performed with fit.
Follow-up: How would you configure different losses for multiple outputs? Pass a loss structure matching the model outputs and use loss_weights to control each output's contribution.
optimizer may be an optimizer name or instance, while loss may be a loss name, Loss instance, or callable receiving y_true and y_pred. Metrics can be supplied to report evaluation measures during training and evaluation.
Follow-up: What is the distinction between metrics and weighted_metrics? weighted_metrics applies sample weights to metric calculations, whereas ordinary metrics do not use those weights in the same way.
jit_compile controls XLA compilation of the model's training step. With the TensorFlow backend, auto enables XLA when the model supports it and otherwise disables it.
Follow-up: When would run_eagerly help? It can make debugging easier by executing the training step eagerly, at the cost of graph execution optimizations.
tf.keras.Model.fittf.keras.Model.evaluatetf.keras.Model.predicttf.keras.lossestf.keras.metricstf.keras.optimizers