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

Changing colors to rainbow colors #104

Open
wants to merge 86 commits into
base: xor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
2d9b40a
Merge pull request #1 from CodingTrain/master
Maik1999 Feb 5, 2018
7edb855
Fixed typo
mikaelsouza Feb 7, 2018
5ecde14
Coding Challenge: the "Hello World" of NN - handwritten number recogn…
JonasFovea Feb 7, 2018
d39fc16
Fix video repo link
philaturner Feb 7, 2018
fa362fb
Moved the To-Do list in the README
mdatsev Feb 7, 2018
30c6790
Merge pull request #35 from JonasFovea/patch-1
shiffman Feb 8, 2018
e688e95
Merge branch 'master' into patch-1
shiffman Feb 8, 2018
0ae8a0d
Merge pull request #31 from mikaelsouza/patch-1
shiffman Feb 8, 2018
f75b0ea
Merge branch 'master' into patch-2
shiffman Feb 8, 2018
c5f6f1c
Merge pull request #36 from philaturner/patch-2
shiffman Feb 8, 2018
59509bd
Adding to README the CircleCi Build Status.
narchontis Feb 8, 2018
d65aba1
Merge pull request #39 from narchontis/patch-1
shiffman Feb 8, 2018
91625d1
Updated To-Do List
mdatsev Feb 8, 2018
ddc159e
merge
mdatsev Feb 8, 2018
d9a34e4
Merge pull request #37 from mdatsev/master
shiffman Feb 8, 2018
1fcd1cc
Merge pull request #2 from CodingTrain/master
Maik1999 Feb 9, 2018
8e0057c
Added error for elemntwise addition, subbtraction and hadamard produc…
Feb 9, 2018
b59d427
Give frame work to add additional activation functions
schrummy14 Feb 9, 2018
1ac89ae
new mnist example
shiffman Feb 9, 2018
f181c5c
Merge branch 'master' into mnist
shiffman Feb 9, 2018
1410e5f
Merge pull request #46 from CodingTrain/mnist
shiffman Feb 9, 2018
a3ab00c
Only show guess if there's a user drawing
AlcaDesign Feb 9, 2018
2d5b3d2
Alter how mnist files are loaded
meiamsome Feb 9, 2018
4957d96
Merge branch 'master' into added-error-handling
shiffman Feb 9, 2018
c081d38
Merge branch 'master' into patch-1
shiffman Feb 9, 2018
b3ce7d3
Merge pull request #43 from Maik1999/added-error-handling
shiffman Feb 9, 2018
e57a6f2
Merge branch 'master' into patch-1
shiffman Feb 9, 2018
cfae556
Merge branch 'master' into master
shiffman Feb 9, 2018
5b46ce3
Merge pull request #48 from meiamsome/master
shiffman Feb 9, 2018
0a44090
Merge branch 'master' into patch-1
AlcaDesign Feb 9, 2018
be04a3f
Move select into setup
AlcaDesign Feb 9, 2018
f876f65
Add .gitattributes for Windows compatibility and add Emacs junk to .g…
Versatilus Feb 9, 2018
c00678a
Merge branch 'master' into pr-gitattributes
Versatilus Feb 9, 2018
300b427
Merge branch 'master' into patch-1
schrummy14 Feb 10, 2018
07d81b3
Added de- and serialization functions.
enginefeeder101 Feb 10, 2018
b33a97d
Merge pull request #47 from AlcaDesign/patch-1
shiffman Feb 10, 2018
7e69f68
Merge branch 'master' into savestate
shiffman Feb 10, 2018
37051d9
Merge pull request #50 from enginefeeder101/savestate
shiffman Feb 10, 2018
47b4423
Merge branch 'master' into pr-gitattributes
shiffman Feb 10, 2018
efc8aec
Merge pull request #49 from Versatilus/pr-gitattributes
shiffman Feb 10, 2018
87f12e7
Merge branch 'master' into patch-1
shiffman Feb 10, 2018
07b072b
Update README.md
shiffman Feb 10, 2018
b147cba
Merge pull request #45 from schrummy14/patch-1
shiffman Feb 10, 2018
bc86d80
Merge branch 'master' into shiffman-patch-2
shiffman Feb 10, 2018
08e188d
Merge pull request #51 from CodingTrain/shiffman-patch-2
shiffman Feb 10, 2018
aa33ad8
Update README.md
shiffman Feb 10, 2018
57ff0da
Merge pull request #55 from CodingTrain/shiffman-patch-3
shiffman Feb 10, 2018
a6b03ad
User Input needs to be normalized
papalotis Feb 11, 2018
8f4c76d
Add links to demos running in GitHub Pages
enginefeeder101 Feb 11, 2018
a267379
Show links in a list.
enginefeeder101 Feb 11, 2018
10b49a5
Updated p5.js to v0.6.0, removed unused sound lib and deleted local libs
enginefeeder101 Feb 11, 2018
1ceadaa
Merge pull request #3 from CodingTrain/master
Maik1999 Feb 11, 2018
11c6d98
Add MIT license file
enginefeeder101 Feb 11, 2018
e604173
Merge pull request #60 from enginefeeder101/MIT
shiffman Feb 11, 2018
185f92d
Merge branch 'master' into pages
shiffman Feb 11, 2018
3e5ef5f
Merge pull request #58 from enginefeeder101/pages
shiffman Feb 11, 2018
7b165d5
Merge branch 'master' into patch-1
shiffman Feb 11, 2018
eacfdf0
Merge pull request #57 from papalotis/patch-1
shiffman Feb 11, 2018
7ed5b8c
Merge branch 'master' into p5fix
enginefeeder101 Feb 11, 2018
e2d8cea
Added local libraries for p5.js v0.6.0
enginefeeder101 Feb 11, 2018
43f5ab9
Merge pull request #59 from enginefeeder101/p5fix
shiffman Feb 11, 2018
871e5b5
Merge pull request #4 from CodingTrain/master
Maik1999 Feb 11, 2018
4b06127
manage activation function in a class
Feb 11, 2018
5f9555b
added tanh
Feb 11, 2018
0a97778
Update README.md
shiffman Feb 11, 2018
ca8b90d
Merge pull request #63 from CodingTrain/shiffman-patch-4
shiffman Feb 11, 2018
b728f5a
Merge branch 'master' into activation-function-class
shiffman Feb 11, 2018
bfc0be8
Merge pull request #62 from Maik1999/activation-function-class
shiffman Feb 11, 2018
2dc6b2b
Update README.md
shiffman Feb 11, 2018
a20aa4a
Added links to the mentioned pull requests
Maik1999 Feb 11, 2018
60a0319
Merge pull request #67 from Maik1999/patch-1
shiffman Feb 11, 2018
88bae36
Made build badge clickable
rhbvkleef Feb 16, 2018
bd12277
Merge pull request #81 from rhbvkleef/clickable-build-badge
shiffman Feb 17, 2018
fcacc8d
Added link to pull request 62
Maik1999 Feb 25, 2018
01784f9
Merge pull request #87 from Maik1999/patch-2
shiffman Feb 25, 2018
d641c86
adding new doodle classifier example
shiffman Mar 2, 2018
7615757
Create README.md
shiffman Mar 6, 2018
c8ae50a
adding new neuro-evolution example
shiffman Mar 9, 2018
846a259
Adding code from March 9 live stream
shiffman Mar 9, 2018
03718ae
Add Matrix copy() test
enginefeeder101 Mar 10, 2018
e713dff
Merge pull request #99 from enginefeeder101/copytest
shiffman Mar 10, 2018
3622343
Create a README in the xor example
jonathan-richer Mar 21, 2018
20bc3db
Update the structure of the README file
jonathan-richer Mar 22, 2018
4dd37fa
Merge pull request #103 from codeMeHtm/patch-2
shiffman Mar 24, 2018
f44dbd9
Making the colors rainbow colors!
jonathan-richer Mar 24, 2018
80bfe0b
Changing most of the let keywords for const
jonathan-richer Jun 29, 2018
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
12 changes: 12 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
* text=auto
*.js text
*.html text
*.css text
*.txt text
*.json text
*.md text
*.yml text

