Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Keras บันทึก (Save) และ Serialize โมเดลด้วย Python

TensorFlow: ML Framework Google Open-source Tensor = Multi-dimensional array (Rank, Type, Shape) ติดตั้ง pip install tensorflow

Keras: Deep Learning API Python ระดับสูง จาก ONEIROS รันบน TensorFlow Scalable, Cross-platform (TPU, GPU, Web, Mobile) เข้าถึงด้วย from tensorflow import keras

Functional API

ยืดหยุ่นกว่า Sequential API รองรับ Non-linear topology, Shared layers, Multiple I/O สร้าง DAG ของเลเยอร์

ตัวอย่าง: Save และ Load โมเดล

print("Save the model to a file")
model.save("path_to_my_model")

print("Delete the model")
del model

print("Recreating the model from the saved model")
model = keras.models.load_model("path_to_my_model")

Code credit: TensorFlow Keras Functional Guide

ผลลัพธ์

Save the model to a file
INFO:tensorflow:Assets written to: path_to_my_model/assets
Delete the model
Recreating the model from the saved model

คำอธิบาย

  • บันทึกโมเดลลงไฟล์ด้วย model.save() (รวม Architecture, Weights, Optimizer State)
  • ลบโมเดลตัวแปร
  • โหลดกลับมาด้วย keras.models.load_model()