溫馨提示×

tensorflow怎么保存模型

小億
87
2024-04-10 14:59:36

在TensorFlow中,可以使用tf.keras.models.save_model()函數(shù)保存模型。以下是一個保存模型的示例代碼:

import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

# 創(chuàng)建一個簡單的神經(jīng)網(wǎng)絡(luò)模型
model = Sequential([
    Dense(10, activation='relu', input_shape=(784,)),
    Dense(10, activation='softmax')
])

# 編譯模型
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

# 訓(xùn)練模型
model.fit(x_train, y_train, epochs=10)

# 保存模型
model.save('my_model.h5')

在這個示例中,我們首先創(chuàng)建了一個簡單的神經(jīng)網(wǎng)絡(luò)模型,編譯并訓(xùn)練模型。然后使用model.save()函數(shù)保存模型為名為’my_model.h5’的HDF5文件??梢允褂胻f.keras.models.load_model()函數(shù)加載保存的模型。

0