Published
Edited
Jan 24, 2021
Insert cell
md`# Ice Cream Sales and Temperature AI Model - Tensorflow.js`
Insert cell
md`## Try the model`
Insert cell
viewof temperatureInput = html`<input type=number placeholder="Temperature">`
Insert cell
{
if (isNaN(temperatureInput)) {
return 'First insert a temperature above';
}
const xPredict = tf.tensor([temperatureInput])
const yPredict = model.predict(xPredict)
const revenueOutput = (await yPredict.data())[0]
return `Predicted Revenue: ${revenueOutput}`;
}
Insert cell
md`## Code`
Insert cell
tf = require('@tensorflow/tfjs')
Insert cell
file = FileAttachment("018 SalesData.csv")
Insert cell
dataset = file.csv()
Insert cell
xTrain = tf.tensor(dataset.map(el => parseFloat(el['Temperature'])))
Insert cell
yTrain = tf.tensor(dataset.map(el => parseFloat(el['Revenue'])))
Insert cell
mutable history = ({epoch: 0, loss: 0});
Insert cell
model = {
const model = tf.sequential({layers: [
tf.layers.dense({units: 1, inputShape: [1]})
]})
model.compile({optimizer: 'adam', loss: 'meanSquaredError'})
await model.fit(xTrain, yTrain, {epochs: 1000, callbacks: {onEpochEnd: (epoch, {loss}) => {
mutable history = {epoch, loss}
}}})
return model;
}
Insert cell
model.getWeights().toString()
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