Model Persistence

Back

Loading concept...

🧳 Model Persistence: Your AI’s Suitcase

Imagine you spent weeks teaching your pet robot to recognize your family. What if every time you turned it off, it forgot everything? That would be terrible! Model Persistence is like packing your robot’s memories into a suitcase, so it never forgets.


🎯 The Big Picture

Your TensorFlow model learns amazing things. But without saving it, all that learning vanishes when your program ends. Model Persistence is how we keep AI memories safe forever.

Think of it like this:

📖 The Analogy: Your model is a talented chef who has learned 1000 recipes. Model persistence is writing down those recipes in a cookbook. Now, even if the chef takes a vacation, anyone can read the cookbook and make the same dishes!


📦 Saving Models

What is Saving?

Saving a model means storing everything it learned onto your computer’s disk. Just like saving a game so you can continue later!

Two Ways to Save

1. The Complete Package (SavedModel format)

# Save EVERYTHING - like packing
# your whole room into boxes
model.save('my_model')

This creates a folder with:

  • The architecture (the model’s brain design)
  • The weights (what it learned)
  • The optimizer state (its learning habits)

2. Just the Weights (H5 format)

# Save just the memories,
# not the brain design
model.save('my_model.h5')

When to Use Which?

graph TD A["Need to Save?"] --> B{What do you need?} B --> C["Everything"] B --> D["Just Weights"] C --> E["Use .save folder"] D --> F["Use .h5 file"] E --> G["✅ Deployment Ready"] F --> H["✅ Smaller Files"]

📂 Loading Models

Bringing Your Model Back to Life!

Loading is like unpacking that suitcase. Your model wakes up exactly where it left off.

# Load the complete model
from tensorflow import keras

loaded_model = keras.models.load_model(
    'my_model'
)

# It remembers everything!
predictions = loaded_model.predict(new_data)

Important Tips

What You Saved How to Load
Folder (my_model/) load_model('my_model')
H5 file (.h5) load_model('my_model.h5')

💡 Pro Tip: The loaded model is ready to use immediately. No retraining needed!


⚖️ Weight Management

What Are Weights?

Weights are the numbers your model learned during training. They’re like the answers your model figured out after studying millions of examples.

Saving Just Weights

Sometimes you only want to save what the model learned, not its whole design:

# Save weights to a file
model.save_weights('my_weights.weights.h5')

Loading Weights into a New Model

# First, create a model with
# the same design
new_model = create_same_model()

# Then load the learned knowledge
new_model.load_weights('my_weights.weights.h5')

Why Save Weights Separately?

graph TD A["Why Separate Weights?"] --> B["Smaller Files"] A --> C["Share Learning"] A --> D["Transfer Learning"] B --> E["Faster to download"] C --> F["Give knowledge to friends"] D --> G["Train new models faster"]

🎯 Real Example: A model trained on cats can give its “eye for patterns” to a model learning about dogs!


🏗️ Architecture Serialization

What’s Architecture?

Architecture is your model’s blueprint. It’s the design of how layers connect, like a recipe without the actual cooking.

Saving Architecture Only

# Get the blueprint as JSON
json_config = model.to_json()

# Save it to a file
with open('model_design.json', 'w') as f:
    f.write(json_config)

Rebuilding from Architecture

from tensorflow.keras.models import model_from_json

# Read the blueprint
with open('model_design.json', 'r') as f:
    json_config = f.read()

# Build a fresh model from it
new_model = model_from_json(json_config)
# Note: This model has no training!

Architecture vs Weights

Architecture Weights
The recipe The secret sauce
How layers connect What layers learned
Small file Large file
No training needed Training required

🔧 Custom Objects in Saving

The Problem

Sometimes your model uses custom pieces you created yourself. TensorFlow doesn’t automatically know about them!

# Your custom layer
class MySpecialLayer(keras.layers.Layer):
    def call(self, inputs):
        return inputs * 2

# Your custom loss function
def my_loss(y_true, y_pred):
    return keras.backend.mean(
        keras.backend.abs(y_true - y_pred)
    )

The Solution: Tell TensorFlow!

When loading, you must remind TensorFlow about your custom pieces:

# Method 1: Use custom_objects
loaded = keras.models.load_model(
    'my_model',
    custom_objects={
        'MySpecialLayer': MySpecialLayer,
        'my_loss': my_loss
    }
)

Alternative: Register Globally

# Register once, use everywhere!
@keras.utils.register_keras_serializable()
class MySpecialLayer(keras.layers.Layer):
    def call(self, inputs):
        return inputs * 2
graph TD A["Custom Objects"] --> B{Loading Model?} B --> C["Pass custom_objects"] B --> D["Use @register decorator"] C --> E["Works for one load"] D --> F["Works everywhere"]

💾 Checkpointing

What’s a Checkpoint?

Imagine training your model for 10 hours, and your computer crashes at hour 9. All that work—gone! 😱

Checkpoints are automatic saves during training. Like auto-save in a video game!

Setting Up Checkpoints

from tensorflow.keras.callbacks import (
    ModelCheckpoint
)

# Save after every epoch
checkpoint = ModelCheckpoint(
    'checkpoint_{epoch:02d}.weights.h5',
    save_weights_only=True,
    save_freq='epoch'
)

# Train with auto-save enabled
model.fit(
    x_train, y_train,
    epochs=100,
    callbacks=[checkpoint]
)

Smart Checkpointing

Save only when the model improves:

# Only save the best version
best_checkpoint = ModelCheckpoint(
    'best_model.weights.h5',
    save_best_only=True,
    monitor='val_loss'
)

Checkpoint Strategies

graph TD A["Checkpointing"] --> B["Every Epoch"] A --> C["Best Only"] A --> D["Every N Steps"] B --> E["Many files, safe"] C --> F["One file, efficient"] D --> G["Balance both"]

Recovering from Crashes

# After restart, load last checkpoint
model.load_weights('checkpoint_42.weights.h5')

# Continue training from epoch 43!
model.fit(
    x_train, y_train,
    initial_epoch=42,
    epochs=100
)

🎁 Putting It All Together

Here’s the complete picture:

graph TD A["Your Trained Model"] --> B{What to Save?} B --> C["Everything"] B --> D["Weights Only"] B --> E["Architecture Only"] C --> F["model.save"] D --> G["save_weights"] E --> H["to_json"] F --> I["Ready for Production"] G --> J["Transfer Learning"] H --> K["Share Design"]

Quick Reference

Goal Method
Save everything model.save('path')
Save weights model.save_weights('file.h5')
Save design model.to_json()
Auto-save training ModelCheckpoint
Custom objects custom_objects={}

🚀 You Did It!

Now you know how to:

  • Save your models completely or partially
  • Load them back anytime
  • Manage weights separately from architecture
  • Handle custom objects when saving
  • Checkpoint during training to never lose progress

Your AI will never forget its training again. You’ve given it a perfect memory!

🎯 Remember: A saved model is like a trained employee you can hire anytime. Train once, use forever!

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.