cpmct32

nn.js

Feb 12th, 2018
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Flooring function for display on the site (won't clutter up page)
  2. function floorValues(x) {
  3.     return +x.toFixed(0);
  4. }
  5.  
  6. function sigmoid(x) {
  7.     return 1 / (1 + Math.exp(-x));
  8. }
  9.  
  10. function dsigmoid(y) {
  11.     // return sigmoid(x) * (1 - sigmoid(x));
  12.     return y * (1 - y);
  13. }
  14.  
  15. // All of the above functions are used on matrices using the map() method
  16.  
  17. class NeuralNetwork {
  18.  
  19.     constructor(inputNodes, hiddenNodes, outputNodes) {
  20.         this.inputNodes = inputNodes;
  21.         this.hiddenNodes = hiddenNodes;
  22.         this.outputNodes = outputNodes;
  23.  
  24.         this.weights_ih = new Matrix(hiddenNodes, inputNodes);
  25.         this.weights_ho = new Matrix(outputNodes, hiddenNodes);
  26.         this.weights_ih.randomize();
  27.         this.weights_ho.randomize();
  28.  
  29.         this.bias_h = new Matrix(hiddenNodes, 1);
  30.         this.bias_o = new Matrix(outputNodes, 1);
  31.         this.bias_h.randomize();
  32.         this.bias_o.randomize();
  33.     }
  34.  
  35.     // the load method is used for loading a preexisting net using just the weights and biases
  36.     load(weights_ih, weights_ho, bias_h, bias_o){
  37.         this.inputNodes = weights_ih.cols;
  38.         this.hiddenNodes = weights_ih.rows;
  39.         this.outputNodes = weights_ho.rows;
  40.         this.weights_ih = weights_ih;
  41.         this.weights_ho = weights_ho;
  42.         this.bias_h = bias_h;
  43.         this.bias_o = bias_o;
  44.     }
  45.  
  46.     // The feedforward method returns an array of outputs for the given inputs
  47.     // round can also be set to true for rounding the outputs
  48.     feedforward(input_array, round) {
  49.  
  50.         let input = Matrix.fromArray(input_array);
  51.  
  52.         let hidden = Matrix.multiply(this.weights_ih, input);
  53.         hidden.add(this.bias_h);
  54.         hidden.map(sigmoid);
  55.  
  56.         let output = Matrix.multiply(this.weights_ho, hidden);
  57.         output.add(this.bias_o);
  58.         output.map(sigmoid);
  59.         if (round) output.map(floorValues);
  60.  
  61.         return output.toArray();
  62.  
  63.         // zank
  64.     }
  65.  
  66.     train(input_array, target_array) {
  67.  
  68.         // Generating the Hidden Outputs
  69.         let inputs = Matrix.fromArray(input_array);
  70.         let hidden = Matrix.multiply(this.weights_ih, inputs);
  71.         //inputs.print();
  72.         hidden.add(this.bias_h);
  73.         // activation function!
  74.         hidden.map(sigmoid);
  75.  
  76.         let lr = 0.1;
  77.  
  78.         // Generating the output's output!
  79.         let outputs = Matrix.multiply(this.weights_ho, hidden);
  80.         outputs.add(this.bias_o);
  81.         outputs.map(sigmoid);
  82.  
  83.         // Convert array to matrix object
  84.         let targets = Matrix.fromArray(target_array);
  85.  
  86.         // Calculate the error
  87.         // ERROR = TARGETS - OUTPUTS
  88.         let output_errors = Matrix.subtract(targets, outputs);
  89.  
  90.         // let gradient = outputs * (1 - outputs);
  91.         // Calculate gradient
  92.         let gradients = Matrix.map(outputs, dsigmoid);
  93.         gradients.multiply(output_errors);
  94.         gradients.multiply(lr);
  95.  
  96.  
  97.         // Calculate deltas
  98.         let hidden_T = Matrix.transpose(hidden);
  99.         let weight_ho_deltas = Matrix.multiply(gradients, hidden_T);
  100.  
  101.         // Adjust the weights by deltas
  102.         this.weights_ho.add(weight_ho_deltas);
  103.         // Adjust the bias by its deltas (which is just the gradients)
  104.         this.bias_o.add(gradients);
  105.  
  106.         // Calculate the hidden layer errors
  107.         let who_t = Matrix.transpose(this.weights_ho);
  108.         let hidden_errors = Matrix.multiply(who_t, output_errors);
  109.  
  110.         // Calculate hidden gradient
  111.         let hidden_gradient = Matrix.map(hidden, dsigmoid);
  112.         hidden_gradient.multiply(hidden_errors);
  113.         hidden_gradient.multiply(lr);
  114.  
  115.         // Calcuate input->hidden deltas
  116.         let inputs_T = Matrix.transpose(inputs);
  117.         let weight_ih_deltas = Matrix.multiply(hidden_gradient, inputs_T);
  118.  
  119.         this.weights_ih.add(weight_ih_deltas);
  120.         // Adjust the bias by its deltas (which is just the gradients)
  121.         this.bias_h.add(hidden_gradient);
  122.         //hidden_gradient.print();
  123.  
  124.     }
  125. }
Add Comment
Please, Sign In to add comment