Tensorflow Save Load
In machine learning and deep learning projects, model saving and loading are crucial steps.
TensorFlow provides multiple ways to save and restore models, enabling developers to:
* Save trained models for later use
* Share models with other developers
* Resume training from checkpoints
* Deploy models to production environments
TensorFlow 2.x primarily supports three model saving formats:
1. SavedModel format (recommended)
2. HDF5 format (.h5)
3. Legacy Keras format
* * *
## Saving the Entire Model
### SavedModel Format
SavedModel is the recommended model-saving format in TensorFlow, containing complete model information:
## Example
import tensorflow as tf
# Create and train a simple model
model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
# Save as SavedModel format
model.save('my_model')# Note: No file extension
The saved directory structure is:
my_model/βββ assets/βββ variables/β βββ variables.data-00000-of-00001β βββ variables.index βββ saved_model.pb
### HDF5 Format
HDF5 is another commonly used model-saving format:
## Example
# Save as HDF5 format
model.save('my_model.h5')# Note: .h5 extension
### Differences Between the Two Formats
| Feature | SavedModel | HDF5 |
| --- | --- | --- |
| Includes custom objects | Yes | Requires additional configuration |
| Includes optimizer states | Yes | Optional |
| TensorFlow Serving | Native support | Not supported |
| File size | Larger | Smaller |
* * *
## Loading the Entire Model
### Loading from SavedModel
## Example
# Load from SavedModel
loaded_model = tf.keras.models.load_model('my_model')
# Verify the model
loss, acc = loaded_model.evaluate(x_test, y_test, verbose=2)
print(f"Restored model, accuracy: {100*acc:.1f}%")
### Loading from an HDF5 File
## Example
# Load from an HDF5 file
loaded_model = tf.keras.models.load_model('my_model.h5')
# Verify the model
loss, acc = loaded_model.evaluate(x_test, y_test, verbose=2)
print(f"Restored model, accuracy: {100*acc:.1f}%")
* * *
## Selective Saving and Loading
### Saving Only Weights
## Example
# Save weights
model.save_weights('my_model_weights')
# Save weights as HDF5 file
model.save_weights('my_model_weights.h5')
### Loading Weights
## Example
# Create a model with the same architecture
new_model = tf.keras.Sequential([
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
new_model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Load weights
new_model.load_weights('my_model_weights')
# Or for .h5 files
new_model.load_weights('my_model_weights.h5')
### Saving Checkpoints for Custom Training Loops
## Example
# Create a checkpoint callback
checkpoint_path ="training_1/cp.ckpt"
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
# Train the model using the callback
model.fit(x_train, y_train,
epochs=10,
callbacks=)
* * *
## Best Practices for Model Saving and Loading
1. **Production Deployment**: Prefer the SavedModel format
2. **Cross-Platform Sharing**: HDF5 format is more universal
3. **Resume Training After Interruption**: Use checkpoint callbacks to save periodically
4. **Handling Custom Objects**: model.save('custom_model', save_format='tf')
5. **Model Version Control**: Create separate directories for different model versions
* * *
## Common Issues and Solutions
### Custom Layers/Models Saving Problems
## Example
# Custom layer example
class CustomLayer(tf.keras.layers.Layer):
def __init__ (self, units=32, **kwargs):
super(). __init__ (**kwargs)
self.units= units
def build(self, input_shape):
self.w=self.add_weight(
shape=(input_shape,self.units),
initializer="random_normal",
trainable=True)
def call(self, inputs):
return tf.matmul(inputs,self.w)
def get_config(self):
config =super().get_config()
config.update({"units": self.units})
return config
# Use the custom layer and save
model = tf.keras.Sequential([CustomLayer(10)])
model.compile(optimizer='adam', loss='mse')
model.save('custom_model')# Automatically saves custom layers
### Cross-Version Compatibility Issues
* Try to save and load models using the same TensorFlow version
* For production environments, consider using TensorFlow Serving to avoid version issues
### Optimizing Large Model Saving
## Example
# Use save_weights instead of save to reduce saving time
model.save_weights('large_model_weights.h5')
YouTip