Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple NN test #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/nn.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// Other techniques for learning
if (typeof(require) !== 'undefined') {
Matrix = require('./matrix');
}

class ActivationFunction{
constructor(func, dfunc){
class ActivationFunction {
constructor(func, dfunc) {
this.func = func;
this.dfunc = dfunc;
}
}

let sigmoid = new ActivationFunction(
x => 1 / (1 + Math.exp(-x)),
y => y * (1- y)
y => y * (1 - y)
);

let tanh = new ActivationFunction(
x => Math.tanh(x),
y => 1-(y*y)
y => 1 - (y * y)
);


Expand Down Expand Up @@ -128,8 +130,7 @@ class NeuralNetwork {
}

static deserialize(data) {
if(typeof data == 'string')
{
if (typeof data == 'string') {
data = JSON.parse(data);
}
let nn = new NeuralNetwork(data.input_nodes, data.hidden_nodes, data.output_nodes);
Expand All @@ -142,3 +143,12 @@ class NeuralNetwork {
}

}

if (typeof module !== 'undefined') {
module.exports = {
NeuralNetwork: NeuralNetwork,
ActivationFunction: ActivationFunction,
sigmoid: sigmoid,
tanh: tanh,
};
}
37 changes: 37 additions & 0 deletions lib/nn.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//const Matrix = require('./matrix');
const {
NeuralNetwork,
} = require('./nn');

test('XOR Feed forward 100 times (NN [2,4,1], lr = 0.1)', () => {
const defaultNN = '{"input_nodes":2,"hidden_nodes":4,"output_nodes":1,"weights_ih":{"rows":4,"cols":2,"data":[[-0.67573921323278,-0.6631309040105413],[0.12043373460672324,0.9164706630341213],[0.8065050836856171,0.6200864007638192],[0.17026959573001887,0.8558180147448349]]},"weights_ho":{"rows":1,"cols":4,"data":[[0.25208907823805715,-0.15513622033337615,-0.7593404409594813,-0.2200931714640979]]},"bias_h":{"rows":4,"cols":1,"data":[[-0.35555433502368805],[0.5915246859082983],[0.7931155122071059],[-0.5196051276393758]]},"bias_o":{"rows":1,"cols":1,"data":[[0.16280502479816095]]},"learning_rate":0.1,"activation_function":{}}';

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

const targetNN = '{"input_nodes":2,"hidden_nodes":4,"output_nodes":1,"weights_ih":{"rows":4,"cols":2,"data":[[-0.569756962028276,-0.5551249563852504],[0.12350867790872079,0.9117849077336435],[0.6448544388169963,0.37673042356680453],[0.15511871409391612,0.8289329770778586]]},"weights_ho":{"rows":1,"cols":4,"data":[[0.25671912531734464,0.02490177829691785,-0.537758005494704,-0.10072084075558174]]},"bias_h":{"rows":4,"cols":1,"data":[[-0.28493369112696326],[0.6027906492507047],[0.7374757365358221],[-0.5460077788408344]]},"bias_o":{"rows":1,"cols":1,"data":[[0.37380624852136013]]},"learning_rate":0.1,"activation_function":{}}';

let nn = NeuralNetwork.deserialize(defaultNN);

for (let i = 0; i < (training_data.length * 100); i++) {
let index = i % training_data.length;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should randomize the index to train the NN better probably like :-
let index = Math.random() * training_data.length | 0;

let data = training_data[index];
nn.train(data.inputs, data.outputs);
}
expect(nn.serialize()).toEqual(targetNN);
});