保存和恢复模型

在 tensorflow.google.cn 上查看 在 Google Colab 运行 在 Github 上查看源代码 下载此 notebook

模型可以在训练期间和训练完成后进行保存。这意味着模型可以从任意中断中恢复,并避免耗费比较长的时间在训练上。保存也意味着您可以共享您的模型,而其他人可以通过您的模型来重新创建工作。在发布研究模型和技术时,大多数机器学习从业者分享:

  • 用于创建模型的代码
  • 模型训练的权重 (weight) 和参数 (parameters) 。

共享数据有助于其他人了解模型的工作原理,并使用新数据自行尝试。

注意:小心不受信任的代码——Tensorflow 模型是代码。有关详细信息,请参阅 安全使用Tensorflow

选项

保存 Tensorflow 的模型有许多方法——具体取决于您使用的 API。本指南使用 tf.keras, 一个高级 API 用于在 Tensorflow 中构建和训练模型。有关其他方法的实现,请参阅 TensorFlow 保存和恢复指南或保存到 eager

配置

安装并导入

安装并导入Tensorflow和依赖项:


!pip install -q pyyaml h5py  # 需要以 HDF5 格式保存模型
from __future__ import absolute_import, division, print_function, unicode_literals

import os

import tensorflow as tf
from tensorflow import keras

print(tf.version.VERSION)
2.0.0

获取示例数据集

要演示如何保存和加载权重,您将使用 MNIST 数据集. 要加快运行速度,请使用前1000个示例:

(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

定义模型

首先构建一个简单的序列(sequential)模型:

# 定义一个简单的序列模型
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, activation='softmax')
  ])

  model.compile(optimizer='adam',
                loss='sparse_categorical_crossentropy',
                metrics=['accuracy'])

  return model

# 创建一个基本的模型实例
model = create_model()

# 显示模型的结构
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
_________________________________________________________________

在训练期间保存模型(以 checkpoints 形式保存)

您可以使用训练好的模型而无需从头开始重新训练,或在您打断的地方开始训练,以防止训练过程没有保存。 tf.keras.callbacks.ModelCheckpoint 允许在训练的过程中结束时回调保存的模型。

Checkpoint 回调用法

创建一个只在训练期间保存权重的 tf.keras.callbacks.ModelCheckpoint 回调:

checkpoint_path = "training_1/cp.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# 创建一个保存模型权重的回调
cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
                                                 save_weights_only=True,
                                                 verbose=1)

# 使用新的回调训练模型
model.fit(train_images, 
          train_labels,  
          epochs=10,
          validation_data=(test_images,test_labels),
          callbacks=[cp_callback])  # 通过回调训练

# 这可能会生成与保存优化程序状态相关的警告。
# 这些警告(以及整个笔记本中的类似警告)是防止过时使用,可以忽略。
Train on 1000 samples, validate on 1000 samples
Epoch 1/10
 832/1000 [=======================>......] - ETA: 0s - loss: 1.2203 - accuracy: 0.6370 
Epoch 00001: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 1s 909us/sample - loss: 1.1273 - accuracy: 0.6700 - val_loss: 0.7261 - val_accuracy: 0.7830
Epoch 2/10
 800/1000 [=======================>......] - ETA: 0s - loss: 0.4441 - accuracy: 0.8800
Epoch 00002: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 163us/sample - loss: 0.4224 - accuracy: 0.8860 - val_loss: 0.5240 - val_accuracy: 0.8370
Epoch 3/10
 800/1000 [=======================>......] - ETA: 0s - loss: 0.2870 - accuracy: 0.9225
Epoch 00003: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 185us/sample - loss: 0.2738 - accuracy: 0.9280 - val_loss: 0.4789 - val_accuracy: 0.8480
Epoch 4/10
 768/1000 [======================>.......] - ETA: 0s - loss: 0.2167 - accuracy: 0.9414
