Published
Edited
Apr 28, 2020
Insert cell
Insert cell
testFunction = x => 3 * x - 2
Insert cell
Insert cell
testXs = R.uniq(Array.from(Array(15)).map(() => Math.round(Math.random() * 10)))
Insert cell
Insert cell
testYs = testXs.map(testFunction)
Insert cell
Insert cell
result = {
// montrer que tensorflow réfléchit avant de retourner le résultat
yield html`<p>Predicting...</p>`
// initialiser tensorflow
const m = tf.sequential()
// definir la structure du réseau de neurones
m.add(tf.layers.dense({ units: 1, inputShape: [1] }))
m.compile({ loss: 'meanSquaredError', optimizer: 'sgd' })
// passer les données crées plus haut à tensorflow
yield m.fit(
tf.tensor2d(testXs, [testXs.length, 1]),
tf.tensor2d(testYs, [testYs.length, 1]),
{ epochs: 500 }
)
.then(() => {
// une fois que tensorflow a créé un modèle à partir des donnée que nous lui avons données...
// créeons une série de valeurs "x" pour voir s'il arrive en deviner les valeurs "y" correspondantes
const toCheck = R.uniq(Array.from(Array(8)).map(() => Math.round(Math.random() * 10)))
// la table qui va être affichée ci-dessus
return html`<table
<tr>
<th>Input</th><th>Output</th><th>Expected</th><th>Difference</th>
</tr>
${toCheck.map(x => {
// pour chaque "x", demander à tensorflow de prédire "y"
const result = m.predict(tf.tensor2d([x], [1,1])).dataSync()
return html`<tr>
<td>${x}</td>
<td>${result}</td>
<td>${testFunction(x)}</td>
<td>${Math.abs(testFunction(x) - result)}</td>
</tr>`
})}
</table>`
})
}
Insert cell
tf = require('@tensorflow/tfjs')
Insert cell
import {R} from '@itacirgabral/ramda'
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more