ENع
→Loss
الدرس 05 · الأسس

Loss: measuring mistakes

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة10 خطوات5 أسئلةBeginner
عن هذا الدرس

A model learns by making its mistakes smaller, so first it needs one number that says how wrong it is: the loss. Follow four deliveries through a tiny model, watch plain errors cancel out to a misleading 0, square them, and average them into the mean squared error. Then use the loss to compare models, see training as a loop that pushes the loss down, and meet log loss, the loss classifiers use.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 5 · تفاعلي
01 / 10

Models make mistakes

A model makes guesses, and some are wrong. To improve it, we first need one number that says how wrong it is overall. That number is the loss, also called the cost. Smaller loss means smaller mistakes.

model    makes a guess for each example
loss     one number: how wrong the guesses are, overall

loss big    -> the guesses are far off
loss small  -> the guesses are close
loss 0      -> every guess is exactly right
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Models make mistakes

    A model makes guesses, and some are wrong. To improve it, we first need one number that says how wrong it is overall. That number is the loss, also called the cost. Smaller loss means smaller mistakes.

    model    makes a guess for each example
    loss     one number: how wrong the guesses are, overall
    
    loss big    -> the guesses are far off
    loss small  -> the guesses are close
    loss 0      -> every guess is exactly right
  2. Four real deliveries

    Our task: guess how many minutes a delivery takes from its distance. We have four past deliveries with their real times: 1 km took 3, 2 km took 3, 3 km took 8 and 4 km took 6 minutes. The real times are the labels.

    training data: distance -> real time (the label)
      1 km   3 min
      2 km   3 min
      3 km   8 min
      4 km   6 min
    
    the model never sees the real time when it guesses;
    we use the real time to check the guess
  3. The model guesses

    Our model is one rule with one adjustable number, w: guess = w × km. Start with w = 2. Each distance goes through the model: the guesses are 2, 4, 6, 8 minutes, one per delivery.

    w = 2
    guess = w * x
      1 km  ->  2 * 1 = 2 min
      2 km  ->  2 * 2 = 4 min
      3 km  ->  2 * 3 = 6 min
      4 km  ->  2 * 4 = 8 min
  4. Each error

    The error is real time minus guess. 3 − 2 = +1, 3 − 4 = −1, 8 − 6 = +2, 6 − 8 = −2. A plus means the guess was too low; a minus, too high.

    err = y - guess      (real - guess)
      3 - 2 = +1
      3 - 4 = -1
      8 - 6 = +2
      6 - 8 = -2
    
    + guess too low     - guess too high
  5. Plain errors cancel out

    Why not just average the errors? +1 − 1 + 2 − 2 = 0, so the average error is 0, as if the model were perfect. But every guess was wrong: misses that are too high cancel misses that are too low.

    sum of errors:  +1 - 1 + 2 - 2 = 0
    average error:  0 / 4 = 0
    
    looks perfect, but all four guesses were wrong
    so the plain average is a bad loss
  6. Square every error

    The fix: square each error, which means multiply it by itself. (+1)² = 1, (−1)² = 1, (+2)² = 4, (−2)² = 4. Squares are never negative, so nothing cancels, and a miss twice as big costs four times as much.

    total += err ** 2      (** is "to the power of")
      (+1)² = 1
      (-1)² = 1
      (+2)² = 4
      (-2)² = 4
    
    a miss of 2 costs 4, a miss of 1 costs 1
  7. Average them: the loss

    Add the squares, 1 + 1 + 4 + 4 = 10, and divide by the 4 deliveries: 2.5. This is the mean squared error (MSE), our loss: one number for the whole data set, in minutes squared.

    loss = total / len(data)
      total = 1 + 1 + 4 + 4 = 10
      loss  = 10 / 4 = 2.5
    
    MSE = mean squared error
    the program prints 2.5
  8. Compare models by loss

    Now try other values of w and measure each one. With w = 1 the loss is 8.5; with w = 3 it is 11.5; with w = 2 it is 2.5. Each bar shows one loss. Lower is better, so w = 2 wins.

    same data, three models, three losses
      w = 1   guesses 1, 2, 3, 4   loss 8.5
      w = 2   guesses 2, 4, 6, 8   loss 2.5
      w = 3   guesses 3, 6, 9, 12  loss 11.5
    
    the lowest bar is the best model of the three
  9. Training lowers the loss

    Training is a loop: guess, measure the loss, nudge w to lower it, repeat. From w = 1, nudges to 1.5 and then 1.9 bring the loss down: 8.5, then 3.625, then 2.425. For this data, no w does better than 1.9.

    the training loop:
      1 guess      2 measure the loss
      3 nudge w    4 repeat
    
      w = 1    loss 8.5
      w = 1.5  loss 3.625
      w = 1.9  loss 2.425
    
    best possible here: w = 1.9, loss 2.425
  10. Log loss for classifiers

    Classifiers, which pick a label, usually use log loss: −log(p), where p is the chance the model gave the right answer. p = 0.9 costs 0.11; p = 0.1 costs 2.30. Being sure and wrong costs the most.

    log loss = -log(p)      (natural log)
      p = chance given to the right answer
    
      p = 0.9   ->  -log(0.9) = 0.11
      p = 0.1   ->  -log(0.1) = 2.30
      p = 1     ->  -log(1)   = 0.00  (sure and right)
    
    it is also called cross-entropy loss

اختبار يتاح بعد الخطوة الأخيرة.