Javascript Implementation of Error

Overview


The error function (loss function) of a neural network is used to gauge how close the networks outputs are to the true outputs on a given dataset. Just like a network layer, it takes a set of inputs and produces an output.

The error function also needs to produce an input gradient, as it is used in the backpropagation algorithm to propagate the errors back to each layer.

Layer Implementation



export function meanSquaredError(){
    let layer = {
        type:'meanSquaredError',
        evaluate:function(val1, val2){
            let diff = la.subtract(val1, val2);
            let result = la.multiply(la.transpose(diff), diff);
            result[0][0] = 0.5 * result[0][0];
            return result;
        },
        inputGradient:function(input, output){
            let result = [];
            for(let i=0;i<input.length;i++){
                result.push([input[i][0] - output[i][0]]);
            }
            return result;
        }
    };
    return layer;
}