Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Flooring function for display on the site (won't clutter up page)
- function floorValues(x) {
- return +x.toFixed(0);
- }
- function sigmoid(x) {
- return 1 / (1 + Math.exp(-x));
- }
- function dsigmoid(y) {
- // return sigmoid(x) * (1 - sigmoid(x));
- return y * (1 - y);
- }
- // All of the above functions are used on matrices using the map() method
- class NeuralNetwork {
- constructor(inputNodes, hiddenNodes, outputNodes) {
- this.inputNodes = inputNodes;
- this.hiddenNodes = hiddenNodes;
- this.outputNodes = outputNodes;
- this.weights_ih = new Matrix(hiddenNodes, inputNodes);
- this.weights_ho = new Matrix(outputNodes, hiddenNodes);
- this.weights_ih.randomize();
- this.weights_ho.randomize();
- this.bias_h = new Matrix(hiddenNodes, 1);
- this.bias_o = new Matrix(outputNodes, 1);
- this.bias_h.randomize();
- this.bias_o.randomize();
- }
- // the load method is used for loading a preexisting net using just the weights and biases
- load(weights_ih, weights_ho, bias_h, bias_o){
- this.inputNodes = weights_ih.cols;
- this.hiddenNodes = weights_ih.rows;
- this.outputNodes = weights_ho.rows;
- this.weights_ih = weights_ih;
- this.weights_ho = weights_ho;
- this.bias_h = bias_h;
- this.bias_o = bias_o;
- }
- // The feedforward method returns an array of outputs for the given inputs
- // round can also be set to true for rounding the outputs
- feedforward(input_array, round) {
- let input = Matrix.fromArray(input_array);
- let hidden = Matrix.multiply(this.weights_ih, input);
- hidden.add(this.bias_h);
- hidden.map(sigmoid);
- let output = Matrix.multiply(this.weights_ho, hidden);
- output.add(this.bias_o);
- output.map(sigmoid);
- if (round) output.map(floorValues);
- return output.toArray();
- // zank
- }
- train(input_array, target_array) {
- // Generating the Hidden Outputs
- let inputs = Matrix.fromArray(input_array);
- let hidden = Matrix.multiply(this.weights_ih, inputs);
- //inputs.print();
- hidden.add(this.bias_h);
- // activation function!
- hidden.map(sigmoid);
- let lr = 0.1;
- // Generating the output's output!
- let outputs = Matrix.multiply(this.weights_ho, hidden);
- outputs.add(this.bias_o);
- outputs.map(sigmoid);
- // Convert array to matrix object
- let targets = Matrix.fromArray(target_array);
- // Calculate the error
- // ERROR = TARGETS - OUTPUTS
- let output_errors = Matrix.subtract(targets, outputs);
- // let gradient = outputs * (1 - outputs);
- // Calculate gradient
- let gradients = Matrix.map(outputs, dsigmoid);
- gradients.multiply(output_errors);
- gradients.multiply(lr);
- // Calculate deltas
- let hidden_T = Matrix.transpose(hidden);
- let weight_ho_deltas = Matrix.multiply(gradients, hidden_T);
- // Adjust the weights by deltas
- this.weights_ho.add(weight_ho_deltas);
- // Adjust the bias by its deltas (which is just the gradients)
- this.bias_o.add(gradients);
- // Calculate the hidden layer errors
- let who_t = Matrix.transpose(this.weights_ho);
- let hidden_errors = Matrix.multiply(who_t, output_errors);
- // Calculate hidden gradient
- let hidden_gradient = Matrix.map(hidden, dsigmoid);
- hidden_gradient.multiply(hidden_errors);
- hidden_gradient.multiply(lr);
- // Calcuate input->hidden deltas
- let inputs_T = Matrix.transpose(inputs);
- let weight_ih_deltas = Matrix.multiply(hidden_gradient, inputs_T);
- this.weights_ih.add(weight_ih_deltas);
- // Adjust the bias by its deltas (which is just the gradients)
- this.bias_h.add(hidden_gradient);
- //hidden_gradient.print();
- }
- }
Add Comment
Please, Sign In to add comment