Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SatvikVejendla committed Feb 20, 2021
1 parent e707683 commit c5934c3
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
30 changes: 30 additions & 0 deletions examples/XOR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const NeuralNetwork = require("neural-network-node");

let nn = new NeuralNetwork(2, 4, 1);

let training_data = [
{
inputs: [0, 0],
outputs: [0],
},
{
inputs: [0, 1],
outputs: [1],
},
{
inputs: [1, 0],
outputs: [1],
},
{
inputs: [1, 1],
outputs: [0],
},
];

for (let i = 0; i < 1000; i++) {
let data = random(training_data);
nn.train(data.inputs, data.outputs);
}

const output = nn.predict([0, 1]); //The more you train the model, the closer this gets to one
console.log(output);
23 changes: 23 additions & 0 deletions examples/sinwave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const NeuralNetwork = require("neural-network-node");

let nn = new NeuralNetwork(1, 2, 1);

function generatedata() {
let input = Math.random() * 2 * Math.PI;
let output = Math.sin(input);

return {
input: [input],
output: [output],
};
}

for (let i = 0; i < 10000; i++) {
var data = generatedata();
nn.train(data.input, data.output);
}

console.log("Finished training");

const output = nn.predict(Math.PI); //The more you train the model, the closer this gets to zero
console.log(output);
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "neural-network-node",
"version": "1.0.4",
"version": "1.1.0",
"description": "A simple 2 layer neural network coded from scratch with no external modules. Lets you train your own neural network in a simple and easy to learn way.",
"main": "src/index.js",
"scripts": {
Expand All @@ -13,8 +13,18 @@
"node",
"javascript",
"machine",
"learning"
"learning",
"supervised",
"data"
],
"author": "SatvikVejendla",
"license": "ISC"
"license": "ISC",
"repository": {
"type": "git",
"url": "git+https://github.com/SatvikVejendla/Neural-Network-Node.git"
},
"bugs": {
"url": "https://github.com/SatvikVejendla/Neural-Network-Node/issues"
},
"homepage": "https://github.com/SatvikVejendla/Neural-Network-Node#readme"
}
107 changes: 65 additions & 42 deletions utils/NeuralNetwork.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ class NeuralNetwork {
this.learning_rate = 0.1;
}

getLearningRate() {
return this.learning_rate;
}

setLearningRate(x = 0.1) {
this.learning_rate = learning_rate;
this.learning_rate = x;
}

setWeights(x) {
Expand Down Expand Up @@ -48,7 +52,13 @@ class NeuralNetwork {
}
}

feedforward(input_array) {
predict(input_array) {
if (input_array.length != this.input_nodes.length) {
const err =
"ERROR! The predict function input array does not match the length of the neural network's input nodes.";
console.log(err);
return err;
}
let inputs = Matrix.fromArray(input_array);

//input to hidden
Expand All @@ -68,46 +78,59 @@ class NeuralNetwork {
}

train(input_array, target_array) {
let inputs = Matrix.fromArray(input_array);

//input to hidden
let hidden = Matrix.multiply(this.weights_ih, inputs);
hidden.add(this.bias_h);

//activation
hidden.map(Sigmoid.sigmoid);

//hidden to output
let outputs = Matrix.multiply(this.weights_ho, hidden);
outputs.add(this.bias_o);
outputs.map(Sigmoid.sigmoid);

let targets = Matrix.fromArray(target_array);
//error calculation
let output_errors = Matrix.subtract(targets, outputs);
let gradients = Matrix.map(outputs, Sigmoid.dsigmoid);
gradients.multiply(output_errors);
gradients.multiply(this.learning_rate);

let hidden_T = Matrix.transpose(hidden);
let weight_ho_deltas = Matrix.multiply(gradients, hidden_T);

//Weights and Biases adjustments
this.weights_ho.add(weight_ho_deltas);
this.bias_o.add(gradients);

//hidden layer errors
let who_t = Matrix.transpose(this.weights_ho);
let hidden_errors = Matrix.multiply(who_t, output_errors);
let hidden_gradient = Matrix.map(hidden, Sigmoid.dsigmoid);
hidden_gradient.multiply(hidden_errors);
hidden_gradient.multiply(this.learning_rate);

let inputs_T = Matrix.transpose(inputs);
let weight_ih_deltas = Matrix.multiply(hidden_gradient, inputs_T);

this.weights_ih.add(weight_ih_deltas);
this.bias_h.add(hidden_gradient);
if (input_array.length != this.input_nodes.length) {
console.log(input_array.length + ": " + this.input_array.length);
const err =
"ERROR! The train function input array does not match the length of the neural network's input nodes.";
console.log(err);
return err;
} else if (target_array.length != this.output_nodes.length) {
const err =
"ERROR! The train function target output array does not match the length of the neural network's output nodes.";
console.log(err);
return err;
} else {
let inputs = Matrix.fromArray(input_array);

//input to hidden
let hidden = Matrix.multiply(this.weights_ih, inputs);
hidden.add(this.bias_h);

//activation
hidden.map(Sigmoid.sigmoid);

//hidden to output
let outputs = Matrix.multiply(this.weights_ho, hidden);
outputs.add(this.bias_o);
outputs.map(Sigmoid.sigmoid);

let targets = Matrix.fromArray(target_array);
//error calculation
let output_errors = Matrix.subtract(targets, outputs);
let gradients = Matrix.map(outputs, Sigmoid.dsigmoid);
gradients.multiply(output_errors);
gradients.multiply(this.learning_rate);

let hidden_T = Matrix.transpose(hidden);
let weight_ho_deltas = Matrix.multiply(gradients, hidden_T);

//Weights and Biases adjustments
this.weights_ho.add(weight_ho_deltas);
this.bias_o.add(gradients);

//hidden layer errors
let who_t = Matrix.transpose(this.weights_ho);
let hidden_errors = Matrix.multiply(who_t, output_errors);
let hidden_gradient = Matrix.map(hidden, Sigmoid.dsigmoid);
hidden_gradient.multiply(hidden_errors);
hidden_gradient.multiply(this.learning_rate);

let inputs_T = Matrix.transpose(inputs);
let weight_ih_deltas = Matrix.multiply(hidden_gradient, inputs_T);

this.weights_ih.add(weight_ih_deltas);
this.bias_h.add(hidden_gradient);
}
}
}

Expand Down

0 comments on commit c5934c3

Please sign in to comment.