iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

compile / fit

model.compile + model.fit: optimizer, loss, metrics, callbacks, and the configuration that makes training a breeze.

TensorFlow — compile + fit

EXAMPLE
import tensorflow as tf
from tensorflow.keras import layers, Sequential

# ===== Build =====
model = Sequential([
    layers.Dense(64, activation='relu', input_shape=(4,)),
    layers.Dropout(0.2),
    layers.Dense(3, activation='softmax'),
])

# ===== Compile =====
model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
    loss='sparse_categorical_crossentropy',     # or 'categorical_crossentropy' if y is one-hot
    metrics=['accuracy', tf.keras.metrics.AUC(name='auc')],
)

# Common optimizers:
# Adam, SGD, RMSprop, AdamW, Nadam, Ftrl

# Common losses:
# Regression: 'mse', 'mae', 'huber'
# Binary classification: 'binary_crossentropy'
# Multi-class (sparse): 'sparse_categorical_crossentropy'
# Multi-class (one-hot): 'categorical_crossentropy'

# ===== Fit =====
import numpy as np
X = np.random.rand(1000, 4); y = np.random.randint(0, 3, 1000)
Xv = np.random.rand(200, 4); yv = np.random.randint(0, 3, 200)

history = model.fit(
    X, y,
    validation_data=(Xv, yv),
    batch_size=32,
    epochs=20,
    verbose=1,
)

# history.history -> dict of metrics per epoch (for plotting)

# ===== Callbacks (the production seasoning) =====
from tensorflow.keras.callbacks import (
    EarlyStopping, ModelCheckpoint, ReduceLROnPlateau,
    TensorBoard, CSVLogger, BackupAndRestore,
)

callbacks = [
    EarlyStopping(patience=4, restore_best_weights=True, monitor='val_loss'),
    ModelCheckpoint('best.keras', save_best_only=True, monitor='val_loss'),
    ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2),
    TensorBoard(log_dir='./logs'),
    CSVLogger('train.csv'),
    BackupAndRestore('./backup'),    # resume on crash
]

model.fit(X, y, validation_data=(Xv, yv), epochs=50, callbacks=callbacks)

# ===== Class weights (imbalanced data) =====
weights = {0: 1.0, 1: 5.0, 2: 2.0}
model.fit(X, y, class_weight=weights, epochs=10)

# ===== Sample weights =====
sw = np.where(y == 1, 5.0, 1.0)
model.fit(X, y, sample_weight=sw)

# ===== tf.data input pipeline (recommended) =====
ds = tf.data.Dataset.from_tensor_slices((X, y))
ds = ds.shuffle(1000).batch(32).cache().prefetch(tf.data.AUTOTUNE)
val_ds = tf.data.Dataset.from_tensor_slices((Xv, yv)).batch(64).cache().prefetch(tf.data.AUTOTUNE)

model.fit(ds, validation_data=val_ds, epochs=10)

# ===== Mixed precision =====
tf.keras.mixed_precision.set_global_policy('mixed_float16')
# Then compile + fit as usual. Big speedup on modern GPUs.

# ===== Evaluating + predicting =====
test_loss, test_acc, test_auc = model.evaluate(Xv, yv, verbose=0)
preds = model.predict(Xv)                    # probabilities
pred_class = preds.argmax(axis=1)

# ===== Save / load =====
model.save('mymodel.keras')                  # newer .keras format
loaded = tf.keras.models.load_model('mymodel.keras')

# ===== Patterns to internalise =====
# - Adam + sparse_categorical_crossentropy is a fine starting baseline
# - EarlyStopping + ModelCheckpoint + ReduceLROnPlateau every time
# - tf.data with cache + prefetch for non-trivial datasets
# - Mixed precision on modern GPUs

# ===== Pitfalls =====
# - Using categorical_crossentropy with integer labels -> wrong shape
# - validation_split + tf.data dataset -> use validation_data instead
# - No EarlyStopping -> overfitting + wasted epochs
# - Forgetting class_weight on imbalanced data

Why it matters

model.compile picks the optimizer + loss + metrics; model.fit runs the loop. Add EarlyStopping + ModelCheckpoint + ReduceLROnPlateau and you have a production-shaped training run. tf.data with cache + prefetch is the perf trick worth learning early.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)
model.fit(Xtr, ytr, validation_data=(Xva, yva), epochs=10, batch_size=128)
Try it Yourself »

Exercise

Train a Keras model.

model. (Xtr, ytr, epochs=10)

Discussion

Loading…