เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

การเทรนโมเดลด้วย Cross-Validation

ถึงเวลาเทรนโมเดลด้วยพารามิเตอร์ที่ดีที่สุดที่ค้นพบมาแล้ว ได้แก่ learning rate ที่ 0.001, 50 epochs, batch_size ขนาด 128 และ activation แบบ relu

ฟังก์ชัน create_model() จากแบบฝึกหัดก่อนหน้าพร้อมให้ใช้งานแล้ว โดย X และ y ถูกโหลดไว้เป็น features และ labels

ใช้ค่าที่ดีที่สุดที่ค้นพบเมื่อสร้างออบเจกต์ KerasClassifier เพื่อให้ค่าเหล่านั้นถูกนำไปใช้ในการทำ cross-validation

ปิดท้ายบทนี้ด้วยการเทรนโมเดลที่ปรับแต่งอย่างดีบน breast cancer dataset!

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Deep Learning เบื้องต้นด้วย Keras

ดูคอร์ส

คำแนะนำการฝึกหัด

  • นำเข้า KerasClassifier จาก scikit_learn wrappers ของ tensorflow.keras
  • สร้างออบเจกต์ KerasClassifier โดยระบุพารามิเตอร์ที่ดีที่สุดที่ค้นพบ
  • ส่ง model, features และ labels ให้กับ cross_val_score เพื่อทำ cross-validation แบบ 3 folds

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Import KerasClassifier from tensorflow.keras wrappers
from tensorflow.keras.wrappers.____ import ____

# Create a KerasClassifier
model = ____(build_fn = create_model(learning_rate = ____, activation = ____), epochs = ____, 
             batch_size = ____, verbose = 0)

# Calculate the accuracy score for each fold
kfolds = cross_val_score(____, ____, ____, cv = ____)

# Print the mean accuracy
print('The mean accuracy was:', kfolds.mean())

# Print the accuracy standard deviation
print('With a standard deviation of:', kfolds.std())
แก้ไขและรันโค้ด