Public
Edited
Apr 16, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
curve = _.range(50)
.map((x) => x / 5)
.map((x) => new Object({ x: x, y: Math.sin(x)}))
Insert cell
noisy = curve.map(
(pt) => new Object({ x: pt.x, y: pt.y + d3.randomNormal(0, 0.1)() })
)
Insert cell
Insert cell
model = {
const input = tf.input({ shape: [1] });
const dense = tf.layers.dense({ units: 2, activation: "tanh" }).apply(input);
const output = tf.layers.dense({ units: 1 }).apply(dense);
const model = tf.model({ inputs: input, outputs: output });
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });
return model;
}
Insert cell
Insert cell
Insert cell
Insert cell
predictions = {
let step = 0;
while (step < 200) {
step += 1;
await model.fit(xs, ys, { epochs: 50 });
yield predict();
}
}
Insert cell
predict = () => {
return noisy.map(
(pt) =>
new Object({
x: pt.x,
y: model.predict(tf.tensor2d([pt.x], [1, 1])).arraySync()[0][0]
})
);
}
Insert cell
Insert cell
losses = []
Insert cell
losses.push(
d3.mean(
noisy.map(
(pt) => (pt.y - predictions.filter((pt2) => pt.x == pt2.x)[0].y) ** 2
)
)
)
Insert cell
lossesUpdating = {
predictions;
return losses;
}
Insert cell
Insert cell
tf = require('@tensorflow/tfjs')
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