<commit_before>/**
 * HAL 9000 Neural Network Beta v.01
 * Written By: <NAME>
 * Email: <EMAIL>
 * Released under MIT License 
 * 
 * This is the brain for HAL. It contains the logic to create the neural network
 */

//creates NN obj
var NeuralNetwork = function(name) {
    this.name = name || "NN";
    this.layers = {
        "inputLayer": {},
        "hiddenLayer": [],
        "outputLayer": []
    };
    this.targets = [];
};

//creates new layer
NeuralNetwork.prototype.createLayer = function() {

    var num = arguments[0];
    var attr = arguments[1];

    if (typeof num !== "number") {
        attr = arguments[0];
        num = 1;
    }

    try {
        num = (attr.type === "output") ? 1 : num;
    } catch (e) {
        alert("Please provide an attribute type when creating layers: output or hidden");
        return false;
    }
    //ensures output layer count is always 1


    for (let i = 0; i < num; i++) {
        switch (attr.type) {
            case "hidden":
                var layerID;
                var layerCount = this.layers.hiddenLayer.length;
                layerID = (layerCount < 1) ? "A" : utilities.toLetters(layerCount + 1);

                if (attr.neurons.length > 0) {
                    for (let i = 0; i < attr.neurons.length; i++) {
                        attr.neurons[i].id = layerID + (i + 1).toString();
                    }
                }

                this.layers.hiddenLayer.push({
                    name: attr.name || "",
                    layerID: layerID,
                    type: attr.type,
                    neurons: attr.neurons,
                });
                break;
            case "output":
                for (let i = 0; i < attr.neurons.length; i++) {
                    attr.neurons[i].id = "Output_" + (i + 1);
                }

                this.layers.outputLayer = {
                        type: attr.type,
                        neurons: attr.neurons,
                    }
                    //Ensures output layer only ever has one layer
                return;
            default:
                alert("Please specify layer type: 'output' or 'hidden'");
                return false;
        }
    }

};

//sets training targets
NeuralNetwork.prototype.setTarget = function(targets) {
    this.targets = targets;
};


//Creates a JSON obj with all initial weights and biases in NN
NeuralNetwork.prototype.init = function(numInputs) {
    var num;
    var hArr = this.layers.hiddenLayer;
    var oArr = this.layers.outputLayer;
    var iArr = this.layers.inputLayer;
    var valArr = [];

    //adding inputs to input layer
    iArr.inputs = numInputs;

    //seeding random wb data in hidden layer neurons
    for (let i = 0; i < hArr.length; i++) { //seeding random wb data in hidden layer neurons
        for (let j = 0; j < hArr[i].neurons.length; j++) {
            valArr = randomValues(numInputs);
            hArr[i].neurons[j].wb = { "w": valArr, "b": NeuralMathLib.randomGauss() };
        }
    }

    var numLayers = hArr.length;
    var numLastInputs = hArr[numLayers - 1].neurons.length;
    //seeding random wb data in output layer neurons
    for (let i = 0; i < oArr.neurons.length; i++) {
        valArr = randomValues(numLastInputs);
        oArr.neurons[i].wb = { "w": valArr, "b": NeuralMathLib.randomGauss() };
    }
};

// NeuralNetwork.prototype.initTEST = function (numInputs) {
//     var num;
//     var hArr = this.layers.hiddenLayer;
//     var oArr = this.layers.outputLayer;
//     var iArr = this.layers.inputLayer;

//     //seeding random wb data in hidden layer neurons
//     hArr[0].neurons[0].wb = { "w": [.15,.20], "b": .35 };
//     hArr[0].neurons[1].wb =  { "w": [.25,.30], "b": .35 };
//     oArr.neurons[0].wb = { "w": [.40,.45], "b": 0.60 };
//     oArr.neurons[1].wb = { "w": [.50,.55], "b": 0.60 };
// };

NeuralNetwork.prototype.errors = function(globalError, globalFinal, predictionArray) {
    this.globalError = globalError; //All Errors from each training cycle
    this.globalFinal = globalFinal; //Absolute final error 
    this.predictionArray = predictionArray; //Prediction arrays from each cycle
}

