溫馨提示×

TensorFlow中怎么導入導出模型

小億
105
2024-05-10 18:49:54
欄目: 深度學習

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

0