C.W.K.
Stream
Lesson 05 of 05 · published

Practical: Save, Load, and Verify

~12 min · verification, deployment-prep

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

The verification step that prevents production accidents

A professional save workflow doesn't end at saving — it verifies the loaded model produces identical outputs to the original. Numerical differences can creep in from BatchNormalization in inference mode, custom layer configs that didn't serialize properly, or version mismatches.

The check is simple: predict on a fixed test sample before saving, predict on the loaded model, and compare. The max absolute difference should be near zero (< 1e-5 for float32). Anything bigger is a serialization bug — find it before deployment, not after.

Code

End-to-end save + verify·python
import tensorflow as tf
import numpy as np
import os

# Build, train (snippet)
model = tf.keras.Sequential([
    tf.keras.Input(shape=(784,)),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.BatchNormalization(),
    tf.keras.layers.Dropout(0.3),
    tf.keras.layers.Dense(10),
])
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
# ... model.fit(...)

# 1. Capture original predictions
test_sample = x_test[:10]
original = model.predict(test_sample)

# 2. Save in multiple formats
os.makedirs('exports', exist_ok=True)
model.save('exports/model.keras')
model.save('exports/saved_model/')
model.save_weights('exports/weights.weights.h5')

# 3. Verify each format
def verify(loaded, name):
    preds = loaded.predict(test_sample)
    diff = np.max(np.abs(original - preds))
    print(f"{name}: max diff = {diff:.8f}")
    assert diff < 1e-5, f"{name} differs! diff={diff}"

verify(tf.keras.models.load_model('exports/model.keras'), '.keras')
verify(tf.keras.models.load_model('exports/saved_model/'), 'SavedModel')

# 4. Version-stamp for TF Serving
tf.saved_model.save(model, 'serving/my_classifier/1/')

print("All formats verified. Ready for deployment.")

Exercise

Take a model you've trained (or train MNIST briefly). Save it as .keras and SavedModel. Reload each, verify predictions match within 1e-5 of the original. If you see a larger diff, dig into which layer is the culprit.

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.