This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds example of multi layered network
- Loading branch information
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<html> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Neural network with custom architecture</title> | ||
|
||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.min.js"></script> | ||
<script src="http://localhost:8080/ml5.js" type="text/javascript"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Neural network with custom architecture</h1> | ||
<script src="sketch.js"></script> | ||
</body> | ||
|
||
</html> |
72 changes: 72 additions & 0 deletions
72
p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
let nn; | ||
const nn_options = { | ||
inputs: 1, | ||
outputs: 1, | ||
layers: [ | ||
ml5.tf.layers.dense({ | ||
units: 16, | ||
inputShape: [1], | ||
activation: 'sigmoid', | ||
}), | ||
ml5.tf.layers.dense({ | ||
units: 16, | ||
inputShape: [1], | ||
activation: 'relu', | ||
}), | ||
ml5.tf.layers.dense({ | ||
units: 1, | ||
activation: 'sigmoid', | ||
}) | ||
], | ||
debug: true | ||
} | ||
|
||
function setup() { | ||
createCanvas(400, 400); | ||
background(240); | ||
nn = ml5.neuralNetwork(nn_options); | ||
console.log(nn); | ||
createTrainingData(); | ||
|
||
nn.normalizeData(); | ||
const train_options = { | ||
epochs: 32 | ||
} | ||
nn.train(train_options, finishedTraining); | ||
} | ||
|
||
function finishedTraining(){ | ||
|
||
nn.predict([10], function(err, result){ | ||
if(err){ | ||
console.log(err); | ||
return | ||
} | ||
console.log(result) | ||
}) | ||
|
||
nn.predict([390], function(err, result){ | ||
if(err){ | ||
console.log(err); | ||
return | ||
} | ||
console.log(result) | ||
}) | ||
} | ||
|
||
function createTrainingData(){ | ||
for(let i = 0; i < 400; i++){ | ||
if(i%2){ | ||
const x = floor(random(0, width/2)); | ||
nn.addData([x], [0]) | ||
}else { | ||
const x = floor(random(width/2, width)); | ||
nn.addData([x], [1]) | ||
} | ||
} | ||
|
||
} | ||
|
||
// function draw(){ | ||
|
||
// } |