เรียนรู้การจำแนกตัวเลข
เราจะสร้างโมเดลบน digits dataset ซึ่งเป็นชุดข้อมูลตัวอย่างที่โหลดมาพร้อมกับ scikit-learn digits dataset ประกอบด้วย ภาพตัวเลขลายมือขนาด 8x8 พิกเซล ตั้งแต่ 0 ถึง 9:
ชุดข้อมูลถูกแบ่งไว้แล้วเป็น X_train, y_train, X_test, และ X_test โดยใช้ข้อมูล 30% เป็นชุดทดสอบ ป้ายกำกับ (label) ถูก encode แบบ one-hot เรียบร้อยแล้ว จึงไม่จำเป็นต้องใช้ฟังก์ชัน to_categorical() ของ Keras
มาสร้าง model ใหม่นี้กัน!
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Deep Learning เบื้องต้นด้วย Keras
คำแนะนำการฝึกหัด
- เพิ่ม layer
Denseที่มี 16 neurons พร้อม activation แบบreluและกำหนดinput_shapeให้เท่ากับจำนวนพิกเซลทั้งหมดของภาพตัวเลขขนาด 8x8 - เพิ่ม layer
Denseที่มี 10 outputs พร้อม activation แบบsoftmax - คอมไพล์โมเดลด้วย
adam,categorical_crossentropy, และ metrics แบบaccuracy - ตรวจสอบว่าโมเดลทำงานได้ถูกต้องโดยทดลองพยากรณ์บน
X_train
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Instantiate a Sequential model
model = Sequential()
# Input and hidden layer with input_shape, 16 neurons, and relu
model.add(Dense(____, input_shape = (____,), activation = ____))
# Output layer with 10 neurons (one per digit) and softmax
model.____(____)
# Compile your model
model.____(optimizer = ____, loss = ____, metrics = [____])
# Test if your model is well assembled by predicting before training
print(model.predict(____))