Epoch 00004: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 160us/sample - loss: 0.2092 - accuracy: 0.9450 - val_loss: 0.4725 - val_accuracy: 0.8440
Epoch 5/10
 832/1000 [=======================>......] - ETA: 0s - loss: 0.1525 - accuracy: 0.9663
Epoch 00005: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 146us/sample - loss: 0.1630 - accuracy: 0.9630 - val_loss: 0.4204 - val_accuracy: 0.8630
Epoch 6/10
 768/1000 [======================>.......] - ETA: 0s - loss: 0.1115 - accuracy: 0.9805
Epoch 00006: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 152us/sample - loss: 0.1170 - accuracy: 0.9780 - val_loss: 0.4273 - val_accuracy: 0.8630
Epoch 7/10
 832/1000 [=======================>......] - ETA: 0s - loss: 0.0890 - accuracy: 0.9868
Epoch 00007: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 149us/sample - loss: 0.0875 - accuracy: 0.9870 - val_loss: 0.4281 - val_accuracy: 0.8650
Epoch 8/10
 800/1000 [=======================>......] - ETA: 0s - loss: 0.0710 - accuracy: 0.9900
Epoch 00008: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 154us/sample - loss: 0.0697 - accuracy: 0.9900 - val_loss: 0.4510 - val_accuracy: 0.8560
Epoch 9/10
 800/1000 [=======================>......] - ETA: 0s - loss: 0.0458 - accuracy: 0.9987
Epoch 00009: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 155us/sample - loss: 0.0460 - accuracy: 0.9970 - val_loss: 0.4158 - val_accuracy: 0.8690
Epoch 10/10
 736/1000 [=====================>........] - ETA: 0s - loss: 0.0415 - accuracy: 0.9973
Epoch 00010: saving model to training_1/cp.ckpt
1000/1000 [==============================] - 0s 158us/sample - loss: 0.0402 - accuracy: 0.9980 - val_loss: 0.4097 - val_accuracy: 0.8700

<tensorflow.python.keras.callbacks.History at 0x7f804abec1d0>

这将创建一个 TensorFlow checkpoint 文件集合,这些文件在每个 epoch 结束时更新:

!ls {checkpoint_dir}
checkpoint           cp.ckpt.data-00001-of-00002
cp.ckpt.data-00000-of-00002  cp.ckpt.index

创建一个新的未经训练的模型。仅恢复模型的权重时,必须具有与原始模型具有相同网络结构的模型。由于模型具有相同的结构,您可以共享权重,尽管它是模型的不同实例。 现在重建一个新的未经训练的模型,并在测试集上进行评估。未经训练的模型将在机会水平(chance levels)上执行(准确度约为10%):

# 创建一个基本模型实例
model = create_model()

# 评估模型
loss, acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Untrained model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 2.4783 - accuracy: 0.1080
Untrained model, accuracy: 10.80%

然后从 checkpoint 加载权重并重新评估:

# 加载权重
model.load_weights(checkpoint_path)

# 重新评估模型
loss,acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 0.4545 - accuracy: 0.8700
Restored model, accuracy: 87.00%

checkpoint 回调选项

回调提供了几个选项,为 checkpoint 提供唯一名称并调整 checkpoint 频率。

训练一个新模型,每五个 epochs 保存一次唯一命名的 checkpoint :

# 在文件名中包含 epoch (使用 `str.format`)
checkpoint_path = "training_2/cp-{epoch:04d}.ckpt"
checkpoint_dir = os.path.dirname(checkpoint_path)

# 创建一个回调,每 5 个 epochs 保存模型的权重
cp_callback = tf.keras.callbacks.ModelCheckpoint(
    filepath=checkpoint_path, 
    verbose=1, 
    save_weights_only=True,
    period=5)

# 创建一个新的模型实例
model = create_model()

# 使用 `checkpoint_path` 格式保存权重
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:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of samples seen.

Epoch 00005: saving model to training_2/cp-0005.ckpt

Epoch 00010: saving model to training_2/cp-0010.ckpt

