当前位置: 首页 > java >正文

Tensorflow2保存和加载模型

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

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

保存模型:

model.save('my_model.h5')

加载模型:

# 加载整个模型
loaded_model = tf.keras.models.load_model('my_model.h5')

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

示例:

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

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

保存权重:

# 只保存权重
model.save_weights('my_model_weights.h5')

加载权重:

# 创建一个新的模型实例(确保架构与原始模型相同)
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,否则会报错!

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.

示例:

def model_handle(x_train, y_train):# 模型构建,多分类的激活函数使用sigmoid 或 softmaxmodel = 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

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

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

http://www.xdnf.cn/news/6081.html

相关文章:

  • 2025年PMP 学习十二 第9章 项目资源管理
  • 02_Servlet
  • .Net HttpClient 使用代理功能
  • Leetcode (力扣)做题记录 hot100(62,64,287,108)
  • C#调用C++dll 过程记录
  • 技术债务积累,如何进行有效管理
  • C++ 日志输出(宏定义)
  • 无人机数据处理与特征提取技术分析!
  • 劫持__security_check_cookie
  • 入门OpenTelemetry——部署OpenTelemetry
  • 分布式1(cap base理论 锁 事务 幂等性 rpc)
  • 微信小程序之将轮播图设计为组件
  • “强强联手,智启未来”凯创未来与绿算技术共筑高端智能家居及智能照明领域新生态
  • 【Alist+RaiDrive挂载网盘到本地磁盘】
  • 面向对象设计模式之代理模式详解
  • 如何查看SD卡存储扇区分配表?有什么不同之处
  • 远程连接电脑的方法?异地远程桌面连接和三方软件实现
  • Java 重试机制详解
  • QT之QComboBox组件
  • 软考 系统架构设计师系列知识点之杂项集萃(59)
  • 【springcloud学习(dalston.sr1)】Eureka单个服务端的搭建(含源代码)(三)
  • Python 常用模块(八):logging模块
  • 基于GpuGeek平台的深度学习项目
  • Keil5 MDK 安装教程
  • LeetCode 热题 100 35.搜索插入位置
  • python打包exe报错:处理文件时错误:Excel xlsx file; not supported
  • iOS Safari调试教程
  • vue使用路由技术实现登录成功后跳转到首页
  • 【Vue 3 + Vue Router 4】如何正确重置路由实例(resetRouter)——避免“VueRouter is not defined”错误
  • 数据结构与算法:状压dp