View on TensorFlow.org | Run in Google Colab | View source on GitHub |

Model progress can be saved during—and after—training. This means a model can resume where it left off and avoid long training times. Saving also means you can share your model and others can recreate your work. When publishing research models and techniques, most machine learning practitioners share:

- code to create the model, and
- the trained weights, or parameters, for the model

Sharing this data helps others understand how the model works and try it themselves with new data.

### Options

There are different ways to save TensorFlow models—depending on the API you're using. This guide uses tf.keras, a high-level API to build and train models in TensorFlow. For other approaches, see the TensorFlow Save and Restore guide or Saving in eager.

## Setup

### Installs and imports

Install and import TensorFlow and dependencies:

```
!pip install -q h5py pyyaml
```

You are using pip version 18.1, however version 19.0.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command.

### Get an example dataset

We'll use the MNIST dataset to train our model to demonstrate saving weights. To speed up these demonstration runs, only use the first 1000 examples:

```
from __future__ import absolute_import, division, print_function
import os
import tensorflow as tf
from tensorflow import keras
tf.__version__
```

'1.12.0'

```
(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
```

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step

### Define a model

Let's build a simple model we'll use to demonstrate saving and loading weights.

```
# Returns a short sequential model
def create_model():
model = tf.keras.models.Sequential([
keras.layers.Dense(512, activation=tf.keras.activations.relu, input_shape=(784,)),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation=tf.keras.activations.softmax)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
return model
# Create a basic model instance
model = create_model()
model.summary()
```

_________________________________________________________________ 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 _________________________________________________________________

## Save checkpoints during training

The primary use case is to automatically save checkpoints *during* and at *the end* of training. This way you can use a trained model without having to retrain it, or pick-up training where you left of—in case the training process was interrupted.

`tf.keras.callbacks.ModelCheckpoint`

is a callback that performs this task. The callback takes a couple of arguments to configure checkpointing.

### Checkpoint callback usage

Train the model and pass it the `ModelCheckpoint`

callback:

```
checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create checkpoint callback
cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path,
save_weights_only=True,
verbose=1)
model = create_model()
model.fit(train_images, train_labels, epochs = 10,
validation_data = (test_images,test_labels),
callbacks = [cp_callback]) # pass callback to training
```

Train on 1000 samples, validate on 1000 samples Epoch 1/10 960/1000 [===========================>..] - ETA: 0s - loss: 1.2423 - acc: 0.6427 Epoch 00001: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 458us/step - loss: 1.2087 - acc: 0.6520 - val_loss: 0.7103 - val_acc: 0.7980 Epoch 2/10 928/1000 [==========================>...] - ETA: 0s - loss: 0.4150 - acc: 0.8955 Epoch 00002: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 154us/step - loss: 0.4119 - acc: 0.8940 - val_loss: 0.5376 - val_acc: 0.8360 Epoch 3/10 992/1000 [============================>.] - ETA: 0s - loss: 0.2879 - acc: 0.9254 Epoch 00003: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 168us/step - loss: 0.2874 - acc: 0.9260 - val_loss: 0.5016 - val_acc: 0.8420 Epoch 4/10 992/1000 [============================>.] - ETA: 0s - loss: 0.1957 - acc: 0.9597 Epoch 00004: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 149us/step - loss: 0.1980 - acc: 0.9590 - val_loss: 0.4515 - val_acc: 0.8570 Epoch 5/10 928/1000 [==========================>...] - ETA: 0s - loss: 0.1589 - acc: 0.9612 Epoch 00005: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 162us/step - loss: 0.1560 - acc: 0.9620 - val_loss: 0.4369 - val_acc: 0.8570 Epoch 6/10 896/1000 [=========================>....] - ETA: 0s - loss: 0.1174 - acc: 0.9754 Epoch 00006: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 174us/step - loss: 0.1154 - acc: 0.9770 - val_loss: 0.4205 - val_acc: 0.8600 Epoch 7/10 832/1000 [=======================>......] - ETA: 0s - loss: 0.0913 - acc: 0.9856 Epoch 00007: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 180us/step - loss: 0.0917 - acc: 0.9850 - val_loss: 0.4153 - val_acc: 0.8710 Epoch 8/10 896/1000 [=========================>....] - ETA: 0s - loss: 0.0657 - acc: 0.9888 Epoch 00008: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 170us/step - loss: 0.0652 - acc: 0.9900 - val_loss: 0.4121 - val_acc: 0.8690 Epoch 9/10 800/1000 [=======================>......] - ETA: 0s - loss: 0.0519 - acc: 0.9962 Epoch 00009: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 181us/step - loss: 0.0506 - acc: 0.9960 - val_loss: 0.4122 - val_acc: 0.8660 Epoch 10/10 832/1000 [=======================>......] - ETA: 0s - loss: 0.0379 - acc: 0.9988 Epoch 00010: saving model to training_1/cp.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07fb8728d0>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 178us/step - loss: 0.0409 - acc: 0.9970 - val_loss: 0.4151 - val_acc: 0.8600 <tensorflow.python.keras.callbacks.History at 0x7f086c253b70>

