Overview
Once a model has been created, it can be trained on an array of inputs and an array of corresponding outputs.
Model
Given a single input and output, the backpropagation algorithm can be run on a single datapoint as in the following.
ba.backpropagate(layers, input, output, ba.crossEntropyLoss());
The function requires a loss function to be specified. In this case, the cross entroy loss (defined in the library) is used.
Error of Loss
The total error of the model run against and array of inputs, against of set of outputs can be calculated with the error function.
let error = ba.error(layers, inputs, outputs, ba.crossEntropyLoss());
The loss function used to calculate the total error is required.
Iterating
Typically you will want to run several interations of backpropagation against each input in the training dataset. The following code runs 10,000 iterations against each input in an array of inputs.
for(let i=0;i<10000;i++){
if(i%10 === 0)await $wait();
for(let j=0;j<inputs.length;j++){
let input = inputs[j];
let output = outputs[j];
ba.backpropagate(layers, input, output, ba.crossEntropyLoss());
let error = ba.error(layers, inputs, outputs, ba.crossEntropyLoss());
}
}
This code includes a $wait call, which returns the processor to handle UI events, so that the browser tab does not lock up. It is not necessary when running the code on background thread.