TensorFlow2.0保存模型

介绍

模型保存有5种:1、整体保存;2、网络架构保存;3、权重保存;4、回调保存;5、自定义训练模型的保存

1、整体保存:权重值,模型配置(架构),优化器配置

整个模型可以保存到一个文件中,其中包含权重值、模型配置乃至优化器配置。这样,您就可以为模型设置检查点,并稍后从完全相同的状态继续训练,而无需访问原始代码。

在Keras中保存完全可以正常使用的额模型非常有用,您可以在TensorFlow.js中加载它们,然后在网络浏览器中训练和运行它们。

Keras使用HDF5标准提供基本的保存格式。

# -*- coding: UTF-8 -*-"""
Author: LGD
FileName: save_model
DateTime: 2020/12/23 16:21 
SoftWare: PyCharm
"""# the whole model saveimport tensorflow as tf
import os
import pandas
import numpy as np
import matplotlib.pyplot as plt

(train_image, train_label), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()

train_image = train_image / 255
test_image = test_image / 255

# model = tf.keras.Sequential()# model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))# model.add(tf.keras.layers.Dense(128, activation='relu'))# model.add(tf.keras.layers.Dense(10, activation='softmax'))## model.summary()## model.compile(#     optimizer='adam',#     loss='sparse_categorical_crossentropy',#     metrics=['acc']# )## model.fit(train_image, train_label, epochs=5)

# save model# model.save('less_model.h5')

# use the model
new_model = tf.keras.models.load_model('less_model.h5')
new_model.summary()

# evaluate the model
eva_result = new_model.evaluate(test_image, test_label, verbose=0)  # 0表示不显示提示
print('evaluate result: ', eva_result)

2、仅保存架构

有时我们只对模型的架构感兴趣,而无需保存权重值或优化器。在这种情况下,可以仅保存模型的“配置”。

json_config = model.to_json()
reinitialized_model = tf.keras.models.model_from_json(json_config)
reinitialized_model.summary()
# 若要使用就需要配置
reinitialized_model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['acc']
)

3、仅保存权重

有时我们只需要保存模型的状态(其权重值),而对模型架构不感兴趣。在这种情况下,可以通过get_weights()获取权重值,并通过set_weights()设置权重值。

 weights = model.get_weights()
print(weights)
# 设置权重
reinitialized_model.set_weights(weights)
# 保存权重
model.save_weights('less_weights.h5')

reinitialized_model.load_weights('less_weights.h5')
# 保存架构和保存权重合在一起并不能和保存整个模型等同,还有优化器配置没有保存。

4、在训练期间保存检查点

在训练期间或训练结束时自动保存检查点,这样一来,你便可以使用经过训练的模型,而无需重新训练该模型,或从上次暂停的地方继续训练,以防训练过程中断。

回调函数:tf.keras.callbacks.ModelCheckpoint

# # 4、在训练期间保存检查点# checkpoint_path = 'cp.ckpt'# cp_callback = tf.keras.callbacks.ModelCheckpoint(checkpoint_path, save_weights_only=True)## model = tf.keras.Sequential()# model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))# model.add(tf.keras.layers.Dense(128, activation='relu'))# model.add(tf.keras.layers.Dense(10, activation='softmax'))## model.summary()## model.compile(#     optimizer='adam',#     loss='sparse_categorical_crossentropy',#     metrics=['acc']# )## eva_result = model.evaluate(test_image, test_label, verbose=0)  # 0表示不显示提示# print(eva_result)# # model.fit(train_image, train_label, epochs=5, callbacks=[cp_callback])## model.load_weights(checkpoint_path)# eva_result = model.evaluate(test_image, test_label, verbose=0)  # 0表示不显示提示# print(eva_result)

5、在自定训练中保存模型

# 在自定义训练中保存检查点
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

optimizer = tf.keras.optimizers.Adam()
loss_func = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)


def loss(model, x, y):
    y_ = model(x)
    return loss_func(y, y_)


train_loss = tf.keras.metrics.Mean('train_loss', dtype=tf.float32)
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('train_accuracy')
test_loss = tf.keras.metrics.Mean('test_loss', dtype=tf.float32)
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy('test_accuracy')


def train_step(model, images, labels):
    with tf.GradientTape() as t:
        pred = model(images)
        loss_step = loss_func(labels, pred)
    grads = t.gradient(loss_step, model.trainable_variables)
    optimizer.apply_gradients(zip(grads, model.trainable_variables))
    train_loss(loss_step)
    train_accuracy(labels, pred)


dataset = tf.data.Dataset.from_tensor_slices((train_image, train_label))
dataset = dataset.shuffle(10000).batch(32)

cp_dir = './custom_train_cp'
cp_prefix = os.path.join(cp_dir, 'ckpt')
checkpoint = tf.train.Checkpoint(
    optimizer=optimizer,
    model=model
)


# def train():#     for epoch in range(5):#         for (batch, (images, labels)) in enumerate(dataset):#             train_step(model, images, labels)#         print('Epoch{} loss is {}'.format(epoch, train_loss.result()))#         print('Epoch{} accuracy is {}'.format(epoch, train_accuracy.result()))##         train_accuracy.reset_states()#         train_loss.reset_states()##         checkpoint.save(file_prefix=cp_prefix)### # 训练# train()# 恢复模型
print(checkpoint.restore(tf.train.latest_checkpoint(cp_dir)))
# 测试恢复的模型
print(tf.argmax(model(train_image, training=False), axis=-1).numpy())
print(train_label)
print((tf.argmax(model(train_image, training=False), axis=-1).numpy() ==
      train_label).sum()/len(train_label))

你可能感兴趣的:(神经网络和深度学习,TensorFlow,深度学习,tensorflow,神经网络,python)