Error, loss functions, and why they are needed
Case 5. Training a model as minimizing error
Implementation in RubixML
Next we do the same with Rubix ML: the library finds the best parameter (weights) that minimizes the error (for linear regression it does so analytically via the least squares method).
Example of use
<?php
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\Datasets\Unlabeled;
use Rubix\ML\Regressors\Ridge;
// Training data for a simple relationship y = 2x.
$samples = [[1], [2], [3], [4]];
$labels = [2, 4, 6, 8];
// A labeled dataset pairs each sample with its target value.
$dataset = new Labeled($samples, $labels);
// Ridge is linear regression with L2 regularization.
// With a tiny alpha (1e-6) it behaves almost like ordinary least squares.
$model = new Ridge(1e-6);
// Train = fit the best parameters (weights) that minimize error.
$model->train($dataset);
$predictions = $model->predict(new Unlabeled([[5]]));
$coefficients = $model->coefficients();
$bestW = number_format($coefficients[0], 2, '.', '');
$bestLoss = number_format($model->bias(), 3, '.', '');
echo "Best w ≈ {$bestW}, loss ≈ {$bestLoss}\n";
echo 'Predict for 5: ≈ ' . number_format($predictions[0], 7, '.', '');
Result:
Memory: 0.994 Mb
Time running: 0.016 sec.
Best w ≈ 2.00, loss ≈ 0.000
Predict for 5: ≈ 9.9999990