Created
May 21, 2018 17:47
-
-
Save bileschi/fbbf5f5522db56ecf178d6551bdf74be to your computer and use it in GitHub Desktop.
Jeremy Ellis 2d conv sample
This file contains hidden or 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
| <html> | |
| <head> | |
| </head> | |
| <body> | |
| <!-- <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.10.3"> </script> --> | |
| <script src="./tf.js"> </script> | |
| <input type="text" id="myAsk" size=40 value="1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0"><br> | |
| <input id="myButton123" type="button" value="Convolutional Train and Test" onclick=" | |
| { document.getElementById('myButton123').style.backgroundColor = 'red' // just to show something is happening // Generate some synthetic data for training. | |
| const xs = tf.tensor4d([ | |
| 0,0,0,0, | |
| 0,0,1,0, | |
| 1,0,0,0, | |
| 0,0,0,0, | |
| 0,0,0,0, | |
| 0,0,0,0, | |
| 1,1,1,1, | |
| 0,0,0,0, | |
| 0,0,0,0, | |
| 0,0,0,0, | |
| 0,0,0,0, | |
| 1,1,1,1, | |
| 0,0,1,0, | |
| 0,0,1,0, | |
| 1,1,1,1, | |
| 0,0,1,0 ], | |
| [4, 4, 4, 1] ); | |
| // Labels of how many lines in each image. I can make much more images just few here for simplicity | |
| const ys = tf.tensor2d([0, 1, 1, 2 ], [4, 1]); | |
| const model = tf.sequential(); | |
| model.add(tf.layers.conv2d({ | |
| inputShape: [4, 4, 1], | |
| kernelSize: 2, | |
| filters: 4, | |
| strides: 1, | |
| activation: 'relu', | |
| kernelInitializer: 'varianceScaling' | |
| })); | |
| model.add(tf.layers.conv2d({ kernelSize: 2, filters: 4, strides: 1, activation: 'relu', kernelInitializer: 'varianceScaling' })); | |
| model.add(tf.layers.flatten()); | |
| model.add(tf.layers.dense({ units: 10, kernelInitializer: 'varianceScaling', activation: 'softmax' })); | |
| model.add(tf.layers.dense({ units: 1, activation: 'linear' }) ); model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); | |
| model.compile({optimizer: 'sgd', loss: 'meanSquaredError'}); | |
| (async function () { // inline async so we can use promises and await | |
| for ( let myLoop = 1; myLoop <= 100; myLoop++ ) { | |
| var myFit = await model.fit(xs, ys, { epochs: 10 }); | |
| if (myLoop % 20 == 0){ | |
| await tf.nextFrame(); // This allows the GUI to update but only every 20 batches | |
| document.getElementById('myDiv123').innerHTML = 'Loss after Batch ' + myLoop + ' : ' + myFit.history.loss[0] +'<br><br>' | |
| } | |
| } | |
| const myPredictArray = await model.predict(tf.tensor4d(document.getElementById('myAsk').value.split(','), [1, 4, 4, 1])) | |
| document.getElementById('myButton123').style.backgroundColor = 'lightgray' | |
| document.getElementById('myDiv123').innerHTML += 'Input '+document.getElementById('myAsk').value+', Output = ' + await myPredictArray.data() +'<br>' | |
| })() // end the inline async function | |
| }" > <div id='myDiv123'>...</div><br> | |
| </body> | |
| <html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment