Public
Edited
May 11, 2023
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
model_dict = {
reset_parameters;
// Feature weights and bias - the parameters we want to learn
const w = tf.tidy(
() => tf.variable(tf.randomNormal([D,1], 0, 1.0), true)
);
const b = tf.tidy(
() => tf.variable(tf.randomNormal([1], 0, 1.0), true)
);
// Logistic regression model: P(y=1 | x) = Bernoulli(y=1 | sigm(w.T * x + b))
// where * is matrix multiplication,
// w, x ∈ ℝ^D, b ∈ ℝ, and y ∈ {0,1}
const f = x =>
x.matMul(w)
.add(b)
.sigmoid()
.as1D();
yield {'model': f, 'w': w, 'b':b};

try {
yield invalidation;
} finally {
w.dispose();
b.dispose();
}
}
Insert cell
Insert cell
loss = (pred, truth) => tf.metrics.binaryCrossentropy(truth, pred)
Insert cell
Insert cell
Insert cell
Insert cell
current_loss = {
step; // Take an optimization step when the button is clicked
const optimizer = tf.train.sgd(learningRate);
// What parameters are we optimizing?
const vars = [];
if (params_to_optimize.includes('w'))
vars.push(model_dict.w);
if (params_to_optimize.includes('b'))
vars.push(model_dict.b);
// Optimize the loss or evaluate it if no parameters selected
let current_loss = 0;
if (vars.length > 0)
current_loss = tf.tidy(
() => optimizer.minimize(
() => loss(model(X), y), true, vars
)
);
else
current_loss = tf.tidy(
() => loss(model(X), y)
);
// Yield result and clean up
optimizer.dispose();
yield current_loss;
try {
yield invalidation;
} finally {
current_loss.dispose();
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {latex_matrix} from "@sorig/latex_matrix"
Insert cell
import {button, slider, checkbox} from "@jashkenas/inputs"
Insert cell
Insert cell
tick = {
while (true) {
yield Promises.tick(1000);
}
}
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