Created
October 25, 2022 02:50
-
-
Save thedamian/ecc57e6aefa0e9f46d746da27de2c91d to your computer and use it in GitHub Desktop.
TesnorFlow.js Basic Example
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
<html> | |
<head> | |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"></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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment