Imagine you trained a machine learning model on your computer. Why do you need to save (persist) this model before using it in a real app?
Think about what happens if you have to train the model every time you want to use it.
Model persistence means saving the trained model so you can load it later without retraining. This saves time and ensures the model behaves the same in deployment as during training.
Given the code below, what will be printed?
import tensorflow as tf import numpy as np # Create and train a simple model model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=(1,))]) model.compile(optimizer='sgd', loss='mse') model.fit(np.array([[1], [2], [3]]), np.array([[2], [4], [6]]), epochs=1, verbose=0) # Save the model model.save('my_model') # Load the model loaded_model = tf.keras.models.load_model('my_model') # Predict using the loaded model prediction = loaded_model.predict(np.array([[4]])) print(round(float(prediction[0][0]), 2))
The model learned to double the input values during training.
The model was trained to output roughly double the input. After saving and loading, it still predicts correctly, so input 4 gives output close to 8.
You want to deploy a TensorFlow model to a web service. Which saving format should you choose to ensure easy loading and compatibility?
Consider which format TensorFlow recommends for saving and loading models reliably.
SavedModel is TensorFlow's standard format that saves architecture, weights, and optimizer state, making it ideal for deployment.
After tuning hyperparameters and saving the best model, what is the main benefit of loading this saved model during deployment?
Think about what happens if you deploy without saving the tuned model.
Saving the tuned model preserves the best hyperparameters so deployment uses the optimized model directly, avoiding retraining or guesswork.
Consider this code snippet:
import tensorflow as tf
# Attempt to load a model
model = tf.keras.models.load_model('non_existent_model')
What error will this code raise?
Check what happens if you try to load a model from a path that does not exist.
TensorFlow raises an OSError when the specified model path does not exist or cannot be opened.