Epoch 00015: saving model to training_2/cp-0015.ckpt

Epoch 00020: saving model to training_2/cp-0020.ckpt

Epoch 00025: saving model to training_2/cp-0025.ckpt

Epoch 00030: saving model to training_2/cp-0030.ckpt

Epoch 00035: saving model to training_2/cp-0035.ckpt

Epoch 00040: saving model to training_2/cp-0040.ckpt

Epoch 00045: saving model to training_2/cp-0045.ckpt

Epoch 00050: saving model to training_2/cp-0050.ckpt

<tensorflow.python.keras.callbacks.History at 0x7f80483eb240>

现在查看生成的 checkpoint 并选择最新的 checkpoint :

! ls {checkpoint_dir}
checkpoint            cp-0025.ckpt.data-00001-of-00002
cp-0000.ckpt.data-00000-of-00002  cp-0025.ckpt.index
cp-0000.ckpt.data-00001-of-00002  cp-0030.ckpt.data-00000-of-00002
cp-0000.ckpt.index        cp-0030.ckpt.data-00001-of-00002
cp-0005.ckpt.data-00000-of-00002  cp-0030.ckpt.index
cp-0005.ckpt.data-00001-of-00002  cp-0035.ckpt.data-00000-of-00002
cp-0005.ckpt.index        cp-0035.ckpt.data-00001-of-00002
cp-0010.ckpt.data-00000-of-00002  cp-0035.ckpt.index
cp-0010.ckpt.data-00001-of-00002  cp-0040.ckpt.data-00000-of-00002
cp-0010.ckpt.index        cp-0040.ckpt.data-00001-of-00002
cp-0015.ckpt.data-00000-of-00002  cp-0040.ckpt.index
cp-0015.ckpt.data-00001-of-00002  cp-0045.ckpt.data-00000-of-00002
cp-0015.ckpt.index        cp-0045.ckpt.data-00001-of-00002
cp-0020.ckpt.data-00000-of-00002  cp-0045.ckpt.index
cp-0020.ckpt.data-00001-of-00002  cp-0050.ckpt.data-00000-of-00002
cp-0020.ckpt.index        cp-0050.ckpt.data-00001-of-00002
cp-0025.ckpt.data-00000-of-00002  cp-0050.ckpt.index
latest = tf.train.latest_checkpoint(checkpoint_dir)
latest
'training_2/cp-0050.ckpt'

注意: 默认的 tensorflow 格式仅保存最近的5个 checkpoint 。

如果要进行测试,请重置模型并加载最新的 checkpoint :

# 创建一个新的模型实例
model = create_model()

# 加载以前保存的权重
model.load_weights(latest)

# 重新评估模型
loss, acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 0.5545 - accuracy: 0.8750
Restored model, accuracy: 87.50%

这些文件是什么?

上述代码将权重存储到 checkpoint—— 格式化文件的集合中,这些文件仅包含二进制格式的训练权重。 Checkpoints 包含:

  • 一个或多个包含模型权重的分片。
  • 索引文件,指示哪些权重存储在哪个分片中。

如果你只在一台机器上训练一个模型,你将有一个带有后缀的碎片: .data-00000-of-00001

手动保存权重

您将了解如何将权重加载到模型中。使用 Model.save_weights 方法手动保存它们同样简单。默认情况下, tf.kerassave_weights 特别使用 TensorFlow checkpoints 格式 .ckpt 扩展名和 ( 保存在 HDF5 扩展名为 .h5 保存并序列化模型 ):

# 保存权重
model.save_weights('./checkpoints/my_checkpoint')

# 创建模型实例
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))
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/alpha/guide/checkpoints#loading_mechanics for details.
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/alpha/guide/checkpoints#loading_mechanics for details.
1000/1 - 0s - loss: 0.5545 - accuracy: 0.8750
Restored model, accuracy: 87.50%

保存整个模型

