Error, loss functions, and why they are needed

Case 5. Training a model as minimizing error


Implementation in pure PHP

Up to this point we used loss as a way to evaluate a model. Now we want to see the key idea: training a model is the process of minimizing a loss function. In this case we will literally "watch" training happen without gradient descent and without library "magic": we take a simple dependency $y = 2x$, define a model $ŷ = w·x$, and search for the $w$ that minimizes MSE.

 
<?php

require_once __DIR__ '/code.php';

$x = [1234];
$y = [2468];

$result findBestW($x$y);
$bestW $result['bestW'];
$bestLoss num_format($result['bestLoss']);

$bestWFormatted $bestW !== null num_format($bestW2) : 'null';

echo 
"Best w ≈ {$bestWFormatted}, loss ≈ {$bestLoss}\n";
echo 
'Predict for 5: ≈ ' . ($bestW);
Result: Memory: 0.007 Mb Time running: 0.001 sec.
Best w ≈ 2, loss ≈ 0.000
Predict for 5: ≈ 10