รับมือกับ overfitting ด้วย dropout
ปัญหาที่พบบ่อยในโครงข่ายประสาทเทียมคือแนวโน้มที่จะเกิด overfitting กับข้อมูลฝึก กล่าวคือค่าเมตริกอย่าง R\(^2\) หรือความแม่นยำจะสูงสำหรับชุดข้อมูลฝึก แต่ต่ำสำหรับชุดทดสอบและชุด validation เนื่องจากโมเดลเรียนรู้ noise ในข้อมูลฝึกมากเกินไป
วิธีหนึ่งที่ช่วยป้องกัน overfitting คือการใช้ dropout ซึ่งจะสุ่มปิดการทำงานของ neuron บางส่วนในระหว่างการฝึก ช่วยไม่ให้โครงข่ายประสาทเทียมจดจำ noise ในข้อมูล keras มี layer ชื่อ Dropout สำหรับจุดประสงค์นี้ โดยต้องกำหนดอัตรา dropout ซึ่งคือสัดส่วนของการเชื่อมต่อที่จะถูกปิดในระหว่างการฝึก โดยระบุเป็นค่าทศนิยมระหว่าง 0 ถึง 1 ใน Dropout()
ในแบบฝึกหัดนี้ เราจะกลับมาใช้ mean squared error เป็น loss function
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับการเงินด้วย Python
คำแนะนำการฝึกหัด
- เพิ่ม dropout layer (
Dropout()) ต่อจาก Dense layer แรกในโมเดล และกำหนดอัตรา dropout เป็น 20% (0.2) - ใช้ optimizer
adamและ loss functionmseเมื่อ compile โมเดลใน.compile() - Fit โมเดลกับ
scaled_train_featuresและtrain_targetsโดยใช้ 25 epochs
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from keras.layers import Dropout
# Create model with dropout
model_3 = Sequential()
model_3.add(Dense(100, input_dim=scaled_train_features.shape[1], activation='relu'))
model_3.add(____)
model_3.add(Dense(20, activation='relu'))
model_3.add(Dense(1, activation='linear'))
# Fit model with mean squared error loss function
model_3.compile(optimizer=____, loss=____)
history = model_3.fit(____, ____, epochs=____)
plt.plot(history.history['loss'])
plt.title('loss:' + str(round(history.history['loss'][-1], 6)))
plt.show()