In this article, I’ll walk you through how to save machine learning models. Model progress can be recorded during and after training. This means that a model can pick up where it left off and avoid long periods of training. You should know how to save complex machine learning models so that you can share your model and your team members can recreate your work.

You should know how to save machine learning models because in the professional world you will be working with as complex machine learning models which will need you to work within teams and will require days. So to share your work within the teams and continue where you left will always require you to know how you can save machine learning models.

**Also, Read – Machine Learning Full Course for free.**

## How to Save Machine Learning Models?

Now to demonstrate to you how to save machine learning models, I will first train a TensorFlow model because these models are large and complex enough that they need to be saved and worked within teams. So I will start by importing all the necessary packages we need for this task:

```
import os
import tensorflow as tf
from tensorflow import keras
```

Now, we need a dataset to train our model. I will use the MNIST dataset:

`(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data() train_labels = train_labels[:1000] test_labels = test_labels[:1000] train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0 test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0`

## Building a Model

Now to move forward with this task I will build a simple sequential model:

```
# Define a simple sequential model
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation='relu', input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(10)
])
model.compile(optimizer='adam',
loss=tf.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.metrics.SparseCategoricalAccuracy()])
return model
# Create a basic model instance
model = create_model()
# Display the model's architecture
model.summary()
```

Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense (Dense) (None, 512) 401920 _________________________________________________________________ dropout (Dropout) (None, 512) 0 _________________________________________________________________ dense_1 (Dense) (None, 10) 5130 ================================================================= Total params: 407,050 Trainable params: 407,050 Non-trainable params: 0

## To Save Checkpoints While Training

You can use a trained model without having to retrain it or resume training where you left off, in case the training process is interrupted. The ModelCheckpoint callback method allows you to permanently save the model during and at the end of the training:

```
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
save_weights_only=True,
verbose=1)
# Train the model with the new callback
model.fit(train_images,
train_labels,
epochs=10,
validation_data=(test_images,test_labels),
callbacks=[cp_callback]) # Pass callback to training
```

Create a new untrained model. When you are restoring a model from weights only, you must have another model with the same architecture as the trained model. Because this is the same model architecture, you can share weights even if it is a different instance of the model.

Now rebuild a new untrained model and rate it on the test set. An untrained model will run at random levels (about 10% accuracy):

```
# Create a basic model instance
model = create_model()
# Evaluate the model
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))
```

32/32 - 0s - loss: 2.3319 - sparse_categorical_accuracy: 0.1410 Untrained model, accuracy: 14.10%

Now load the weights and reevaluate:

```
# Loads the weights
model.load_weights(checkpoint_path)
# Re-evaluate the model
loss,acc = model.evaluate(test_images, test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

32/32 - 0s - loss: 0.4239 - sparse_categorical_accuracy: 0.8680 Restored model, accuracy: 86.80%

## Callback Checkpoint

Train a new model with unique checkpoints:

```
# Include the epoch in the file name (uses `str.format`)
checkpoint_path = "training_2/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create a callback that saves the model's weights every 5 epochs
cp_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_path,
verbose=1,
save_weights_only=True,
period=5)
# Create a new model instance
model = create_model()
# Save the weights using the `checkpoint_path` format
model.save_weights(checkpoint_path.format(epoch=0))
# Train the model with the new callback
model.fit(train_images,
train_labels,
epochs=50,
callbacks=[cp_callback],
validation_data=(test_images,test_labels),
verbose=0)
```

Now, reset the model and load the latest checkpoint:

```
# Create a new model instance
model = create_model()
# Load the previously saved weights
model.load_weights(latest)
# Re-evaluate the model
loss, acc = model.evaluate(test_images, test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

## Manually save weights

Now, let’s see how we can manually save weights of the model:

```
# Save the weights
model.save_weights('./checkpoints/my_checkpoint')
# Create a new model instance
model = create_model()
# Restore the weights
model.load_weights('./checkpoints/my_checkpoint')
# Evaluate the model
loss,acc = model.evaluate(test_images, test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

32/32 - 0s - loss: 0.5075 - sparse_categorical_accuracy: 0.8730 Restored model, accuracy: 87.30%

## To Save Entire Machine Learning Model

Call model.save to save the architecture, weights, and training configuration of a model in a single file/folder. This will allow you to export a model so that it can be used without having the code that is used in training of the model. Since the optimizer state is recovered, you can resume training exactly where you left off.

```
# Create and train a new model instance.
model = create_model()
model.fit(train_images, train_labels, epochs=5)
# Save the entire model as a SavedModel.
!mkdir -p saved_model
model.save('saved_model/my_model')
```

This will save your model in the directory. I hope you liked this article on how to save machine learning models. Feel free to ask your valuable questions in the comments section below.

**Also, Read – Best Data Science Certifications Available Online.**