Skip to content
This repository has been archived by the owner on Apr 6, 2020. It is now read-only.

Commit

Permalink
Refactors examples to match diy-nn-refactor branch (#195)
Browse files Browse the repository at this point in the history
* neural noteplayer example

* example inspired by Eyeo 2019 Rebecca Fiebrink talk

* rename musical mouse example

* example tweaks and oscillator

* adds refactoring examples

* updates examples based in refactor

* updates xor"

* rm unused co2net

* updates examples for regression

* updates musical note example

* adds experimental normalizationOptions

* adds example of multi layered network

* changes activations

* adds simple classification example

* changes .normalize() to .normalizeData()

* updates all examples to handle array of .predict() or .classify() output
  • Loading branch information
joeyklee authored Oct 2, 2019
1 parent 1dda807 commit 1dd01a0
Show file tree
Hide file tree
Showing 20 changed files with 487 additions and 2,490 deletions.
19 changes: 19 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_Simple-Classification/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>

<head>
<meta charset="UTF-8">
<title>Neural Network</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script>
</head>

<body>
<h1>Neural Network Classification</h1>
<label for="avatar">Load Model:</label>
<input type="file" id="load" multiple />
<script src="sketch.js"></script>
</body>

</html>
56 changes: 56 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_Simple-Classification/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2018 ml5
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

/* ===
ml5 Example
Image classification using MobileNet and p5.js
This example uses a callback pattern to create the classifier
=== */
let nn;

const options = {
inputs: 1,
outputs: 2,
task: 'classification',
debug: true
}

function setup(){
createCanvas(400, 400);
nn = ml5.neuralNetwork(options);


console.log(nn)
createTrainingData();
nn.normalizeData();

const trainingOptions={
batchSize: 24,
epochs: 32
}

nn.train(trainingOptions,finishedTraining); // if you want to change the training options
// nn.train(finishedTraining); // use the default training options
}

function finishedTraining(){

nn.classify([300], function(err, result){
console.log(result);
})

}

function createTrainingData(){
for(let i = 0; i < 400; i++){
if(i%2 === 0){
const x = random(0, width/2);
nn.addData( [x], ['left'])
} else {
const x = random(width/2, width);
nn.addData( [x], ['right'])
}
}
}
4 changes: 2 additions & 2 deletions p5js/NeuralNetwork/NeuralNetwork_Simple-Regression/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function setup(){

console.log(nn)
createTrainingData();
nn.data.normalize();
nn.normalizeData();

const trainingOptions={
batchSize: 24,
Expand All @@ -39,7 +39,7 @@ async function finishedTraining(){
await Promise.all(
[...new Array(400).fill(null).map( async (item, idx) => {
let results = await nn.predict([idx]);
let prediction = results.outputs
let prediction = results[0]
let x = idx
let y = prediction.value
fill(255, 0, 0);
Expand Down
19 changes: 12 additions & 7 deletions p5js/NeuralNetwork/NeuralNetwork_XOR/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ function setup() {
inputs: 2,
outputs: 1,
learningRate: 0.25,
debug:true
// hiddenUnits: 2
}
model = ml5.neuralNetwork(options);

//model = ml5.neuralNetwork(2, 1);
model.data.addData([0, 0], [0]);
model.data.addData([1, 0], [1]);
model.data.addData([0, 1], [1]);
model.data.addData([1, 1], [0]);
model.data.normalize();
model.train({ epochs: 200 }, whileTraining, finishedTraining);
model.addData([0, 0], [0]);
model.addData([1, 0], [1]);
model.addData([0, 1], [1]);
model.addData([1, 1], [0]);
model.normalizeData();
model.train({ epochs: 50 }, whileTraining, finishedTraining);

}

Expand All @@ -50,7 +51,11 @@ function finishedTraining() {
}

function gotResults(error, results) {
console.log(results.values[0]);
if(error){
console.log(err)
return
}
console.log(results[0].value);
}

function draw() {
Expand Down
10 changes: 5 additions & 5 deletions p5js/NeuralNetwork/NeuralNetwork_basics/sketch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function setup() {
inputs: 3,
outputs: 2,
task: 'regression',
// activationOutput: 'sigmoid',
// activationHidden: 'sigmoid'
debug:true
};
// Create Neural Network
nn = ml5.neuralNetwork(options);
Expand Down Expand Up @@ -96,7 +95,7 @@ function trainModel() {
// output1: training_target[1],
// });

nn.data.addData(training_input, training_target)
nn.addData(training_input, training_target)

}

Expand All @@ -105,7 +104,7 @@ function trainModel() {
batchSize: 12
}
// Train
nn.data.normalize();
nn.normalizeData();
nn.train(trainingOptions, finishedTraining);
}

Expand All @@ -127,6 +126,7 @@ function predict() {
function gotResults(error, results) {
if (error) console.log(error);
if (results) {
console.log(results.output);
console.log(results);
results.tensor.print()
}
}
Loading

0 comments on commit 1dd01a0

Please sign in to comment.