TensorFlow framework ML của Google. Keras API deep learning Python trên TensorFlow. Functional API hỗ trợ mô hình lồng nhau (nested models) để ensemble.
Sử dụng Google Colab. Code tạo ensemble bằng cách trung bình dự đoán của 3 mô hình con:
Ví dụ
def get_model():
inputs = keras.Input(shape=(128,))
outputs = layers.Dense(1)(inputs)
return keras.Model(inputs, outputs)
print("Gọi phương thức 'get_model'")
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("Trung bình các lớp trong mô hình")
my_outputs = layers.average([y1, y2, y3])
print("Tạo ensemble model")
ensemble_model = keras.Model(inputs=my_inputs, outputs=my_outputs)
Nguồn: TensorFlow Guide
Đầu ra
Gọi phương thức 'get_model' Trung bình các lớp trong mô hình Tạo ensemble model
Giải thích
- Mô hình có thể lồng nhau (chứa sub-model).
- Sub-model dùng cho ensemble: kết hợp nhiều mô hình thành một, trung bình dự đoán.