模型和优化器可以保存到包含其状态(权重和变量)和模型参数的文件中。这可以让您导出模型,以便在不访问原始 python 代码的情况下使用它。而且您可以通过恢复优化器状态的方式,从中断的位置恢复训练。

保存完整模型会非常有用——您可以在 TensorFlow.js (HDF5, Saved Model) 加载他们,然后在 web 浏览器中训练和运行它们,或者使用 TensorFlow Lite 将它们转换为在移动设备上运行(HDF5, Saved Model)

将模型保存为HDF5文件

Keras 可以使用 HDF5 标准提供基本保存格式。出于我们的目的,可以将保存的模型视为单个二进制blob:

# 创建一个新的模型实例
model = create_model()

# 训练模型
model.fit(train_images, train_labels, epochs=5)

# 将整个模型保存为HDF5文件
model.save('my_model.h5')
Train on 1000 samples
Epoch 1/5
1000/1000 [==============================] - 0s 337us/sample - loss: 1.0956 - accuracy: 0.6970
Epoch 2/5
1000/1000 [==============================] - 0s 69us/sample - loss: 0.4171 - accuracy: 0.8850
Epoch 3/5
1000/1000 [==============================] - 0s 69us/sample - loss: 0.2752 - accuracy: 0.9270
Epoch 4/5
1000/1000 [==============================] - 0s 65us/sample - loss: 0.2100 - accuracy: 0.9450
Epoch 5/5
1000/1000 [==============================] - 0s 68us/sample - loss: 0.1586 - accuracy: 0.9630

现在,从该文件重新创建模型:

# 重新创建完全相同的模型,包括其权重和优化程序
new_model = keras.models.load_model('my_model.h5')

