Published
Edited
Apr 11, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function apply(params, xt) {
let [w, b] = params;
return xt.dot(w).add(b).cos()
}
Insert cell
init_params = [
tf.randomNormal([1]),
tf.randomNormal([1]).squeeze(),
]
Insert cell
n = 100
Insert cell
function make_regression(n, p) {
let _ = Math.seedrandom(7)
let xt = tf.randomUniform([n, p], -3, 3.)
let w = tf.randomNormal([p]).mul(2.)
let b = tf.randomNormal([1]).squeeze()
let noise = tf.randomNormal([n]).mul(0.2)
let yt = xt.dot(w).add(b).sin().add(noise)
let reg = {xt: xt, yt: yt, w: w, b: b}
return reg
}
Insert cell
reg = make_regression(n, 1)
Insert cell
yt = reg["yt"]
Insert cell
y = yt.array()
Insert cell
x = xt.array()
Insert cell
xt = reg["xt"]
Insert cell
x.map((e, i) => (e, i))
Insert cell
data = x.map((e, i) => ({x: e, y: y[i], step: i}))
Insert cell
function make_mse(apply_fn, xt, yt) {
return (...params) => {
let pred = apply_fn(params, xt)
return pred.sub(yt).square().mean([0])
}
}
Insert cell
grad_loss = tf.valueAndGrads(loss)
Insert cell
loss = make_mse(apply, xt, yt)
Insert cell
loss_batch = make_mse(apply, xt, yt.reshape([-1, 1]))
// compute vectorized gradient for e.g. a gradient grid
Insert cell
// grid = tf.stack([xtiled, ytiled], [1]).reshape([n_points ** 2, 2])
Insert cell
// splitted = tf.split(grid, 2, 1)
Insert cell
// vl.markPoint().data(
// recordify(xtiled.arraySync(), ytiled.arraySync(), "xy")
// ).encode(vl.x().fieldQ('x'),
// vl.y().fieldQ('y'),
// // vl.color().fieldN('origin'),
// // vl.tooltip(['origin']) // show the Name and Origin fields in a tooltip
// ).render()
Insert cell
Insert cell
Insert cell
function optimize(grad_loss, epochs, lr, momentum, init_params){
let params = init_params;
let z = params.map(w => 0);
let memo = {loss: [], params: []};
for (let i = 0; i <= epochs; i++) {
let {value, grads} = grad_loss(params);
z = grads.map((g, i) => tf.add(g, tf.mul(momentum, z[i])))
params = params.map((w, i) => tf.sub(w, z[i].mul(lr))) ;
memo["loss"].push(value);
memo["params"].push(params);
}
return memo
}
Insert cell
Insert cell
memo_params = res.params
Insert cell
final_params = memo_params[n_train]
Insert cell
losses = res.loss
Insert cell
loss_data = losses.map((l, i) => ({loss: l.dataSync(), step: i}))
Insert cell
res = optimize(grad_loss, n_train, lr, momentum, init_params)
Insert cell
loss_chart = vl.markLine().data(loss_data).encode(
vl.x().fieldQ('step'),
vl.y().fieldQ('loss'),
).width(600)
Insert cell
pred_chart = vl.markPoint().data(pred_data).encode(
vl.x().fieldQ('x'),
vl.y().fieldQ('y'),
vl.color().fieldN('origin'),
vl.tooltip(['origin'])
).width(600)
Insert cell
Promise.all(final_params.map((x) => x.array()))
Insert cell
ypred = apply(final_params, xt).array()
Insert cell
pred_data = [
...x.map((e, i) => ({x: e, y: y[i], step: i, origin: "truth"})),
...x.map((e, i) => ({x: e, y: ypred[i], step: i, origin: "pred"}))
]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
init_meta = [tf.tensor([0.9]), tf.tensor([0.85])]
Insert cell
memo_meta = optimize(grad_meta_loss, 10, lr_meta, momentum_meta, init_meta)
Insert cell
function meta_loss (lr, momentum) {
let res = optimize(grad_loss, 20, lr, momentum, init_params)
return res.loss[res.loss.length - 1]
}
Insert cell
grad_meta_loss = tf.valueAndGrads(meta_loss)
Insert cell
meta_loss_data = memo_meta.loss.map((l, i) => ({loss: l.dataSync(), step: i}))
Insert cell
memo_meta.params[memo_meta.params.length -1][0].array()
Insert cell
memo_meta.loss[memo_meta.loss.length - 1].array()
Insert cell
md`## Imports`
Insert cell
import {Range, Selection} from "@observablehq/inputs"
Insert cell
import {vl} from '@vega/vega-lite-api'
Insert cell
d3 = require("d3@6")
Insert cell
// dfd = require('danfojs/dist/index.min.js').catch(() => {
// window.dfd.Series.prototype.print = window.dfd.DataFrame.prototype.print = function() { return print(this) };
// return window.dfd;
// })
Insert cell
// df_data = new dfd.DataFrame(data)
Insert cell
tf = {
let tf = await require("@tensorflow/tfjs")
tf.setBackend("cpu")
return tf
}
Insert cell
tf.getBackend()
Insert cell
// import { render, showLayer, showModelSummary, showValuesDistribution, showHistory } from '@horaceg/tensorflow-vis-api';
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