ขยายขนาดไปสู่หลายจุดข้อมูล
เราได้เห็นแล้วว่าค่า weights ที่ต่างกันจะให้ความแม่นยำที่ต่างกันในการทำนายค่าเดียว แต่โดยทั่วไปแล้ว การวัดความแม่นยำของโมเดลต้องใช้ข้อมูลหลายจุด ในแบบฝึกหัดนี้ จะเขียนโค้ดเพื่อเปรียบเทียบความแม่นยำของโมเดลจาก weights สองชุด ซึ่งเก็บไว้ใน weights_0 และ weights_1
input_data คือลิสต์ของอาร์เรย์ โดยแต่ละรายการในลิสต์มีข้อมูลสำหรับการทำนายหนึ่งครั้ง
target_actuals คือลิสต์ของตัวเลข โดยแต่ละรายการคือค่าจริงที่ต้องการทำนาย
ในแบบฝึกหัดนี้ จะใช้ฟังก์ชัน mean_squared_error() จาก sklearn.metrics ซึ่งรับค่าจริงและค่าที่ทำนายได้เป็นอาร์กิวเมนต์
นอกจากนี้ยังใช้ฟังก์ชัน predict_with_network() ที่โหลดไว้ล่วงหน้า ซึ่งรับอาร์เรย์ของข้อมูลเป็นอาร์กิวเมนต์แรก และ weights เป็นอาร์กิวเมนต์ที่สอง
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Deep Learning in Python
คำแนะนำการฝึกหัด
- Import
mean_squared_errorจากsklearn.metrics - ใช้
forloop วนซ้ำในแต่ละแถวของinput_data:- ทำนายผลสำหรับแต่ละแถวด้วย
weights_0โดยใช้ฟังก์ชันpredict_with_network()แล้ว append ผลลัพธ์ไปยังmodel_output_0 - ทำแบบเดียวกันกับ
weights_1โดย append ผลลัพธ์ไปยังmodel_output_1
- ทำนายผลสำหรับแต่ละแถวด้วย
- คำนวณค่า mean squared error ของ
model_output_0และmodel_output_1โดยใช้ฟังก์ชันmean_squared_error()โดยอาร์กิวเมนต์แรกคือค่าจริง (target_actuals) และอาร์กิวเมนต์ที่สองคือค่าที่ทำนายได้ (model_output_0หรือmodel_output_1)
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
from sklearn.metrics import mean_squared_error
# Create model_output_0
model_output_0 = []
# Create model_output_1
model_output_1 = []
# Loop over input_data
for row in input_data:
# Append prediction to model_output_0
model_output_0.append(____)
# Append prediction to model_output_1
model_output_1.append(____)
# Calculate the mean squared error for model_output_0: mse_0
mse_0 = ____
# Calculate the mean squared error for model_output_1: mse_1
mse_1 = ____
# Print mse_0 and mse_1
print("Mean squared error with weights_0: %f" %mse_0)
print("Mean squared error with weights_1: %f" %mse_1)