How to save the model for text-classification in tensorflow?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
After spending hours training a text classification model, the last thing you want is to lose it when your notebook session ends. TensorFlow provides several ways to persist a trained model so you can reload it for inference, share it with others, or deploy it to production. This article covers the main saving formats, how to handle custom objects, and how to export to TensorFlow Lite for mobile deployment.
Building a Sample Text Classification Model
Before discussing saving strategies, here is a minimal text classification model to use as a reference throughout the examples.
Saving with model.save() (SavedModel Format)
The recommended way to save a complete model is model.save(), which writes the architecture, weights, optimizer state, and training configuration to disk in TensorFlow's SavedModel format.
This creates a directory containing a saved_model.pb file, a variables/ folder with weight checkpoints, and an assets/ folder. The SavedModel format is the default in TensorFlow 2.x and is the format expected by TensorFlow Serving, TensorFlow.js, and TensorFlow Lite converters.
To reload the model later:
The loaded model is identical to the original. You can resume training, run inference, or export it further.
Saving Weights Only with save_weights()
Sometimes you only need to save the learned parameters, not the full architecture. This is useful when you want to load weights into a model with a different configuration or when the architecture is defined in code and you want to version-control it separately.
The key requirement is that the architecture you load weights into must have exactly the same layer structure and shapes. If they differ, TensorFlow raises a shape mismatch error.
Handling Custom Objects
Text classification models often include custom layers, loss functions, or preprocessing steps. When you save a model that contains custom objects, TensorFlow needs to know how to reconstruct them at load time.
The critical step is implementing get_config() in your custom layer. Without it, TensorFlow cannot serialize the layer's constructor arguments, and loading will fail. The custom_objects dictionary maps class names to their Python classes so the loader can reconstruct them.
Exporting to TensorFlow Lite
For deploying text classification models on mobile or edge devices, TensorFlow Lite provides a smaller, optimized format. You convert from a SavedModel to a .tflite file using the TFLiteConverter.
You can also apply post-training quantization to reduce the model size further:
Quantization typically reduces model size by roughly 4x with minimal accuracy loss for text classification tasks.
Loading Back for Inference
Regardless of which format you saved in, loading for inference follows a consistent pattern.
The SavedModel path is simpler and supports the full Keras API. The TFLite path requires manual tensor management but runs efficiently on resource-constrained devices.
Common Pitfalls
- Saving a model that has not been built yet (no input shape inferred), which causes
model.save()to fail; always callmodel.build(input_shape)or run at least one forward pass first. - Forgetting to implement
get_config()on custom layers, making the model impossible to load from a SavedModel without workarounds. - Using
model.save("model.h5")with the legacy HDF5 format when your model contains custom objects or TF operations that HDF5 does not support; prefer the default SavedModel directory format. - Attempting to load weights into a model with a different architecture than the one used during saving, which produces shape mismatch errors that can be confusing.
- Not testing the loaded model with sample input immediately after saving, which delays catching serialization bugs until deployment.
Summary
- Use
model.save()with the SavedModel format to persist the complete model (architecture, weights, optimizer state, and training config). - Use
save_weights()when you want to version architecture in code and only persist learned parameters. - Implement
get_config()on all custom layers and passcustom_objectswhen loading models that use them. - Convert to TensorFlow Lite with
TFLiteConverterfor mobile and edge deployment, optionally applying quantization for smaller file sizes. - Always test the loaded model with sample input immediately after saving to verify serialization correctness.

