Week 10 — Neural Networks & Deep Learning for Forecasting
This final notebook introduces neural networks for time-series prediction: feedforward networks, recurrent neural networks (RNNs), and LSTMs — the frontier of forecasting methodology.
import matplotlib as mplimport matplotlib.pyplot as pltimport numpy as np# Edinburgh paletteUOE_RED ='#7A2318'UOE_GOLD ='#B8860B'UOE_BLUE ='#2a78d6'UOE_GREY ='#52514e'COLOURS = [UOE_RED, UOE_BLUE, UOE_GOLD, '#2ca02c', '#9467bd', '#e377c2']mpl.rcParams.update({'figure.figsize': (10, 5),'axes.prop_cycle': mpl.cycler(color=COLOURS),'axes.spines.top': False, 'axes.spines.right': False,'axes.labelsize': 12, 'axes.titlesize': 14,'font.size': 11, 'legend.fontsize': 10,'lines.linewidth': 2,})print("Plotting style set ✓")
Plotting style set ✓
10.1 From Linear Models to Neural Networks
A feedforward neural network with one hidden layer:
\[\hat{y} = W_2 \cdot \sigma(W_1 x + b_1) + b_2\]
where \(\sigma\) is an activation function (ReLU, sigmoid, tanh).
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1786350683.666990 5439 port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
I0000 00:00:1786350683.667999 5439 cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
I0000 00:00:1786350683.729615 5439 cpu_feature_guard.cc:227] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F AVX512_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
I0000 00:00:1786350685.805724 5439 port.cc:153] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
I0000 00:00:1786350685.806251 5439 cudart_stub.cc:31] Could not find cuda drivers on your machine, GPU will not be used.
/usr/local/lib/python3.11/dist-packages/keras/src/layers/core/dense.py:107: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
E0000 00:00:1786350687.434860 5439 cuda_platform.cc:52] failed call to cuInit: INTERNAL: CUDA error: Failed call to cuInit: UNKNOWN ERROR (303)
10.4 Recurrent Neural Networks (RNNs) and LSTMs
RNNs process sequences by maintaining a hidden state:
\[h_t = \sigma(W_h h_{t-1} + W_x x_t + b)\]
LSTMs add gating mechanisms to handle long-range dependencies.
/usr/local/lib/python3.11/dist-packages/keras/src/layers/rnn/rnn.py:199: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(**kwargs)