mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.14k stars 2.53k forks source link

Fix NBEATS first subtract block #395

Open mrdbourke opened 2 years ago

mrdbourke commented 2 years ago

The NBEATS architecture rebuild is slightly off (I think, as of today I haven't read the paper in almost a year).

There's a missing subtract connection (the red arrow below).

Screen Shot 2022-06-27 at 6 58 29 pm

cjtwigt pointed this out on Discord (text below is his quote):

Having said all this.. I think I have found an error in video lecture 336 where you build the NBEATS model. The way you have constructed the model the 2nd NBEATS block does not receive the model inputs via the first subtract layer (the direct line from the model inputs to the first subtract layer is missing). I have constructed it this way (and this way it beats the naive results by 0.5%)):

tf.random.set_seed(42)

nbeats_block_layer = NBeatsBlock(
    input_size=INPUT_SIZE,
    theta_size=THETA_SIZE,
    horizon=HORIZON,
    n_neurons=N_NEURONS,
    n_layers=N_LAYERS,
    name="InitialBlock"
)

inputs = layers.Input(shape=(WINDOW_SIZE,), name="stack_input")

residuals, forecast = nbeats_block_layer(inputs)

x = inputs

for i, _ in enumerate(range(N_STACKS-1)):
    x = layers.subtract([x, residuals], name=f"subtract{i}")
    residuals, block_forecast = NBeatsBlock(
        input_size=INPUT_SIZE,
        theta_size=THETA_SIZE,
        horizon=HORIZON,
        n_neurons=N_NEURONS,
        n_layers=N_LAYERS,
        name=f"NBeatsBlock{i}"
    )(x)
    forecast = layers.add([forecast, block_forecast], name=f"add{i}")

outputs = forecast

model_7 = tf.keras.Model(inputs, outputs, name="model_7_NBEATS")

Original paper: https://arxiv.org/abs/1905.10437

See the notebook it's related to here (see model_7 creation): https://github.com/mrdbourke/tensorflow-deep-learning/blob/main/10_time_series_forecasting_in_tensorflow.ipynb