# 显示网络结构
new_model.summary()
Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_10 (Dense)             (None, 512)               401920    
_________________________________________________________________
dropout_5 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_11 (Dense)             (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________

检查其准确率(accuracy):

loss, acc = new_model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 0.4956 - accuracy: 0.8640
Restored model, accuracy: 86.40%

这项技术可以保存一切:

  • 权重
  • 模型配置(结构)
  • 优化器配置

Keras 通过检查网络结构来保存模型。目前,它无法保存 Tensorflow 优化器(调用自 tf.train)。使用这些的时候,您需要在加载后重新编译模型,否则您将失去优化器的状态。

通过 saved_model 保存

注意:这种保存 tf.keras 模型的方法是实验性的,在将来的版本中可能有所改变。

建立一个新模型,然后训练它:

model = create_model()

model.fit(train_images, train_labels, epochs=5)
Train on 1000 samples
Epoch 1/5
1000/1000 [==============================] - 0s 331us/sample - loss: 1.1637 - accuracy: 0.6750
Epoch 2/5
1000/1000 [==============================] - 0s 66us/sample - loss: 0.4512 - accuracy: 0.8850
Epoch 3/5
1000/1000 [==============================] - 0s 66us/sample - loss: 0.2985 - accuracy: 0.9280
Epoch 4/5
1000/1000 [==============================] - 0s 69us/sample - loss: 0.2193 - accuracy: 0.9440
Epoch 5/5
1000/1000 [==============================] - 0s 70us/sample - loss: 0.1540 - accuracy: 0.9740

<tensorflow.python.keras.callbacks.History at 0x7f803c6f10f0>

创建一个 saved_model,并将其放在带有 tf.keras.experimental.export_saved_model 的带时间戳的目录中:

import time
saved_model_path = "./saved_models/{}".format(int(time.time()))

tf.keras.experimental.export_saved_model(model, saved_model_path)
saved_model_path
WARNING:tensorflow:From <ipython-input-20-9d5aff309515>:4: export_saved_model (from tensorflow.python.keras.saving.saved_model_experimental) is deprecated and will be removed in a future version.
Instructions for updating:
Please use `model.save(..., save_format="tf")` or `tf.keras.models.save_model(..., save_format="tf")`.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1630: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version.
Instructions for updating:
If using Keras pass *_constraint arguments to layers.
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow_core/python/saved_model/signature_def_utils_impl.py:253: build_tensor_info (from tensorflow.python.saved_model.utils_impl) is deprecated and will be removed in a future version.
Instructions for updating:
This function will only be available through the v1 compatibility library as tf.compat.v1.saved_model.utils.build_tensor_info or tf.compat.v1.saved_model.build_tensor_info.
INFO:tensorflow:Signatures INCLUDED in export for Classify: None
INFO:tensorflow:Signatures INCLUDED in export for Regress: None
INFO:tensorflow:Signatures INCLUDED in export for Predict: None
INFO:tensorflow:Signatures INCLUDED in export for Train: ['train']
INFO:tensorflow:Signatures INCLUDED in export for Eval: None
WARNING:tensorflow:Export includes no default signature!
INFO:tensorflow:No assets to save.
INFO:tensorflow:No assets to write.
INFO:tensorflow:Signatures INCLUDED in export for Classify: None
INFO:tensorflow:Signatures INCLUDED in export for Regress: None
INFO:tensorflow:Signatures INCLUDED in export for Predict: None
INFO:tensorflow:Signatures INCLUDED in export for Train: None
INFO:tensorflow:Signatures INCLUDED in export for Eval: ['eval']
WARNING:tensorflow:Export includes no default signature!
INFO:tensorflow:No assets to save.
INFO:tensorflow:No assets to write.
INFO:tensorflow:Signatures INCLUDED in export for Classify: None
INFO:tensorflow:Signatures INCLUDED in export for Regress: None
INFO:tensorflow:Signatures INCLUDED in export for Predict: ['serving_default']
INFO:tensorflow:Signatures INCLUDED in export for Train: 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/1570196971/saved_model.pb

'./saved_models/1570196971'

列出您保存的模型:

!ls saved_models/
1570196971

从保存的模型重新加载新的 Keras 模型:

new_model = tf.keras.experimental.load_from_saved_model(saved_model_path)

# 显示网络结构
new_model.summary()
WARNING:tensorflow:From <ipython-input-22-0668b6b97833>:1: load_from_saved_model (from tensorflow.python.keras.saving.saved_model_experimental) is deprecated and will be removed in a future version.
Instructions for updating:
The experimental save and load functions have been  deprecated. Please switch to `tf.keras.models.load_model`.
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.iter
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_1
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.beta_2
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.decay
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer.learning_rate
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer_with_weights-0.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer_with_weights-0.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer_with_weights-1.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'm' for (root).layer_with_weights-1.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer_with_weights-0.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer_with_weights-0.bias
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer_with_weights-1.kernel
WARNING:tensorflow:Unresolved object in checkpoint: (root).optimizer's state 'v' for (root).layer_with_weights-1.bias
WARNING:tensorflow:A checkpoint was restored (e.g. tf.train.Checkpoint.restore or tf.keras.Model.load_weights) but not all checkpointed values were used. See above for specific issues. Use expect_partial() on the load status object, e.g. tf.train.Checkpoint.restore(...).expect_partial(), to silence these warnings, or use assert_consumed() to make the check explicit. See https://www.tensorflow.org/alpha/guide/checkpoints#loading_mechanics for details.
Model: "sequential_6"
_________________________________________________________________
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
_________________________________________________________________

使用恢复的模型运行预测:

model.predict(test_images).shape
(1000, 10)
# 必须在评估之前编译模型。
# 如果仅部署已保存的模型,则不需要此步骤。

new_model.compile(optimizer=model.optimizer,  # 保留已加载的优化程序
                  loss='sparse_categorical_crossentropy', 
                  metrics=['accuracy'])

# 评估已恢复的模型
loss, acc = new_model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc))
1000/1 - 0s - loss: 0.6147 - accuracy: 0.8550
Restored model, accuracy: 85.50%
#@title MIT License
#
# Copyright (c) 2017 François Chollet
#
# 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.