Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Adding custom loss functions

Matias Vazquez-Levi edited this page Feb 14, 2021 · 5 revisions

Back to Home

Loss functions are stored in an Object value named lossfuncs.
For nodejs require the object like so:

const lossfuncs = require('dannjs').lossfuncs;

To add your own function, add a new object property with the function you want to add. The name of the property is the name the Dann model is going to use to refer to the function.

lossfuncs.name = function;

Here's an example with the pre-existing 'mse' loss function:

const dn = require('dannjs');     //nodejs only
const lossfuncs = dn.lossfuncs;   //nodejs only

lossfuncs.mse = function mse(predictions,target) {
    let sum = 0;
    let n = target.length;

    for (let i = 0; i < n; i++) {
        let y = target[i];
        let yHat = predictions[i];
        sum += Math.pow(y - yHat,2);
    }

    return sum/n;
}

Function arguments

A standard loss function takes two arguments to evaluate the resulting loss of the model.

  • predictions
    The predicted output of the model as an array.

  • target
    The desired output of the model as an array.

  • returns
    A loss value.


Clone this wiki locally