การเพิ่มเลเยอร์ให้กับโครงข่าย
เราได้เรียนรู้วิธีทดลองกับโครงข่ายที่กว้างขึ้นแล้ว ในแบบฝึกหัดนี้ จะลองสร้างโครงข่ายที่ลึกขึ้น (เพิ่มจำนวน hidden layer)
โมเดลพื้นฐานชื่อ model_1 ถูกเตรียมไว้ให้เป็นจุดเริ่มต้น โดยมี hidden layer 1 ชั้น และ 10 units สามารถดูสรุปโครงสร้างของโมเดลนี้ได้จากผลลัพธ์ที่แสดงอยู่ จากนั้นจะสร้างโครงข่ายที่คล้ายกัน แต่มี hidden layer 3 ชั้น (โดยยังคงใช้ 10 units ในแต่ละชั้น)
การ fit โมเดลทั้งสองจะใช้เวลาสักครู่ ดังนั้นรอสักไม่กี่วินาทีหลังรันโค้ดเพื่อดูผลลัพธ์
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Deep Learning in Python
คำแนะนำการฝึกหัด
- สร้างโมเดลชื่อ
model_2ให้มีลักษณะเดียวกับmodel_1แต่เปลี่ยนเป็น hidden layer 3 ชั้น ชั้นละ 10 units แทนที่จะมีเพียงชั้นเดียว- ใช้
input_shapeเพื่อระบุ input shape ใน hidden layer ชั้นแรก - ใช้ activation
'relu'สำหรับ hidden layer ทั้ง 3 ชั้น และใช้'softmax'สำหรับ output layer ซึ่งควรมี 2 units
- ใช้
- Compile
model_2เช่นเดียวกับที่ทำกับโมเดลก่อนหน้า โดยใช้'adam'เป็นoptimizer,'categorical_crossentropy'สำหรับ loss และmetrics=['accuracy'] - กด 'ส่งคำตอบ' เพื่อ fit โมเดลทั้งสองและดูว่าโมเดลใดให้ผลลัพธ์ที่ดีกว่า!
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# The input shape to use in the first hidden layer
input_shape = (n_cols,)
# Create the new model: model_2
model_2 = ____
# Add the first, second, and third hidden layers
____
____
____
# Add the output layer
____
# Compile model_2
____
# Fit model 1
model_1_training = model_1.fit(predictors, target, epochs=15, validation_split=0.4, verbose=False)
# Fit model 2
model_2_training = model_2.fit(predictors, target, epochs=15, validation_split=0.4, verbose=False)
# Create the plot
plt.plot(model_1_training.history['val_loss'], 'r', model_2_training.history['val_loss'], 'b')
plt.xlabel('Epochs')
plt.ylabel('Validation score')
plt.show()