├── test ├── index.html └── test.js ├── LICENSE ├── README.md └── lib ├── matrix.js └── mini-ann.js /test/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | mini-ANN-test 5 | 6 | 7 | 8 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | function AND_gate_test() { 2 | console.log("AND GATE TEST : "); 3 | 4 | let mynn = new NeuralNetwork([2,1]); 5 | 6 | let training_data = [ 7 | [[1,1], [1]], 8 | [[1,0], [0]], 9 | [[0,1], [0]], 10 | [[0,0], [0]], 11 | ]; 12 | 13 | console.log("Before training..."); 14 | console.log('0 AND 0',mynn.feedforward([0,0])); 15 | console.log('0 AND 1',mynn.feedforward([0,1])); 16 | console.log('1 AND 0',mynn.feedforward([1,0])); 17 | console.log('1 AND 1',mynn.feedforward([1,1])); 18 | 19 | // several training epochs 20 | for(let i=0; i<10000; i++) { 21 | let tdata = training_data[Math.floor(Math.random() * training_data.length)]; 22 | mynn.train(tdata[0], tdata[1]); 23 | } 24 | 25 | console.log("After training..."); 26 | console.log('0 AND 0',mynn.feedforward([0,0])); 27 | console.log('0 AND 1',mynn.feedforward([0,1])); 28 | console.log('1 AND 0',mynn.feedforward([1,0])); 29 | console.log('1 AND 1',mynn.feedforward([1,1])); 30 | } 31 | 32 | AND_gate_test(); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Siddharth Maurya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![mini-ann](https://user-images.githubusercontent.com/12862695/65754632-15627f80-e12f-11e9-8333-5fa8112a6af1.png) 2 | 3 | ## A lightweight Neural Network library in Javascript 4 | 5 | This library is inspired by [Toy-Neural-Network](https://github.com/CodingTrain/Toy-Neural-Network-JS), which works for one hidden layer. *mini-ANN-js* provides basic ANN functionalities which includes ability to create multilayer architecture, feed forward, training through backpropagation and some functions for genetic algorithm. 6 | 7 | ### Documentation 8 | 9 | * **Initialize** Neural Network 10 | 11 | ```javascript 12 | // ANN with 4 inputs, 3 neurons in hidden layer and 2 outputs 13 | const my_ann = new NeuralNetwork([4, 3, 2]); 14 | // initializes ANN with random weights and biases 15 | ``` 16 | 17 | * Changing **Activation function** 18 | 19 | ```javascript 20 | // set ReLU function as activation function 21 | my_ann.setActivation(NeuralNetwork.ReLU); 22 | 23 | // set Sigmoid function as activation function 24 | my_ann.setActivation(NeuralNetwork.SIGMOID); 25 | 26 | // By default Sigmoid is the activation function 27 | ``` 28 | 29 | * Performing **feed forward** 30 | 31 | ```javascript 32 | // passing 4 inputs as follows... 33 | const output = my_ann.feedforward([0, 2, 1, 2]); 34 | // returns an array with outputs of ANN 35 | 36 | // pass 2nd arg for feedforward as true to get all layers instead of just output 37 | const all_layers = my_ann.feedforward([0, 2, 1, 2], true); 38 | 39 | ``` 40 | 41 | * **Training** ANN 42 | 43 | ```javascript 44 | let input = [0, 2, 1, 2]; 45 | let expected_output = [1,0]; 46 | my_ann.train(input, expected_output); 47 | ``` 48 | 49 | * Functions for **Genetic algorithms** 50 | 51 | ```javascript 52 | // mutate weights and biases of ANN 53 | my_ann.mutate(0.2); //mutation rate = 0.2 (min-0 & max-1) 54 | 55 | // creates a copy of ann 56 | new_ann = my_ann.copy() 57 | 58 | // crossover 59 | const offspring = my_ann.crossover(other_ann); 60 | ``` 61 | 62 | * **Visualization** function 63 | 64 | ```javascript 65 | const canvas = document.getElementById("canvas") 66 | const ctx = canvas.getContext("2d") 67 | const nn = new NeuralNetwork([3,5,3,2]) 68 | nn.draw( 69 | ctx, // canvas context 70 | 100, // top left x coordinate to start drawing from 71 | 100, // top left y coordinate to start drawing from 72 | [-0.5, 0.75, 0.2], // OPTIONAL network inputs. When given also visualizes activation states 73 | 20 // OPTIONAL neuron radius. Default = 20 74 | ) 75 | ``` 76 | 77 | Example output: 78 | 79 | ![example output of draw function](https://user-images.githubusercontent.com/543577/144102996-ce1a7e90-ed12-4134-af05-70d46db34ce0.png) -------------------------------------------------------------------------------- /lib/matrix.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Matrix library based on - https://github.com/CodingTrain/Toy-Neural-Network-JS/blob/master/lib/matrix.js 3 | */ 4 | class Matrix { 5 | /** 6 | * Values are initialized to 0 7 | * @param {number} Rows 8 | * @param {number} Columns 9 | */ 10 | constructor(rows,cols) { 11 | this.rows = rows; 12 | this.cols = cols; 13 | this.data = []; 14 | 15 | for(let i = 0 ; i < this.rows ; i++) { 16 | this.data[i] = []; 17 | for (let j=0; j < this.cols ; j++) { 18 | this.data[i][j] = 0; 19 | } 20 | } 21 | } 22 | 23 | multiply(n) { 24 | if(n instanceof Matrix) { //hadamard multiplication (corresponding element multiplies to this matrix) 25 | if(n.rows == this.rows && n.cols == this.cols) { 26 | for(let i=0; i < this.rows ; i++) { 27 | for(let j=0; j < this.cols ; j++) { 28 | this.data[i][j] *= n.data[i][j]; 29 | } 30 | } 31 | } 32 | else { 33 | console.error("element wise multiplication failed because of size mismatch!"); 34 | return -1; //failed bcoz of size mismatch 35 | } 36 | } 37 | else { //scalar multiply 38 | for (let i = 0; i < this.rows; i++) { 39 | for(let j=0; j