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

วิธีทำ Transfer Learning ด้วย Keras และ Python

TensorFlow: ML Framework Google Open-source Tensor = Multi-dimensional array ใน Data Flow Graph

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

ตัวอย่าง: Transfer Learning แบบ Freeze Bottom Layers

model = keras.Sequential([
    keras.Input(shape=(784)),
    layers.Dense(32, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(32, activation='relu'),
    layers.Dense(10),
])

print("Load the pre-trained weights")
model.load_weights(...)

print("Freeze all the layers except the last layer")
for layer in model.layers[:-1]:
    layer.trainable = False

print("Recompile the model and train it")
print("The last layer weights will be updated")
model.compile(...)
model.fit(...)

Code credit: TensorFlow Keras Sequential Model Guide

ผลลัพธ์

Load the pre-trained weights
Freeze all the layers except the last layer
Recompile the model and train it
The last layer weights will be updated

คำอธิบาย

  • Transfer Learning = Freeze Bottom Layers, Train Top Layers
  • สร้าง Sequential Model
  • โหลด Pre-trained Weights
  • Freeze ทุกเลเยอร์ยกเว้นเลเยอร์สุดท้าย (layer.trainable = False)
  • Recompile และ Fit