Public
Edited
Dec 1, 2022
Insert cell
Insert cell
online_demo.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data = online_demo
Insert cell
tf = require("@tensorflow/tfjs@0.8.0/dist/tf.min.js")
Insert cell
// model = {
// const x = tf.tensor1d(data.map((e)=>({age: e['age']})).map(e => e.age));
// const y = tf.tensor1d(data.map((e)=>({percare: e['Personal Care Activities']})).map(e => e.percare));
// // Creating variables
// const m = tf.variable(tf.scalar(Math.random()*2-1));
// const b = tf.variable(tf.scalar(Math.random()*2-1));
// // Specifying a learning rate and an optimizer
// const learningRate = 0.1;
// const optimizer = tf.train.sgd(learningRate);
// // Mean Squared Error Loss
// function loss(pred, label) {
// return pred.sub(label).square().mean();
// }
// function predict(xs) {
// // y = mx+b
// const ys = xs.mul(m).add(b)
// return ys
// }
// // Training model for 50 epochs
// for(let i = 0; i < 50; i++) {
// optimizer.minimize(() => loss(predict(x), y))
// }

// m.print()
// b.print()
// // Printing our weights (slope, intercept)
// return {m, b}
// }
Insert cell
data.map((e)=>({age: e['age']})).map(e => e.age).slice(0, 10)
Insert cell
data.map((e)=>({percare: e['Personal Care Activities']})).map(e => e.percare).slice(0, 10)
Insert cell
data.map((e) => ({salary: e['salary']})).map(e => e.salary).slice(0, 10)
Insert cell
model = {
const Sdata = tf.tensor1d(data.map((e)=>({age: e['age']})).map(e => e.age).slice(0, 10));
const Fdata = tf.tensor1d(
data.map((e)=>({percare: e['Personal Care Activities']})).map(e => e.percare).slice(0, 10));
const Rdata = tf.tensor1d(data.map((e) => ({salary: e['salary']})).map(e => e.salary).slice(0, 10))
const a0 = tf.scalar(Math.random()).variable();
const a1 = tf.scalar(Math.random()).variable();
const a2 = tf.scalar(Math.random()).variable();
const fun = (r,s) => a2.mul(r).add(a1.mul(s)).add(a0)
const cost = (pred, label) => pred.sub(label).square().mean();
const learningRate = 0.001;
const optimizer = tf.train.sgd(learningRate);
// Train the model.
for (let i = 0; i < 800; i++) {
console.log("training")
optimizer.minimize(() => cost(fun(Rdata,Sdata), Fdata));
}
console.log(`a: ${a0.dataSync()}, b: ${a1.dataSync()}, c: ${a2.dataSync()}`);
const preds = fun(Rdata,Sdata).dataSync();
preds.forEach((pred, i) => {
console.log(`x: ${i}, pred: ${pred}`);
});

return preds;
}
Insert cell
model2 = {
const ml = tf.sequential();
ml.add(tf.layers.dense({ units: 1, inputShape: 1 }));

const xs = tf.tensor1d(data.map((e)=>({age: e['age']})).map(e => e.age).slice(0, 10));
const ys = tf.tensor1d(
data.map((e)=>({percare: e['Personal Care Activities']})).map(e => e.percare).slice(0, 10));

// parameters
const learningRate = 0.0001;
const optimizer = tf.train.sgd(learningRate);
const batchSize = 32;
const epochs = 500;

//model
ml.compile({
loss: "meanSquaredError",
optimizer: "sgd",
metrics: ["mse"],
});

await ml.fit(xs, ys, batchSize, epochs);

const prediction = ml.predict(tf.tensor1d([1,2,3]));
}
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