การนำโครงข่ายไปใช้กับข้อมูลหลาย observation/แถว
ในแบบฝึกหัดนี้ จะได้กำหนดฟังก์ชันชื่อ predict_with_network() เพื่อสร้างการพยากรณ์สำหรับข้อมูลหลายรายการ ซึ่งโหลดไว้ล่วงหน้าแล้วในชื่อ input_data เช่นเดิม weights ก็ถูกโหลดไว้ล่วงหน้าเช่นกัน รวมถึงฟังก์ชัน relu() ที่กำหนดไว้ในแบบฝึกหัดก่อนหน้าด้วย
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Deep Learning in Python
คำแนะนำการฝึกหัด
- กำหนดฟังก์ชันชื่อ
predict_with_network()ที่รับ argument สองตัว ได้แก่input_data_rowและweightsแล้วคืนค่าการพยากรณ์จากโครงข่ายเป็นผลลัพธ์ - คำนวณค่า input และ output ของแต่ละโหนด แล้วเก็บไว้ในตัวแปร:
node_0_input,node_0_output,node_1_inputและnode_1_output- ในการคำนวณค่า input ของโหนด ให้คูณอาร์เรย์ที่เกี่ยวข้องเข้าด้วยกันแล้วหาผลรวม
- ในการคำนวณค่า output ของโหนด ให้นำฟังก์ชัน
relu()มาใช้กับค่า input ของโหนดนั้น
- คำนวณผลลัพธ์ของโมเดลโดยคำนวณ
input_to_final_layerและmodel_outputด้วยวิธีเดียวกับที่คำนวณค่า input และ output ของโหนด - ใช้
forloop วนซ้ำผ่านinput_data:- เรียกใช้ฟังก์ชัน
predict_with_network()เพื่อสร้างการพยากรณ์สำหรับข้อมูลแต่ละแถวในinput_data-input_data_rowแล้ว append การพยากรณ์แต่ละค่าลงในresults
- เรียกใช้ฟังก์ชัน
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Define predict_with_network()
def predict_with_network(input_data_row, weights):
# Calculate node 0 value
node_0_input = ____
node_0_output = ____
# Calculate node 1 value
node_1_input = ____
node_1_output = ____
# Put node values into array: hidden_layer_outputs
hidden_layer_outputs = np.array([node_0_output, node_1_output])
# Calculate model output
input_to_final_layer = ____
model_output = ____
# Return model output
return(model_output)
# Create empty list to store prediction results
results = []
for input_data_row in input_data:
# Append prediction to results
results.append(____)
# Print results
print(results)