This creates a single collection of TensorFlow checkpoint files that are updated at the end of each epoch:

```
!ls {checkpoint_dir}
```

checkpoint cp.ckpt.data-00000-of-00001 cp.ckpt.index

Create a new, untrained model. When restoring a model from only weights, you must have a model with the same architecture as the original model. Since it's the same model architecture, we can share weights despite that it's a different *instance* of the model.

Now rebuild a fresh, untrained model, and evaluate it on the test set. An untrained model will perform at chance levels (~10% accuracy):

```
model = create_model()
loss, acc = model.evaluate(test_images, test_labels)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))
```

1000/1000 [==============================] - 0s 117us/step Untrained model, accuracy: 11.70%

Then load the weights from the checkpoint, and re-evaluate:

```
model.load_weights(checkpoint_path)
loss,acc = model.evaluate(test_images, test_labels)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

1000/1000 [==============================] - 0s 37us/step Restored model, accuracy: 86.00%

### Checkpoint callback options

The callback provides several options to give the resulting checkpoints unique names, and adjust the checkpointing frequency.

Train a new model, and save uniquely named checkpoints once every 5-epochs:

```
# 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)
cp_callback = tf.keras.callbacks.ModelCheckpoint(
checkpoint_path, verbose=1, save_weights_only=True,
# Save weights, every 5-epochs.
period=5)
model = create_model()
model.save_weights(checkpoint_path.format(epoch=0))
model.fit(train_images, train_labels,
epochs = 50, callbacks = [cp_callback],
validation_data = (test_images,test_labels),
verbose=0)
```

WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00005: saving model to training_2/cp-0005.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00010: saving model to training_2/cp-0010.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00015: saving model to training_2/cp-0015.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00020: saving model to training_2/cp-0020.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00025: saving model to training_2/cp-0025.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00030: saving model to training_2/cp-0030.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00035: saving model to training_2/cp-0035.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00040: saving model to training_2/cp-0040.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00045: saving model to training_2/cp-0045.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. Epoch 00050: saving model to training_2/cp-0050.ckpt WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f07f93a93c8>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. <tensorflow.python.keras.callbacks.History at 0x7f07fb7f81d0>

Now, look at the resulting checkpoints and choose the latest one:

```
! ls {checkpoint_dir}
```

checkpoint cp-0025.ckpt.index cp-0000.ckpt.data-00000-of-00001 cp-0030.ckpt.data-00000-of-00001 cp-0000.ckpt.index cp-0030.ckpt.index cp-0005.ckpt.data-00000-of-00001 cp-0035.ckpt.data-00000-of-00001 cp-0005.ckpt.index cp-0035.ckpt.index cp-0010.ckpt.data-00000-of-00001 cp-0040.ckpt.data-00000-of-00001 cp-0010.ckpt.index cp-0040.ckpt.index cp-0015.ckpt.data-00000-of-00001 cp-0045.ckpt.data-00000-of-00001 cp-0015.ckpt.index cp-0045.ckpt.index cp-0020.ckpt.data-00000-of-00001 cp-0050.ckpt.data-00000-of-00001 cp-0020.ckpt.index cp-0050.ckpt.index cp-0025.ckpt.data-00000-of-00001

```
latest = tf.train.latest_checkpoint(checkpoint_dir)
latest
```

'training_2/cp-0050.ckpt'

To test, reset the model and load the latest checkpoint:

```
model = create_model()
model.load_weights(latest)
loss, acc = model.evaluate(test_images, test_labels)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

1000/1000 [==============================] - 0s 95us/step Restored model, accuracy: 87.20%

## What are these files?

The above code stores the weights to a collection of checkpoint-formatted files that contain only the trained weights in a binary format. Checkpoints contain: * One or more shards that contain your model's weights. * An index file that indicates which weights are stored in a which shard.

If you are only training a model on a single machine, you'll have one shard with the suffix: `.data-00000-of-00001`

## Manually save weights

Above you saw how to load the weights into a model.

Manually saving the weights is just as simple, use the `Model.save_weights`

method.

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

WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f085cca2048>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. 1000/1000 [==============================] - 0s 110us/step Restored model, accuracy: 87.20%

## Save the entire model

The entire model can be saved to a file that contains the weight values, the model's configuration, and even the optimizer's configuration (depends on set up). This allows you to checkpoint a model and resume training later—from the exact same state—without access to the original code.

Saving a fully-functional model is very useful—you can load them in TensorFlow.js (HDF5, Saved Model) and then train and run them in web browsers, or convert them to run on mobile devices using TensorFlow Lite (HDF5, Saved Model)

### As an HDF5 file

Keras provides a basic save format using the HDF5 standard. For our purposes, the saved model can be treated as a single binary blob.

```
model = create_model()
# You need to use a keras.optimizer to restore the optimizer state from an HDF5 file.
model.compile(optimizer='adam',
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
# Save entire model to a HDF5 file
model.save('my_model.h5')
```

