Skip to content

Commit 5a937a4

Browse files
committed
Merge branch 'master' of github.com:CodingTrain/Toy-Neural-Network-JS
2 parents b66f978 + c4019d6 commit 5a937a4

File tree

6 files changed

+353
-93
lines changed

6 files changed

+353
-93
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Neural Network JavaScript library for Coding Train tutorials
44

55
## Getting Started
66

7+
If you're looking for the original source code to match the videos [visit this repo] (https://github.com/CodingTrain/Rainbow-Code/tree/master/Courses/natureofcode/10.18-toy_neural_network)
8+
79
TODO
810

911
### Prerequisites
@@ -22,6 +24,12 @@ npm install
2224

2325
This Project doesn't require any additional Installing steps
2426

27+
### Documentation
28+
29+
* `NeuralNetwork` - The neural network class
30+
* `predict(input_array)` - Returns the output of a neural network
31+
* `train(input_array, target_array)` - Trains a neural network
32+
2533
## Running the tests
2634

2735
The Tests can either be checked via the automaticly running CircleCI Tests or you can also run `npm test` on your PC after you have done the Step "Prerequisites"
@@ -38,7 +46,7 @@ Please send PullRequests. These need to pass a automated Test first and after it
3846

3947
## Versioning
4048

41-
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/CodingTrain/Toy-Neural-Network-JS/tags).
49+
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/CodingTrain/Toy-Neural-Network-JS/tags).
4250

4351
## Authors
4452

TODO.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# To-Do List
2+
3+
* [x] Redo gradient descent video about
4+
* [x] delta weight formulas, connect to "mathematics of gradient" video
5+
* [x] Impelment gradient descent in library / with code
6+
* [ ] XOR coding challenge
7+
* [ ] MNIST coding challenge
8+
* [ ] Support for multiple hidden layers
9+
* [ ] Support for different activation functions
10+
* [ ] Combine with ml5 / deeplearnjs.

examples/xor/sketch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function draw() {
4141
let y = j * resolution;
4242
let input_1 = i / (cols - 1);
4343
let input_2 = j / (rows - 1);
44-
let output = nn.feedforward([input_1, input_2]);
44+
let output = nn.predict([input_1, input_2]);
4545
let col = output[0] * 255;
4646
fill(col);
4747
noStroke();

lib/matrix.js

Lines changed: 23 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,17 @@ class Matrix {
55
constructor(rows, cols) {
66
this.rows = rows;
77
this.cols = cols;
8-
this.data = [];
9-
10-
for (let i = 0; i < this.rows; i++) {
11-
this.data[i] = [];
12-
for (let j = 0; j < this.cols; j++) {
13-
this.data[i][j] = 0;
14-
}
15-
}
8+
this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0));
169
}
1710

1811
static fromArray(arr) {
19-
let m = new Matrix(arr.length, 1);
20-
for (let i = 0; i < arr.length; i++) {
21-
m.data[i][0] = arr[i];
22-
}
23-
return m;
12+
return new Matrix(arr.length, 1).map((e, i) => arr[i]);
2413
}
2514

2615
static subtract(a, b) {
2716
// Return a new Matrix a-b
28-
let result = new Matrix(a.rows, a.cols);
29-
for (let i = 0; i < result.rows; i++) {
30-
for (let j = 0; j < result.cols; j++) {
31-
result.data[i][j] = a.data[i][j] - b.data[i][j];
32-
}
33-
}
34-
return result;
17+
return new Matrix(a.rows, a.cols)
18+
.map((_, i, j) => a.data[i][j] - b.data[i][j]);
3519
}
3620

3721
toArray() {
@@ -45,74 +29,47 @@ class Matrix {
4529
}
4630

4731
randomize() {
48-
for (let i = 0; i < this.rows; i++) {
49-
for (let j = 0; j < this.cols; j++) {
50-
this.data[i][j] = Math.random() * 2 - 1;
51-
}
52-
}
32+
return this.map(e => Math.random() * 2 - 1);
5333
}
5434

5535
add(n) {
5636
if (n instanceof Matrix) {
57-
for (let i = 0; i < this.rows; i++) {
58-
for (let j = 0; j < this.cols; j++) {
59-
this.data[i][j] += n.data[i][j];
60-
}
61-
}
37+
return this.map((e, i, j) => e + n.data[i][j]);
6238
} else {
63-
for (let i = 0; i < this.rows; i++) {
64-
for (let j = 0; j < this.cols; j++) {
65-
this.data[i][j] += n;
66-
}
67-
}
39+
return this.map(e => e + n);
6840
}
6941
}
7042

7143
static transpose(matrix) {
72-
let result = new Matrix(matrix.cols, matrix.rows);
73-
for (let i = 0; i < matrix.rows; i++) {
74-
for (let j = 0; j < matrix.cols; j++) {
75-
result.data[j][i] = matrix.data[i][j];
76-
}
77-
}
78-
return result;
44+
return new Matrix(matrix.cols, matrix.rows)
45+
.map((_, i, j) => matrix.data[j][i]);
7946
}
8047

8148
static multiply(a, b) {
8249
// Matrix product
8350
if (a.cols !== b.rows) {
8451
console.log('Columns of A must match rows of B.')
85-
return undefined;
52+
return;
8653
}
87-
let result = new Matrix(a.rows, b.cols);
88-
for (let i = 0; i < result.rows; i++) {
89-
for (let j = 0; j < result.cols; j++) {
54+
55+
return new Matrix(a.rows, b.cols)
56+
.map((e, i, j) => {
9057
// Dot product of values in col
9158
let sum = 0;
9259
for (let k = 0; k < a.cols; k++) {
9360
sum += a.data[i][k] * b.data[k][j];
9461
}
95-
result.data[i][j] = sum;
96-
}
97-
}
98-
return result;
62+
return sum;
63+
});
9964
}
10065

10166
multiply(n) {
10267
if (n instanceof Matrix) {
10368
// hadamard product
104-
for (let i = 0; i < this.rows; i++) {
105-
for (let j = 0; j < this.cols; j++) {
106-
this.data[i][j] *= n.data[i][j];
107-
}
108-
}
69+
return this.map((e, i, j) => e * n.data[i][j]);
10970
} else {
11071
// Scalar product
111-
for (let i = 0; i < this.rows; i++) {
112-
for (let j = 0; j < this.cols; j++) {
113-
this.data[i][j] *= n;
114-
}
115-
}
72+
return this.map(e => e * n);
11673
}
11774
}
11875

@@ -121,29 +78,24 @@ class Matrix {
12178
for (let i = 0; i < this.rows; i++) {
12279
for (let j = 0; j < this.cols; j++) {
12380
let val = this.data[i][j];
124-
this.data[i][j] = func(val);
81+
this.data[i][j] = func(val, i, j);
12582
}
12683
}
84+
return this;
12785
}
12886

12987
static map(matrix, func) {
130-
let result = new Matrix(matrix.rows, matrix.cols);
13188
// Apply a function to every element of matrix
132-
for (let i = 0; i < matrix.rows; i++) {
133-
for (let j = 0; j < matrix.cols; j++) {
134-
let val = matrix.data[i][j];
135-
result.data[i][j] = func(val);
136-
}
137-
}
138-
return result;
89+
return new Matrix(matrix.rows, matrix.cols)
90+
.map((e, i, j) => func(matrix.data[i][j], i, j));
13991
}
14092

14193
print() {
14294
console.table(this.data);
95+
return this;
14396
}
14497
}
14598

146-
14799
if (typeof module !== 'undefined') {
148100
module.exports = Matrix;
149-
}
101+
}

0 commit comments

Comments
 (0)