From ac1a7376282eb6618a54582e22e19198eb9cbb3f Mon Sep 17 00:00:00 2001 From: Vignesh S Date: Sun, 26 Jan 2020 11:55:48 +0530 Subject: [PATCH 1/3] Copy should be a static function Copy returns a new matrix hence should be a static function --- lib/matrix.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/matrix.js b/lib/matrix.js index b232d36..62fade9 100644 --- a/lib/matrix.js +++ b/lib/matrix.js @@ -8,14 +8,14 @@ class Matrix { this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0)); } - 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]; + static copy(matrix) { + let matrixCopy = new Matrix(matrix.rows, matrix.cols); + for (let i = 0; i < matrix.rows; i++) { + for (let j = 0; j < matrix.cols; j++) { + matrixCopy.data[i][j] = matrix.data[i][j]; } } - return m; + return matrixCopy; } static fromArray(arr) { From bc7ae6ed8aa35c36be3e4e2e983986c31f835415 Mon Sep 17 00:00:00 2001 From: Vignesh S Date: Sun, 26 Jan 2020 12:01:09 +0530 Subject: [PATCH 2/3] Update matrix.test.js --- lib/matrix.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matrix.test.js b/lib/matrix.test.js index 49fc653..9b6fdee 100644 --- a/lib/matrix.test.js +++ b/lib/matrix.test.js @@ -381,7 +381,7 @@ test('matrix copy', () => { let m = new Matrix(5, 5); m.randomize(); - let n = m.copy(); + let n = Matrix.copy(m); expect(n).toEqual({ rows: m.rows, From 6bd3028761fda448a8c018087ab66056a8f08822 Mon Sep 17 00:00:00 2001 From: Vignesh S Date: Sun, 26 Jan 2020 12:03:53 +0530 Subject: [PATCH 3/3] Reflect changes for static function in nn class Reflect changes made in matrix lib for static function in nn class --- lib/nn.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/nn.js b/lib/nn.js index 0cb4527..2ceccb8 100644 --- a/lib/nn.js +++ b/lib/nn.js @@ -30,11 +30,10 @@ class NeuralNetwork { 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(); + this.weights_ih = Matrix.copy(a.weights_ih); + this.weights_ho = Matrix.copy(a.weights_ho); + this.bias_h = Matrix.copy(a.bias_h); + this.bias_o = Matrix.copy(a.bias_o); } else { this.input_nodes = in_nodes; this.hidden_nodes = hid_nodes;