var Train = function(NN, inputs, rate, iter, error) {
    var errArr = [];
    var tError = [];
    if (typeof iter !== "number") {
        iter = 1;
    }
    if (error !== true || error !== false) {
        error = true;
    }
    for (let loop = 0; loop < iter; loop++) {
        //console.log("Initial Inputs: \n" + inputs + "\n");
        var newInputs = [];
        var activatedOutputArr = [];
        var activatedHiddenArr = [];
        var sumsOFAllLayers = [];
        var activatedVal, id, wArr, neuron, summed;

        /////// FEED FORWARD
        //
        for (let i = 0; i < NN.layers.hiddenLayer.length; i++) { //Calculate Activated Hidden Layer Outputs
            var neuronHidArr = NN.layers.hiddenLayer[i].neurons;
            var activatedHiddenObj = NeuralMathLib.activatedLayer(neuronHidArr, inputs, "hidden");
            activatedHiddenArr.push(activatedHiddenObj.activated); //inputs for next layer
            sumsOFAllLayers.push(activatedHiddenObj); //storing activated inputs & sums from layers for later
        }

        //Calculate Activated Output Layers
        lastLayerInputs = activatedHiddenArr[activatedHiddenArr.length - 1]; //Last layer of hidden network
        var neuronOutArr = NN.layers.outputLayer.neurons;
        var activatedOutputObj = NeuralMathLib.activatedLayer(neuronOutArr, lastLayerInputs, "output");

        activatedOutputArr = activatedOutputObj.activated;
        //console.log("Activated Output Layer Array: \n[" + activatedOutputArr + "]\n");

        /////// BACKPROPAGATION
        //
        var errors = LossFunction(activatedOutputArr, NN.targets); //Error Calculation of the Output Layer
        var totalError = errors.reduce(function(previousValue, currentValue, currentIndex, array) {
            return previousValue + currentValue;
        });

        if (error) {
            tError.push(totalError);
            errArr.push(activatedOutputArr._data);
            NN.errors(tError, totalError, errArr);
        }<commit_msg>Create HAL9000.js<commit_after>/**
 * HAL 9000 Neural Network Beta v.01
 * Written By: <NAME>
 * Email: <EMAIL>
 * Released under MIT License 
 * 
 * This is the brain for HAL. It contains the logic to create the neural network
 */

//creates NN obj
var NeuralNetwork = function(name) {
    this.name = name || "NN";
    this.layers = {
        "inputLayer": {},
        "hiddenLayer": [],
        "outputLayer": []
    };
    this.targets = [];
};

//creates new layer
NeuralNetwork.prototype.createLayer = function() {

    var num = arguments[0];
    var attr = arguments[1];

    if (typeof num !== "number") {
        attr = arguments[0];
        num = 1;
    }

    try {
        num = (attr.type === "output") ? 1 : num;
    } catch (e) {
        alert("Please provide an attribute type when creating layers: output or hidden");
        return false;
    }
    //ensures output layer count is always 1


    for (let i = 0; i < num; i++) {
        switch (attr.type) {
            case "hidden":
                var layerID;
                var layerCount = this.layers.hiddenLayer.length;
                layerID = (layerCount < 1) ? "A" : utilities.toLetters(layerCount + 1);

                if (attr.neurons.length > 0) {
                    for (let i = 0; i < attr.neurons.length; i++) {
                        attr.neurons[i].id = layerID + (i + 1).toString();
                    }
                }

                this.layers.hiddenLayer.push({
                    name: attr.name || "",
                    layerID: layerID,
                    type: attr.type,
                    neurons: attr.neurons,
                });
                break;
            case "output":
                for (let i = 0; i < attr.neurons.length; i++) {
                    attr.neurons[i].id = "Output_" + (i + 1);
                }

                this.layers.outputLayer = {
                        type: attr.type,
                        neurons: attr.neurons,
                    }
                    //Ensures output layer only ever has one layer
                return;
            default:
                alert("Please specify layer type: 'output' or 'hidden'");
                return false;
        }
    }

};

//sets training targets
NeuralNetwork.prototype.setTarget = function(targets) {
    this.targets = targets;
};


//Creates a JSON obj with all initial weights and biases in NN
NeuralNetwork.prototype.init = function(numInputs) {
    var num;
    var hArr = this.layers.hiddenLayer;
    var oArr = this.layers.outputLayer;
    var iArr = this.layers.inputLayer;
    var valArr = [];

    //adding inputs to input layer
    iArr.inputs = numInputs;

    //seeding random wb data in hidden layer neurons
    for (let i = 0; i < hArr.length; i++) { //seeding random wb data in hidden layer neurons
        for (let j = 0; j < hArr[i].neurons.length; j++) {
            valArr = randomValues(numInputs);
            hArr[i].neurons[j].wb = { "w": valArr, "b": NeuralMathLib.randomGauss() };
        }
    }

    var numLayers = hArr.length;
    var numLastInputs = hArr[numLayers - 1].neurons.length;
    //seeding random wb data in output layer neurons
    for (let i = 0; i < oArr.neurons.length; i++) {
        valArr = randomValues(numLastInputs);
        oArr.neurons[i].wb = { "w": valArr, "b": NeuralMathLib.randomGauss() };
    }
};

// NeuralNetwork.prototype.initTEST = function (numInputs) {
//     var num;
//     var hArr = this.layers.hiddenLayer;
//     var oArr = this.layers.outputLayer;
//     var iArr = this.layers.inputLayer;

//     //seeding random wb data in hidden layer neurons
//     hArr[0].neurons[0].wb = { "w": [.15,.20], "b": .35 };
//     hArr[0].neurons[1].wb =  { "w": [.25,.30], "b": .35 };
//     oArr.neurons[0].wb = { "w": [.40,.45], "b": 0.60 };
//     oArr.neurons[1].wb = { "w": [.50,.55], "b": 0.60 };
// };

NeuralNetwork.prototype.errors = function(globalError, globalFinal, predictionArray) {
    this.globalError = globalError; //All Errors from each training cycle
    this.globalFinal = globalFinal; //Absolute final error 
    this.predictionArray = predictionArray; //Prediction arrays from each cycle
}

var Train = function(NN, inputs, rate, iter, error) {
    var errArr = [];
    var tError = [];
    if (typeof iter !== "number") {
        iter = 1;
    }
    if (error !== true || error !== false) {
        error = true;
    }
    for (let loop = 0; loop < iter; loop++) {
        //console.log("Initial Inputs: \n" + inputs + "\n");
        var newInputs = [];
        var activatedOutputArr = [];
        var activatedHiddenArr = [];
        var sumsOFAllLayers = [];
        var activatedVal, id, wArr, neuron, summed;

        /////// FEED FORWARD
        //
        for (let i = 0; i < NN.layers.hiddenLayer.length; i++) { //Calculate Activated Hidden Layer Outputs
            var neuronHidArr = NN.layers.hiddenLayer[i].neurons;
            var activatedHiddenObj = NeuralMathLib.activatedLayer(neuronHidArr, inputs, "hidden");
            activatedHiddenArr.push(activatedHiddenObj.activated); //inputs for next layer
            sumsOFAllLayers.push(activatedHiddenObj); //storing activated inputs & sums from layers for later
        }

        //Calculate Activated Output Layers
        var lastLayerInputs = activatedHiddenArr[activatedHiddenArr.length - 1]; //Last layer of hidden network
        var neuronOutArr = NN.layers.outputLayer.neurons;
        var activatedOutputObj = NeuralMathLib.activatedLayer(neuronOutArr, lastLayerInputs, "output");

        activatedOutputArr = activatedOutputObj.activated;
        //console.log("Activated Output Layer Array: \n[" + activatedOutputArr + "]\n");

        /////// BACKPROPAGATION
        //
        var errors = LossFunction(activatedOutputArr, NN.targets); //Error Calculation of the Output Layer
        var totalError = errors.reduce(function(previousValue, currentValue, currentIndex, array) {
            return previousValue + currentValue;
        });

        if (error) {
            tError.push(totalError);
            errArr.push(activatedOutputArr._data);
            NN.errors(tError, totalError, errArr);
        }
{'fraction_non_alphanumeric': 0.13042422279792745, 'fraction_numerical': 0.010038860103626942, 'mean_word_length': 1.9766265060240964, 'pattern_counts': {'":': 34, '<': 25, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3896459', 'n_tokens_mistral': 3809, 'n_tokens_neox': 3543, 'n_words': 982}