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

Commit

Permalink
adds example of multi layered network
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyklee committed Oct 2, 2019
1 parent 519cfe0 commit ab6ef26
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
18 changes: 18 additions & 0 deletions p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html
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 p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js
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(){

// }

0 comments on commit ab6ef26

Please sign in to comment.