Tutorial29 August 20221 min read
A small TensorFlow.js forecast, trained on dummy data
The public Next.js demo fits a two-layer network to five date-and-count points and charts twenty predicted days. It is a starting point, not a production model.
Steven Kamwaza
The repository tensorflowjs_nextjs_14__demo is a Next.js 14 app that depends on @tensorflow/tfjs and Recharts. The page trains a model while it renders, then draws the result.
The data
src/utils/dummy.js exports five points, each a date in November 2023 and a count. The page turns each date into "days since the first point" and uses that number as the input. The count is the output. There is no held-out test set.
The network
The model is a sequential stack of two dense layers with 100 units and a ReLU activation, then a dense layer with one unit. It compiles with stochastic gradient descent and mean squared error, and model.fit runs for 160 epochs on those five points.
After training, the page builds twenty future day-offsets, calls model.predict, rounds each value, and passes the series to PredictionGraph, an area chart.
What this is for
Five points and 160 epochs will memorize the sample. The chart is there so the training loop and the plot can be read in one place. It is not a forecast I would ship.
The useful part, for applied AI in JavaScript, is the shape of the loop: plain records in, tensors in, a small model, a chart of what came out. A later version needs a real series, a split between training and later dates, and a note of the error on those later dates.