Skip to main content

fit

fit(
@object ## llll (required)
@xset ## llll/float (required)
@yset null
) -> llll/float/null

Fits (i.e., trains) a Machine Learning model in-place, such as those generated via the mlp, knn, or kmeans functions, to a given dataset or pair of input and output sets. If @object is either a KNN or MLP model, an additional dataset or labelset must be passed to @yset indicating the expected prediction.


Arguments

  • @object [llll]: Object to fit (required)
  • @xset [llll/float]: Input dataset to train model on. (required)
  • @yset [llll/float]: Output dataset or labelset to train model on. Only required when @object is generated via the mlp and knn functions. (default: null).

Output

The kind of returned value will vary based on what @object is:

  • MLP: returns the training loss/error, as a float.
  • KMeans: returns the cluster centroids.

Otherwise, it returns null. [llll/float/null]


Usage

$indata = null;
$outdata = null;
## generate basic dataset based on "less-than" function
for $i in 1...100 do (
$a = rand(0, 1);
$b = rand(0, 1);
$indata _= [$a $b]; ## input point
$outdata _= [$a < $b] ## expected output (0 or 1)
);
$indataset = dataset($indata); ## input points
$outdataset = dataset($outdata); ## expected outputs
$model = mlp(@outputactivation 1); ## create mlp model
## fit (i.e., train) model to learn input/output mappping from dataset
for $i in 1...10 do ( ## repeat training to minimize loss
print(fit($model, $indataset, $outdataset), 'Loss:')
);
writeobject($model, "./mlp.json"); ## write as JSON for future use (optional)
$model = readobject("./mlp.json"); ## read pre-trained model from JSON (optional)
$xpoint = 0.25 0.75; ## sample point
$pred = predict($model, $xpoint); ## generate prediction
print($pred, "prediction:") ## should be (almost) 1.0, as 0.25 < 0.75 is true