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

