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

Change copy functions over to use (de)serialization feature #98

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 1 addition & 7 deletions lib/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ class Matrix {
}

copy() {
let m = new Matrix(this.rows, this.cols);
for (let i = 0; i < this.rows; i++) {
for (let j = 0; j < this.cols; j++) {
m.data[i][j] = this.data[i][j];
}
}
return m;
return Matrix.deserialize(this.serialize());
}

static fromArray(arr) {
Expand Down
14 changes: 14 additions & 0 deletions lib/matrix.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ test('static map with row and column params', () => {
]
});
});

test('matrix (de)serialization', () => {
let m = new Matrix(5, 5);
m.randomize();
Expand All @@ -375,3 +376,16 @@ test('matrix (de)serialization', () => {
data: m.data
});
});

test('matrix copy', () => {
let m = new Matrix(5, 5);
m.randomize();

let n = m.copy();

expect(n).toEqual({
rows: m.rows,
cols: m.cols,
data: m.data
Copy link
Member

Choose a reason for hiding this comment

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

will this work? Won't we have to check the values of the arrays themselves as opposed to the data reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does, as this does not pass:

  let n = m.copy();
  m.add(1);

  expect(n).toEqual({
    rows: m.rows,
    cols: m.cols,
    data: m.data
  });

schermafdruk_2018-03-10_18-22-24

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually expect(n).toEqual(m); works as well 😆

Copy link
Member

Choose a reason for hiding this comment

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

Oh cool! So Jest does something like a "deep" comparison behind the scenes?

});
});
47 changes: 15 additions & 32 deletions lib/nn.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,22 @@ let tanh = new ActivationFunction(


class NeuralNetwork {
// TODO: document what a, b, c are
constructor(a, b, c) {
if (a instanceof NeuralNetwork) {
this.input_nodes = a.input_nodes;
this.hidden_nodes = a.hidden_nodes;
this.output_nodes = a.output_nodes;

this.weights_ih = a.weights_ih.copy();
this.weights_ho = a.weights_ho.copy();

this.bias_h = a.bias_h.copy();
this.bias_o = a.bias_o.copy();
} else {
this.input_nodes = a;
this.hidden_nodes = b;
this.output_nodes = c;

this.weights_ih = new Matrix(this.hidden_nodes, this.input_nodes);
this.weights_ho = new Matrix(this.output_nodes, this.hidden_nodes);
this.weights_ih.randomize();
this.weights_ho.randomize();

this.bias_h = new Matrix(this.hidden_nodes, 1);
this.bias_o = new Matrix(this.output_nodes, 1);
this.bias_h.randomize();
this.bias_o.randomize();
}

// TODO: copy these as well
constructor(input_nodes, hidden_nodes, output_nodes) {
this.input_nodes = input_nodes;
this.hidden_nodes = hidden_nodes;
this.output_nodes = output_nodes;

this.weights_ih = new Matrix(this.hidden_nodes, this.input_nodes);
this.weights_ho = new Matrix(this.output_nodes, this.hidden_nodes);
this.weights_ih.randomize();
this.weights_ho.randomize();

this.bias_h = new Matrix(this.hidden_nodes, 1);
this.bias_o = new Matrix(this.output_nodes, 1);
this.bias_h.randomize();
this.bias_o.randomize();
this.setLearningRate();
this.setActivationFunction();


}

predict(input_array) {
Expand Down Expand Up @@ -158,7 +141,7 @@ class NeuralNetwork {

// Adding function for neuro-evolution
copy() {
return new NeuralNetwork(this);
return NeuralNetwork.deserialize(this.serialize());
}

mutate(rate) {
Expand Down