Skip to content

Instantly share code, notes, and snippets.

@MickaelBaye
Created August 29, 2018 08:24
Show Gist options
  • Save MickaelBaye/680f634ceeea2f9790f82cdc1c0b020c to your computer and use it in GitHub Desktop.
Save MickaelBaye/680f634ceeea2f9790f82cdc1c0b020c to your computer and use it in GitHub Desktop.
Tensorflow.js Simple Example
<html>
<head>
<!-- Load TensorFlow.js -->
<!-- Get latest version at https://github.com/tensorflow/tfjs -->
<!-- From https://dzone.com/articles/getting-started-with-tensorflowjs -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.11.2"></script>
</head>
<body>
<div id="output_field"></div>
</body>
<script>
async function learnLinear(){
const model = tf.sequential();
model.add(tf.layers.dense({units: 1, inputShape: [1]}));
model.compile({ loss: 'meanSquaredError', optimizer: 'sgd' });
const xs = tf.tensor2d([-1, 0, 1, 2, 3, 4], [6, 1]);
const ys = tf.tensor2d([-3, -1, 1, 3, 5, 7], [6, 1]);
await model.fit(xs, ys, {epochs: 500});
document.getElementById('output_field').innerText = model.predict(tf.tensor2d([10], [1, 1]));
}
learnLinear();
</script>
<html>
@MickaelBaye
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment