├── LICENSE ├── README.md ├── RedeNeural.js ├── index.html ├── matrix.js └── sketch.js /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 José Bezerra 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # YT-Rede-Neural 2 | Rede neural artificial para o canal O Computeiro 3 | -------------------------------------------------------------------------------- /RedeNeural.js: -------------------------------------------------------------------------------- 1 | function sigmoid(x) { 2 | return 1 / (1 + Math.exp(-x)); 3 | } 4 | 5 | function dsigmoid(x){ 6 | return x * (1-x); 7 | } 8 | 9 | class RedeNeural { 10 | constructor(i_nodes, h_nodes, o_nodes) { 11 | this.i_nodes = i_nodes; 12 | this.h_nodes = h_nodes; 13 | this.o_nodes = o_nodes; 14 | 15 | this.bias_ih = new Matrix(this.h_nodes, 1); 16 | this.bias_ih.randomize(); 17 | this.bias_ho = new Matrix(this.o_nodes, 1); 18 | this.bias_ho.randomize(); 19 | 20 | this.weigths_ih = new Matrix(this.h_nodes, this.i_nodes); 21 | this.weigths_ih.randomize() 22 | 23 | this.weigths_ho = new Matrix(this.o_nodes, this.h_nodes) 24 | this.weigths_ho.randomize() 25 | 26 | this.learning_rate = 0.1; 27 | } 28 | 29 | train(arr,target) { 30 | // INPUT -> HIDDEN 31 | let input = Matrix.arrayToMatrix(arr); 32 | let hidden = Matrix.multiply(this.weigths_ih, input); 33 | hidden = Matrix.add(hidden, this.bias_ih); 34 | 35 | hidden.map(sigmoid) 36 | 37 | // HIDDEN -> OUTPUT 38 | // d(Sigmoid) = Output * (1- Output) 39 | let output = Matrix.multiply(this.weigths_ho, hidden); 40 | output = Matrix.add(output, this.bias_ho); 41 | output.map(sigmoid); 42 | 43 | // BACKPROPAGATION 44 | 45 | // OUTPUT -> HIDDEN 46 | let expected = Matrix.arrayToMatrix(target); 47 | let output_error = Matrix.subtract(expected,output); 48 | let d_output = Matrix.map(output,dsigmoid); 49 | let hidden_T = Matrix.transpose(hidden); 50 | 51 | let gradient = Matrix.hadamard(d_output,output_error); 52 | gradient = Matrix.escalar_multiply(gradient,this.learning_rate); 53 | 54 | // Adjust Bias O->H 55 | this.bias_ho = Matrix.add(this.bias_ho, gradient); 56 | // Adjust Weigths O->H 57 | let weigths_ho_deltas = Matrix.multiply(gradient,hidden_T); 58 | this.weigths_ho = Matrix.add(this.weigths_ho,weigths_ho_deltas); 59 | 60 | // HIDDEN -> INPUT 61 | let weigths_ho_T = Matrix.transpose(this.weigths_ho); 62 | let hidden_error = Matrix.multiply(weigths_ho_T,output_error); 63 | let d_hidden = Matrix.map(hidden,dsigmoid); 64 | let input_T = Matrix.transpose(input); 65 | 66 | let gradient_H = Matrix.hadamard(d_hidden,hidden_error); 67 | gradient_H = Matrix.escalar_multiply(gradient_H, this.learning_rate); 68 | 69 | // Adjust Bias O->H 70 | this.bias_ih = Matrix.add(this.bias_ih, gradient_H); 71 | // Adjust Weigths H->I 72 | let weigths_ih_deltas = Matrix.multiply(gradient_H, input_T); 73 | this.weigths_ih = Matrix.add(this.weigths_ih, weigths_ih_deltas); 74 | } 75 | 76 | predict(arr){ 77 | // INPUT -> HIDDEN 78 | let input = Matrix.arrayToMatrix(arr); 79 | 80 | let hidden = Matrix.multiply(this.weigths_ih, input); 81 | hidden = Matrix.add(hidden, this.bias_ih); 82 | 83 | hidden.map(sigmoid) 84 | 85 | // HIDDEN -> OUTPUT 86 | let output = Matrix.multiply(this.weigths_ho, hidden); 87 | output = Matrix.add(output, this.bias_ho); 88 | output.map(sigmoid); 89 | output = Matrix.MatrixToArray(output); 90 | 91 | return output; 92 | } 93 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /matrix.js: -------------------------------------------------------------------------------- 1 | class Matrix { 2 | constructor(rows, cols) { 3 | this.rows = rows; 4 | this.cols = cols; 5 | 6 | this.data = []; 7 | 8 | for (let i = 0; i < rows; i++) { 9 | let arr = [] 10 | for (let j = 0; j < cols; j++) { 11 | arr.push(0) 12 | } 13 | this.data.push(arr); 14 | } 15 | } 16 | 17 | // Funções Diversas 18 | 19 | static arrayToMatrix(arr) { 20 | let matrix = new Matrix(arr.length, 1); 21 | matrix.map((elm, i, j) => { 22 | return arr[i]; 23 | }) 24 | return matrix; 25 | } 26 | 27 | static MatrixToArray(obj) { 28 | let arr = [] 29 | obj.map((elm, i, j) => { 30 | arr.push(elm); 31 | }) 32 | return arr; 33 | } 34 | 35 | 36 | print() { 37 | console.table(this.data); 38 | } 39 | 40 | randomize() { 41 | this.map((elm, i, j) => { 42 | return Math.random() * 2 - 1; 43 | }); 44 | } 45 | 46 | static map(A, func) { 47 | let matrix = new Matrix(A.rows, A.cols); 48 | 49 | matrix.data = A.data.map((arr, i) => { 50 | return arr.map((num, j) => { 51 | return func(num, i, j); 52 | }) 53 | }) 54 | 55 | return matrix; 56 | } 57 | 58 | map(func) { 59 | 60 | this.data = this.data.map((arr, i) => { 61 | return arr.map((num, j) => { 62 | return func(num, i, j); 63 | }) 64 | }) 65 | 66 | return this; 67 | } 68 | 69 | static transpose(A){ 70 | var matrix = new Matrix(A.cols, A.rows); 71 | matrix.map((num,i,j) => { 72 | return A.data[j][i]; 73 | }); 74 | return matrix; 75 | } 76 | 77 | // Operações Estáticas Matriz x Escalar 78 | 79 | static escalar_multiply(A, escalar) { 80 | var matrix = new Matrix(A.rows, A.cols); 81 | 82 | matrix.map((num, i, j) => { 83 | return A.data[i][j] * escalar; 84 | }); 85 | 86 | return matrix; 87 | } 88 | 89 | // Operações Estáticas Matriz x Matriz 90 | 91 | static hadamard(A, B) { 92 | var matrix = new Matrix(A.rows, A.cols); 93 | 94 | matrix.map((num, i, j) => { 95 | return A.data[i][j] * B.data[i][j] 96 | }); 97 | 98 | return matrix; 99 | } 100 | 101 | static add(A, B) { 102 | var matrix = new Matrix(A.rows, A.cols); 103 | 104 | matrix.map((num, i, j) => { 105 | return A.data[i][j] + B.data[i][j] 106 | }); 107 | 108 | return matrix; 109 | } 110 | 111 | static subtract(A, B) { 112 | var matrix = new Matrix(A.rows, A.cols); 113 | 114 | matrix.map((num, i, j) => { 115 | return A.data[i][j] - B.data[i][j] 116 | }); 117 | 118 | return matrix; 119 | } 120 | 121 | static multiply(A, B) { 122 | var matrix = new Matrix(A.rows, B.cols); 123 | 124 | matrix.map((num, i, j) => { 125 | let sum = 0 126 | for (let k = 0; k < A.cols; k++) { 127 | let elm1 = A.data[i][k]; 128 | let elm2 = B.data[k][j]; 129 | sum += elm1 * elm2; 130 | } 131 | 132 | return sum; 133 | }) 134 | 135 | return matrix; 136 | } 137 | } -------------------------------------------------------------------------------- /sketch.js: -------------------------------------------------------------------------------- 1 | var train = true; 2 | 3 | function setup() { 4 | createCanvas(500, 500); 5 | background(0); 6 | 7 | nn = new RedeNeural(2, 3, 1); 8 | 9 | // XOR Problem 10 | dataset = { 11 | inputs: 12 | [[1, 1], 13 | [1, 0], 14 | [0, 1], 15 | [0, 0]], 16 | outputs: 17 | [[0], 18 | [1], 19 | [1], 20 | [0]] 21 | } 22 | } 23 | 24 | function draw() { 25 | if (train) { 26 | for (var i = 0; i < 10000; i++) { 27 | var index = floor(random(4)); 28 | nn.train(dataset.inputs[index], dataset.outputs[index]); 29 | } 30 | if (nn.predict([0, 0])[0] < 0.04 && nn.predict([1, 0])[0] > 0.98) { 31 | train = false; 32 | console.log("terminou"); 33 | } 34 | } 35 | } 36 | --------------------------------------------------------------------------------