Epoch 1/5 1000/1000 [==============================] - 0s 367us/step - loss: 1.1758 - acc: 0.6690 Epoch 2/5 1000/1000 [==============================] - 0s 103us/step - loss: 0.4134 - acc: 0.8840 Epoch 3/5 1000/1000 [==============================] - 0s 111us/step - loss: 0.2891 - acc: 0.9320 Epoch 4/5 1000/1000 [==============================] - 0s 112us/step - loss: 0.2082 - acc: 0.9500 Epoch 5/5 1000/1000 [==============================] - 0s 115us/step - loss: 0.1450 - acc: 0.9760

Now recreate the model from that file:

```
# Recreate the exact same model, including weights and optimizer.
new_model = keras.models.load_model('my_model.h5')
new_model.summary()
```

_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= dense_12 (Dense) (None, 512) 401920 _________________________________________________________________ dropout_6 (Dropout) (None, 512) 0 _________________________________________________________________ dense_13 (Dense) (None, 10) 5130 ================================================================= Total params: 407,050 Trainable params: 407,050 Non-trainable params: 0 _________________________________________________________________

Check its accuracy:

```
loss, acc = new_model.evaluate(test_images, test_labels)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

1000/1000 [==============================] - 0s 130us/step Restored model, accuracy: 86.60%

This technique saves everything:

- The weight values
- The model's configuration(architecture)
- The optimizer configuration

Keras saves models by inspecting the architecture. Currently, it is not able to save TensorFlow optimizers (from `tf.train`

). When using those you will need to re-compile the model after loading, and you will loose the state of the optimizer.

### As a `saved_model`

Build a fresh model:

```
model = create_model()
model.fit(train_images, train_labels, epochs=5)
```

Epoch 1/5 1000/1000 [==============================] - 0s 429us/step - loss: 1.1775 - acc: 0.6680 Epoch 2/5 1000/1000 [==============================] - 0s 120us/step - loss: 0.4389 - acc: 0.8750 Epoch 3/5 1000/1000 [==============================] - 0s 121us/step - loss: 0.2908 - acc: 0.9210 Epoch 4/5 1000/1000 [==============================] - 0s 125us/step - loss: 0.2276 - acc: 0.9330 Epoch 5/5 1000/1000 [==============================] - 0s 137us/step - loss: 0.1609 - acc: 0.9650 <tensorflow.python.keras.callbacks.History at 0x7f085c9749e8>

Create a `saved_model`

:

```
saved_model_path = tf.contrib.saved_model.save_keras_model(model, "./saved_models")
```

WARNING:tensorflow:This model was compiled with a Keras optimizer (<tensorflow.python.keras.optimizers.Adam object at 0x7f086c2d8780>) but is being saved in TensorFlow format with `save_weights`. The model's weights will be saved, but unlike with TensorFlow optimizers in the TensorFlow format the optimizer's state will not be saved. Consider using a TensorFlow optimizer from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a>. WARNING:tensorflow:Model was compiled with an optimizer, but the optimizer is not from <a href="../../api_docs/python/tf/train"><code>tf.train</code></a> (e.g. <a href="../../api_docs/python/tf/train/AdagradOptimizer"><code>tf.train.AdagradOptimizer</code></a>). Only the serving graph was exported. The train and evaluate graphs were not added to the SavedModel. INFO:tensorflow:Signatures INCLUDED in export for Train: None INFO:tensorflow:Signatures INCLUDED in export for Classify: None INFO:tensorflow:Signatures INCLUDED in export for Predict: ['serving_default'] INFO:tensorflow:Signatures INCLUDED in export for Regress: None INFO:tensorflow:Signatures INCLUDED in export for Eval: None INFO:tensorflow:No assets to save. INFO:tensorflow:No assets to write. INFO:tensorflow:SavedModel written to: ./saved_models/temp-b'1548980687'/saved_model.pb

Saved models are placed in a time-stamped directory:

```
!ls saved_models/
```

1548980687

Reload a fresh keras model from the saved model.

```
new_model = tf.contrib.saved_model.load_keras_model(saved_model_path)
new_model
```

<tensorflow.python.keras.engine.sequential.Sequential at 0x7f0840786400>

Run the restored model.

```
# The optimizer was not restored, re-attach a new one.
new_model.compile(optimizer='adam',
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=['accuracy'])
loss, acc = new_model.evaluate(test_images, test_labels)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
```

1000/1000 [==============================] - 0s 289us/step Restored model, accuracy: 86.40%

## What's Next

That was a quick guide to saving and loading in with `tf.keras`

.

The tf.keras guide shows more about saving and loading models with

`tf.keras`

.See Saving in eager for saving during eager execution.

The Save and Restore guide has low-level details about TensorFlow saving.

```
#@title MIT License
#
# Copyright (c) 2017 François Chollet # IGNORE_COPYRIGHT: cleared by OSS licensing
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
```