Tensorflow2保存和加载模型

1、model.save() and model.load()

此种方法可保存模型的结构、参数等内容。加载模型后无需设置即可使用!

保存模型:

python 复制代码
model.save('my_model.h5')

加载模型:

python 复制代码
# 加载整个模型
loaded_model = tf.keras.models.load_model('my_model.h5')

注意,创建的模型不能使用自定义的loss函数等方法,否则导入时会出错!

示例:

python 复制代码
model_file = "data/model/multi_labels_model.h5"    # 模型文件路径
def model_handle(x_train, y_train):
    if os.path.exists(model_file):
        print("---load the model---")
        model = tf.keras.models.load_model(model_file) # 导入已存在的模型
    else:
        # 模型构建
        model = tf.keras.Sequential([
            tf.keras.layers.LSTM(128),
            tf.keras.layers.Dense(class_num, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
        ])
        # 编译模型,不能使用自定义函数方法,否则导入模型会有问题
        model.compile(loss="BinaryCrossentropy", optimizer='adam', metrics=['accuracy'])
        
        history = model.fit(x_train, y_train, epochs=epoch_num, batch_size=1, verbose=1, 
                            callbacks=[PrintPredictionsCallback(x_train, y_train)])

        model.summary()
        model.save(model_file)
    return model

2、model.save_weight() and model.load_weight()

此方法只保存和加载模型的权重。

保存权重:

python 复制代码
# 只保存权重
model.save_weights('my_model_weights.h5')

加载权重:

python 复制代码
# 创建一个新的模型实例(确保架构与原始模型相同)
new_model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(32,)),
    tf.keras.layers.Dense(1)
])
# new_model.build(input_shape=x_train.shape) # 如果模型创建时没有规定input_shape,需要创建
# 加载权重到新模型
new_model.load_weights('my_model_weights.h5')

此方法的模型可以使用自定义的函数方法。

注意:以H5格式加载子类模型的参数时,需要提前建立模型,规定输入网络的shape,否则会报错!

python 复制代码
ValueError: Unable to load weights saved in HDF5 format into a subclassed Model which has not created its variables yet. Call the Model first, then load the weights.

示例:

python 复制代码
def model_handle(x_train, y_train):
    # 模型构建,多分类的激活函数使用sigmoid 或 softmax
    model = tf.keras.Sequential([
        tf.keras.layers.LSTM(128),
        tf.keras.layers.Dense(class_num, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
    ])
    if os.path.exists(model_file):
        print("-----load model weights-----")
        model.build(input_shape=x_train.shape)  # 以H5格式加载子类模型的参数时,需要提前建立模型,规定输入网络的shape,否则会报错
        model.load_weights(model_file)
    else:
        # 编译模型,使用自定义loss函数
        model.compile(loss=custom_loss, optimizer='adam', metrics=['accuracy'])
        # model.compile(loss="BinaryCrossentropy", optimizer='adam', metrics=['accuracy'])

        history = model.fit(x_train, y_train, epochs=epoch_num, batch_size=1, verbose=1, 
                            callbacks=[PrintPredictionsCallback(x_train, y_train)])

        model.summary()
        model.save_weights(model_file)

    return model

3、model.checkpoint

主要是用于模型的断点续训。用法参考如下:

python 复制代码
checkpoint_save_path = "./checkpoint/my_checkpoint.ckpt"

if os.path.exists(checkpoint_save_path + '.index'):
    print('-------------load the model-----------------')
    model.load_weights(checkpoint_save_path)

cp_callback = tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_save_path,
                                                 save_weights_only=True,
                                                 save_best_only=True,
                                                 monitor='val_loss')

history = model.fit(x_train, y_train, batch_size=64, epochs=50, validation_data=(x_test, y_test), validation_freq=1,
                    callbacks=[cp_callback])

model.summary()
相关推荐
Hcoco_me8 小时前
大模型面试题62:PD分离
人工智能·深度学习·机器学习·chatgpt·机器人
OpenCSG8 小时前
AgenticOps 如何重构企业 AI 的全生命周期管理体系
大数据·人工智能·深度学习
All The Way North-8 小时前
RNN基本介绍
rnn·深度学习·nlp·循环神经网络·时间序列
yatingliu20198 小时前
将深度学习环境迁移至老旧系统| 个人学习笔记
笔记·深度学习·学习
kebijuelun9 小时前
REAP the Experts:去掉 MoE 一半专家还能保持性能不变
人工智能·gpt·深度学习·语言模型·transformer
医工交叉实验工坊9 小时前
从零详解WGCNA分析
人工智能·机器学习
ldccorpora9 小时前
Multiple-Translation Arabic (MTA) Part 2数据集介绍,官网编号LDC2005T05
人工智能·深度学习·自然语言处理·动态规划·语音识别
其美杰布-富贵-李11 小时前
深度学习中的 tmux
服务器·人工智能·深度学习·tmux
LaughingZhu11 小时前
Product Hunt 每日热榜 | 2026-01-12
人工智能·经验分享·深度学习·神经网络·产品运营
不如自挂东南吱11 小时前
空间相关性 和 怎么捕捉空间相关性
人工智能·深度学习·算法·机器学习·时序数据库