From ab6ef265823639b31b85f51d6bc192d2fda71398 Mon Sep 17 00:00:00 2001 From: joeyklee Date: Wed, 2 Oct 2019 11:33:40 -0400 Subject: [PATCH] adds example of multi layered network --- .../NeuralNetwork_multiple-layers/index.html | 18 +++++ .../NeuralNetwork_multiple-layers/sketch.js | 72 +++++++++++++++++++ 2 files changed, 90 insertions(+) create mode 100644 p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html create mode 100644 p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js diff --git a/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html b/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html new file mode 100644 index 00000000..98213dfd --- /dev/null +++ b/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/index.html @@ -0,0 +1,18 @@ + + + + + Neural network with custom architecture + + + + + + + + +

Neural network with custom architecture

+ + + + \ No newline at end of file diff --git a/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js b/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js new file mode 100644 index 00000000..69ec8fc2 --- /dev/null +++ b/p5js/NeuralNetwork/NeuralNetwork_multiple-layers/sketch.js @@ -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(){ + +// } \ No newline at end of file