☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

The straight-line model

الأسبوع 11 · الدرس ١ «التنبؤ باستخدام نموذج الذكاء الاصطناعي» (٢): النموذج الخطي ومعنى w وb والكود والتدريبات · 3 دروس
3

The model as a Python function

ICT Prep 3 Term 1, Unit 2, Lesson 1: pp. 68–75
GoalWrite the model as a Python function, and explain that a person typed w and b here while a machine learning model finds them itself.
New words
model function · a function that takes x and returns the prediction ymistake (error) · prediction minus the real valuetraining · adjusting w and b, try after try, to make the mistakes smallerrange of the data · the inputs the model has seen; outside it, predictions are weak
Explanation

Remember Week 8: a function takes an input and returns an output, just like a model. The function predict_score(hours) holds the model with w = 10 and b = 40 and returns w * hours + b. The calls give 60 for 2 hours, 40 for 0 hours and 90 for 5 hours. But who found the numbers 10 and 40? A person typed them. The computer did not learn them. In machine learning the computer adjusts w and b by itself: it makes a prediction, measures the mistake (prediction minus the real value), changes w and b a little and tries again. The table shows three tries for one invented data point, x = 2 with the real mark 65. The mistake gets smaller until it is 0. Real training uses many data points and does this very fast. A model works well only inside the range of its data: predict_score(20) gives 240 marks, which is impossible if the maximum mark is 100.

Python
1def predict_score(hours):
2    w = 10
3    b = 40
4    return w * hours + b
5
6print(predict_score(2))
7print(predict_score(0))
8print(predict_score(5))
Output
60
40
90
Worked examples
TrywbPrediction for x = 2Mistake (prediction - real 65)
153040-25
283854-11
31045650
Three tries for one data point (x = 2, real mark 65). The prediction is w × 2 + b. The mistake is the prediction minus 65. By try 3 the mistake is 0.
Python
1def predict_score(hours):
2    w = 10
3    b = 40
4    return w * hours + b
5
6print(predict_score(20))
Output
240
Outside the range of the data the line can say silly things. The model has only seen 0 to 5 hours, so 20 hours giving 240 marks should not be trusted.
Class activity

Write a function predict_height(days) for a plant with b = 25 and w = 4, and work out the results for 0, 2 and 10 days. Then say what is typed by a person here and what a trained model would find by itself.

Practice
1
What does predict_score(2) return?
Python
1def predict_score(hours):
2    w = 10
3    b = 40
4    return w * hours + b
5
6print(predict_score(2))
7print(predict_score(0))
8print(predict_score(5))
2
In the lesson’s function, who found w = 10 and b = 40?
3
How does a machine learning model change w and b?
4
Why is 240 marks for 20 hours a warning sign?