#MNIST data
*idx3-ubyte binary
*idx1-ubyte binary
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
node_modules
*\# # Emacs cruft
*~ # Emacs cruft
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 The Coding Train

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
# Toy-Neural-Network-JS
# Toy-Neural-Network-JS [![Build Status](https://circleci.com/gh/CodingTrain/Toy-Neural-Network-JS.png?&style=shield&circle-token=:circle-token)](https://circleci.com/gh/CodingTrain/Toy-Neural-Network-JS)

Neural Network JavaScript library for Coding Train tutorials

## Getting Started
## Examples / Demos
Here are some demos running directly in the browser:
* [XOR problem](https://codingtrain.github.io/Toy-Neural-Network-JS/examples/xor/)
* [Handwritten digit recognition](https://codingtrain.github.io/Toy-Neural-Network-JS/examples/mnist/)

## To-Do List

* [x] Redo gradient descent video about
* [x] Delta weight formulas, connect to "mathematics of gradient" video
* [x] Implement gradient descent in library / with code
* [x] XOR coding challenge [live example](https://codingtrain.github.io/Toy-Neural-Network-JS/examples/xor/)
* [ ] MNIST coding challenge [live example](https://codingtrain.github.io/Toy-Neural-Network-JS/examples/mnist/)
* redo this challenge
* cover softmax activation, cross-entropy
* graph cost function?
* only use testing data
* [ ] Support for saving / restoring network (see [#50](https://github.com/CodingTrain/Toy-Neural-Network-JS/pull/50))
* [ ] Support for different activation functions (see [#45](https://github.com/CodingTrain/Toy-Neural-Network-JS/pull/45), [#62](https://github.com/CodingTrain/Toy-Neural-Network-JS/pull/62))
* [ ] Support for multiple hidden layers (see [#61](https://github.com/CodingTrain/Toy-Neural-Network-JS/pull/61))
* [ ] Support for neuro-evolution
* [ ] play flappy bird (many players at once).
* [ ] play pong (many game simulations at once)
* [ ] steering sensors (a la Jabril's forrest project!)
* [ ] Combine with ml5 / deeplearnjs

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)
## Getting Started

TODO
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)

### Prerequisites

Expand Down Expand Up @@ -53,3 +76,7 @@ We use [SemVer](http://semver.org/) for versioning. For the versions available,
* **shiffman** - *Initial work* - [shiffman](https://github.com/shiffman)

See also the list of [contributors](https://github.com/CodingTrain/Toy-Neural-Network-JS/contributors) who participated in this project.

## License

This project is licensed under the terms of the MIT license, see LICENSE.
10 changes: 0 additions & 10 deletions TODO.md

This file was deleted.

1 change: 1 addition & 0 deletions examples/doodle_classification/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://codingtrain.github.io/Toy-Neural-Network-JS/examples/doodle_classification/
Binary file added examples/doodle_classification/data/cats1000.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
15 changes: 15 additions & 0 deletions examples/doodle_classification/dataprep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function prepareData(category, data, label) {
category.training = [];
category.testing = [];
for (let i = 0; i < totalData; i++) {
let offset = i * len;
let threshold = floor(0.8 * totalData);
if (i < threshold) {
category.training[i] = data.bytes.subarray(offset, offset + len);
category.training[i].label = label;
} else {
category.testing[i - threshold] = data.bytes.subarray(offset, offset + len);
category.testing[i - threshold].label = label;
}
}
}
34 changes: 34 additions & 0 deletions examples/doodle_classification/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>doodle classifier</title>

<script src="libraries/p5.min.js"></script>
<script src="libraries/p5.dom.min.js"></script>
<script src="loadbinary.js"></script>
<script src="traintest.js"></script>
<script src="dataprep.js"></script>
<script src="sketch.js"></script>
<script src="../../lib/nn.js"></script>
<script src="../../lib/matrix.js"></script>
<style>
body {
background-color: #AAA;
}
button {
font-size: 24pt;
margin: 10px;
}
</style>
</head>
<body>
<button id="train">train</button>
<button id="test">test</button>
<button id="guess">guess</button>
<button id="clear">clear</button>
</body>
</html>
1 change: 1 addition & 0 deletions examples/doodle_classification/libraries/p5.dom.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions examples/doodle_classification/libraries/p5.min.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions examples/doodle_classification/loadbinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
p5.prototype.registerPreloadMethod('loadBytes');

p5.prototype.loadBytes = function(file, callback) {
var self = this;
var data = {};
var oReq = new XMLHttpRequest();
oReq.open("GET", file, true);
oReq.responseType = "arraybuffer";
oReq.onload = function(oEvent) {
var arrayBuffer = oReq.response;
if (arrayBuffer) {
data.bytes = new Uint8Array(arrayBuffer);
if (callback) {
callback(data);
}
self._decrementPreload();
}
}
oReq.send(null);
return data;
}
107 changes: 107 additions & 0 deletions examples/doodle_classification/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const len = 784;
const totalData = 1000;

const CAT = 0;
const RAINBOW = 1;
const TRAIN = 2;

let catsData;
let trainsData;
let rainbowsData;

let cats = {};
let trains = {};
let rainbows = {};

let nn;

function preload() {
catsData = loadBytes('data/cats1000.bin');
trainsData = loadBytes('data/trains1000.bin');
rainbowsData = loadBytes('data/rainbows1000.bin');
}


function setup() {
createCanvas(280, 280);
background(255);

// Preparing the data
prepareData(cats, catsData, CAT);
prepareData(rainbows, rainbowsData, RAINBOW);
prepareData(trains, trainsData, TRAIN);

// Making the neural network
nn = new NeuralNetwork(784, 64, 3);

// Randomizing the data
let training = [];
training = training.concat(cats.training);
training = training.concat(rainbows.training);
training = training.concat(trains.training);

let testing = [];
testing = testing.concat(cats.testing);
testing = testing.concat(rainbows.testing);
testing = testing.concat(trains.testing);

let trainButton = select('#train');
let epochCounter = 0;
trainButton.mousePressed(function() {
trainEpoch(training);
epochCounter++;
console.log("Epoch: " + epochCounter);
});

let testButton = select('#test');
testButton.mousePressed(function() {
let percent = testAll(testing);
console.log("Percent: " + nf(percent, 2, 2) + "%");
});

let guessButton = select('#guess');
guessButton.mousePressed(function() {
let inputs = [];
let img = get();
img.resize(28, 28);
img.loadPixels();
for (let i = 0; i < len; i++) {
let bright = img.pixels[i * 4];
inputs[i] = (255 - bright) / 255.0;
}

let guess = nn.predict(inputs);
// console.log(guess);
let m = max(guess);
let classification = guess.indexOf(m);
if (classification === CAT) {
console.log("cat");
} else if (classification === RAINBOW) {
console.log("rainbow");
} else if (classification === TRAIN) {
console.log("train");
}

//image(img, 0, 0);
});

let clearButton = select('#clear');
clearButton.mousePressed(function() {
background(255);
});
// for (let i = 1; i < 6; i++) {
// trainEpoch(training);
// console.log("Epoch: " + i);
// let percent = testAll(testing);
// console.log("% Correct: " + percent);
// }
}


function draw() {
strokeWeight(8);
stroke(0);
if (mouseIsPressed) {
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
41 changes: 41 additions & 0 deletions examples/doodle_classification/traintest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function trainEpoch(training) {
shuffle(training, true);
//console.log(training);
// Train for one epoch
for (let i = 0; i < training.length; i++) {
let data = training[i];
let inputs = Array.from(data).map(x => x / 255);
let label = training[i].label;
let targets = [0, 0, 0];
targets[label] = 1;
// console.log(inputs);
// console.log(targets);
nn.train(inputs, targets);
}
}

function testAll(testing) {

let correct = 0;
// Train for one epoch
for (let i = 0; i < testing.length; i++) {
// for (let i = 0; i < 1; i++) {
let data = testing[i];
let inputs = Array.from(data).map(x => x / 255);
let label = testing[i].label;
let guess = nn.predict(inputs);

let m = max(guess);
let classification = guess.indexOf(m);
// console.log(guess);
// console.log(classification);
// console.log(label);

if (classification === label) {
correct++;
}
}
let percent = 100 * correct / testing.length;
return percent;

}
3 changes: 3 additions & 0 deletions examples/mnist/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
on
tw
th
51 changes: 51 additions & 0 deletions examples/mnist/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>mnist</title>

<script src="libraries/p5.min.js"></script>
<script src="libraries/p5.dom.min.js"></script>
<script src="mnist.js"></script>
<script src="sketch.js"></script>
<script src="../../lib/nn.js"></script>
<script src="../../lib/matrix.js"></script>

<style>

body {
font-size:30pt;
}
.correct {
color: #00CC00
}
.wrong {
color: #CC0000
}


</style>




</head>
<body>

<div id="container"></div>

<p id="info">

<!-- Label: <span id="label"></span><br/>
Guess: <span id="guess"></span><br/> -->
<br/>
Testing: <span id="percent"></span><br/>
<br/>
User Guess: <span id="user_guess"></span>
</p>

</body>
</html>
1 change: 1 addition & 0 deletions examples/mnist/libraries/p5.dom.min.js

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions examples/mnist/libraries/p5.min.js

Large diffs are not rendered by default.

Loading