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

วิธีใช้ Keras ทำ Ensemble Model ด้วย Python

TensorFlow: ML Framework Google Open-source ใช้ Python ทำ Algorithms, Deep Learning ติดตั้ง pip install tensorflow Tensor = Multi-dimensional array ใน Data Flow Graph

Keras: Deep Learning API Python จาก ONEIROS รันบน TensorFlow Scalable, Cross-platform เข้าถึงด้วย from tensorflow import keras

Functional API

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

ตัวอย่าง: Ensemble Model (Average Predictions)

def get_model():
    inputs = keras.Input(shape=(128,))
    outputs = layers.Dense(1)(inputs)
    return keras.Model(inputs, outputs)

print("Calling the 'get_model' method")
model_1 = get_model()
model_2 = get_model()
model_3 = get_model()

my_inputs = keras.Input(shape=(128,))
y1 = model_1(my_inputs)
y2 = model_2(my_inputs)
y3 = model_3(my_inputs)

print("The average of the layers in the model")
my_outputs = layers.average([y1, y2, y3])

print("Ensemble model is being created")
ensemble_model = keras.Model(inputs=my_inputs, outputs=my_outputs)

Code credit: TensorFlow Keras Functional Guide

ผลลัพธ์

Calling the 'get_model' method
The average of the layers in the model
Ensemble model is being created

คำอธิบาย

  • โมเดลสามารถ Nested ได้ (ประกอบด้วย Sub-models)
  • Sub-models ใช้ใน Ensembling
  • รวมหลายโมเดลเป็นโมเดลเดียว คาดการณ์โดยเฉลี่ย (Average) จากทุกโมเดล