TensorFlow中怎么导入导出模型

783
2024/4/23 18:49:54
栏目: 深度学习
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在TensorFlow中,可以使用tf.saved_model模块来导入和导出模型。以下是导入和导出模型的示例代码:

导出模型:

import tensorflow as tf

# 定义模型
model = tf.keras.Sequential([
    tf.keras.layers.Dense(64, activation='relu', input_shape=(10,)),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1)
])

# 编译模型
model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['accuracy'])

# 训练模型
model.fit(x_train, y_train, epochs=10)

# 导出模型
tf.saved_model.save(model, 'path_to_saved_model')

导入模型:

import tensorflow as tf

# 导入模型
loaded_model = tf.saved_model.load('path_to_saved_model')

# 使用导入的模型进行预测
predictions = loaded_model.predict(x_test)

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: tensorflow国内镜像安装不了怎么解决