Published
Edited
May 7, 2021
Insert cell
md`# Tensorflowjs
Predicting download speeds

`
Insert cell
md `### import tensorflowjs `
Insert cell
tf = require("@tensorflow/tfjs@0.9.0/dist/tf.min.js")
Insert cell
md` ##### create dummy data`
Insert cell
trainData = ({
sizeMB: [0.080, 9.000, 0.001, 0.100, 8.000,

5.000, 0.100, 6.000, 0.050, 0.500,

0.002, 2.000, 0.005, 10.00, 0.010,

7.000, 6.000, 5.000, 1.000, 1.000],
timeSec: [0.135, 0.739, 0.067, 0.126, 0.646,

0.435, 0.069, 0.497, 0.068, 0.116,

0.070, 0.289, 0.076, 0.744, 0.083,

0.560, 0.480, 0.399, 0.153, 0.149] })
Insert cell
testData = ({
sizeMB: [5.000, 0.200, 0.001, 9.000, 0.002,

0.020, 0.008, 4.000, 0.001, 1.000,

0.005, 0.080, 0.800, 0.200, 0.050,

7.000, 0.005, 0.002, 8.000, 0.008],
timeSec: [0.425, 0.098, 0.052, 0.686, 0.066,

0.078, 0.070, 0.375, 0.058, 0.136,

0.052, 0.063, 0.183, 0.087, 0.066,

0.558, 0.066, 0.068, 0.610, 0.057]

})
Insert cell
md ` ## Prepare Data for tensor flow by converting into tensors`
Insert cell
trainTensors = ({ sizeMB: tf.tensor2d(trainData.sizeMB, [20, 1]), timeSec: tf.tensor2d(trainData.timeSec, [20, 1]) })
Insert cell
testTensors = ({sizeMB: tf.tensor2d(testData.sizeMB, [20, 1]), timeSec: tf.tensor2d(testData.timeSec,[20, 1])})
Insert cell
md `## Create model`
Insert cell
model = {
// define a model for linear regression
const model = tf.sequential();
model.add(tf.layers.dense({inputShape:[1], units: 1}));
//Prepare the model for training: Specify the loss and the optimizer
model.compile({optimizer:'sgd',loss:'meanAbsoluteError'});
return model
}
Insert cell
md ` ## train model`
Insert cell
train = {
const h = await model.fit(
trainTensors.sizeMB,
trainTensors.timeSec,
{epochs: 10})
yield `Loss: ${h.history.loss[0]}`
}

Insert cell
train2 = {
const h = await model.fit(
trainTensors.sizeMB,
trainTensors.timeSec,
{epochs: 500})
yield `Loss: ${h.history.loss[0]}`
}
Insert cell
train3 = {
const h = await model.fit(
trainTensors.sizeMB,
trainTensors.timeSec,
{epochs: 1900})
yield `Loss: ${h.history.loss[0]}`
}
Insert cell
md ` ## make predictions`
Insert cell
predict = {
const smallFileMB = 1;
const bigFileMB = 100;
const hugeFileMB = 10000;

return model.predict(tf.tensor2d([[smallFileMB], [bigFileMB], [hugeFileMB]])).buffer().values

}
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