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.
Example of use
<?php
require_once __DIR__ . '/code.php';
$x = [1, 2, 3, 4];
$y = [2, 4, 6, 8];
$result = findBestW($x, $y);
$bestW = $result['bestW'];
$bestLoss = num_format($result['bestLoss']);
$bestWFormatted = $bestW !== null ? num_format($bestW, 2) : 'null';
echo "Best w ≈ {$bestWFormatted}, loss ≈ {$bestLoss}\n";
echo 'Predict for 5: ≈ ' . (5 * $bestW);
Result:
Memory: 0.007 Mb
Time running: 0.001 sec.
Best w ≈ 2, loss ≈ 0.000
Predict for 5: ≈ 10