Skip to content
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
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
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