NARX implementation using keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Nonlinear autoregressive models with exogenous inputs, or NARX models, predict a future output from two kinds of history: past values of the output itself and past values of external driving signals. In practice, a Keras implementation usually turns that idea into a supervised learning problem over lagged windows.
That means the hard part is usually not the neural network layer choice. It is building the training matrix so each sample contains the right slices of past output and exogenous input values.
What a NARX Model Uses
A NARX-style predictor estimates the next output from:
- previous output values
- previous exogenous input values
For example, if you are predicting system temperature, your model might use:
- past temperatures
- past heater power settings
- past ambient conditions
One common implementation does not use a special built-in Keras NARX layer. Instead, it creates lagged features and trains a standard dense network or sequence model.
Building Lagged Training Windows
Suppose y is the target series and u is the exogenous input series. We can prepare samples by concatenating the last ny values of y and the last nu values of u.
This converts a sequential problem into a normal supervised learning dataset.
Training a Keras Model
Once the lagged matrix is prepared, a small feedforward network is often enough for a basic NARX implementation:
This is a practical NARX-style model even though the network itself is a standard multilayer perceptron.
Open-Loop Versus Closed-Loop Thinking
During training, many NARX workflows use true past outputs from the dataset. That is sometimes called open-loop training. At inference time, if you predict multiple future steps, you may feed the model's own previous predictions back into the lag window. That becomes closed-loop forecasting.
The distinction matters because models often perform better in one-step prediction than in multi-step recursive forecasting. Error can accumulate once predicted outputs replace real historical outputs.
When to Use Recurrent Layers
You can also implement a NARX-like predictor with sequence models such as LSTM or GRU, but the conceptual core is the same: the model still needs access to output history and exogenous input history.
For many industrial or control-style NARX problems, explicitly engineered lag windows plus a dense network are easier to debug than a more opaque recurrent architecture.
Common Pitfalls
- A lag window that accidentally includes the target at time
tleaks future information and invalidates training results. - One-step prediction accuracy does not guarantee stable multi-step closed-loop forecasting.
- Random train-test splitting can mislead on time-series data; keep temporal order in validation.
- Using one arbitrary lag size for both output and exogenous inputs is convenient but not always appropriate.
Summary
- A NARX model predicts future output from past outputs and past exogenous inputs.
- In Keras, the usual implementation is a lagged supervised dataset plus a normal neural network.
- Data preparation is the critical step; the model only works if lag windows are built correctly.
- One-step training and recursive forecasting are different evaluation scenarios.
- A simple dense network is often a practical starting point for NARX-style prediction.
Related reading
- Need To Compile Keras Model Before model.evaluate
- Negative Binomial `Loss` in Neural Network using Tensorflow / Keras
- Negative dimension size caused by subtracting 3 from 1 for 'Conv2D
- Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution
- Nearest neighbors in high-dimensional data?
- Nearest neighbors in high-dimensional data?
- Neither PyTorch nor TensorFlow 2.0 have been found.Models won''t be available and only tokenizers, configuration and file/data utilities can be used
- No broadcasting for tf.matmul in TensorFlow
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.