Created
December 31, 2019 12:34
-
-
Save novasush/df35cc2d8a914e06773114986ccde186 to your computer and use it in GitHub Desktop.
Training and predicting an equation in chrome browser asynchronously using tensorflow.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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Training a model on browser</title> | |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script> | |
<script lang="js"> | |
async function doTraining(model){ | |
const history = | |
await model.fit(xs, ys, | |
{ epochs: 500, | |
callbacks:{ | |
onEpochEnd: async(epoch, logs) =>{ | |
console.log("Epoch:" | |
+ epoch | |
+ " Loss:" | |
+ logs.loss); | |
} | |
} | |
}); | |
} | |
const model = tf.sequential(); | |
model.add(tf.layers.dense({units: 1, inputShape: [1]})); | |
model.compile({loss:'meanSquaredError', | |
optimizer:'sgd'}); | |
model.summary(); | |
const xs = tf.tensor2d([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], [6, 1]); | |
const ys = tf.tensor2d([-3.0, -1.0, 2.0, 3.0, 5.0, 7.0], [6, 1]); | |
doTraining(model).then(() => { | |
alert(model.predict(tf.tensor2d([10], [1,1]))); | |
}); | |
</script> | |
</head> | |
<body> | |
<h1 align="center">Press 'f12' key or 'Ctrl' + 'Shift' + 'i' to check whats going on</h1> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment