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

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more