Skip to content

Instantly share code, notes, and snippets.

@hristian-carabulea
Forked from parmarsuraj99/tfjs-example.html
Created June 22, 2019 13:10
Show Gist options
  • Save hristian-carabulea/0931609d84339eb2c736f8ac3ee8d904 to your computer and use it in GitHub Desktop.
Save hristian-carabulea/0931609d84339eb2c736f8ac3ee8d904 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"> </script>
</head>
<body>
<p id="prediction">Prediction(100): </p>
<script>
async function run(){
const model=tf.sequential();
model.add(
tf.layers.dense({
units:1,
inputShape:[1],
bias: true
})
);
model.compile({
loss:'meanSquaredError',
optimizer: 'sgd',
metrics: ['mse']
});
const xs = tf.tensor1d([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const ys = tf.tensor1d([2, 5, 8, 12, 14, 18, 21, 23, 26, 29]);
await model.fit(xs, ys, {epochs:100});
document.getElementById('prediction').innerText+=
model.predict(tf.tensor1d([100])).dataSync();
}
run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment