N-HiTS › Visualizing NHITS

Michael BrenndoerferAugust 23, 20255 min read
Machine Learning from ScratchAll chapters
Part 8 of 9Time Series
Chapter 61N-HiTS › Visualizing NHITS
Reading section 3 of 8 · Visualizing NHITS · 5 minSection 3 of 8 · 5 min

Book contents

Section 3 of 8 · 5 min read

N-HiTS › Visualizing NHITSOne section · Sections in this chapter3 / 8 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Let's create visualizations to understand how NHITS works with hierarchical decomposition and neural interpolation.

In[6]:
Code
use_mls_style()
Out[7]:
Visualization
Multi-panel plot showing original time series and its hierarchical downsampled versions at different temporal resolutions.
Hierarchical decomposition of a synthetic time series showing how the original signal is downsampled at multiple levels. Each level captures patterns at different temporal resolutions, with higher levels (larger downsampling factors) revealing broader trends while filtering out high-frequency noise.
In[8]:
Code
# Demonstrate neural interpolation concept
class SimpleNHITSBlock(nn.Module):
    """Simplified NHITS block for demonstration"""

    def __init__(self, input_size, hidden_size, output_size):
        super().__init__()
        self.encoder = nn.Linear(input_size, hidden_size)
        self.processor = nn.Linear(hidden_size, hidden_size)
        self.decoder = nn.Linear(hidden_size, output_size)
        self.activation = nn.ReLU()

    def forward(self, x):
        h = self.activation(self.encoder(x))
        h = self.activation(self.processor(h))
        return self.decoder(h)


# Create a simple example
input_window = 10
forecast_horizon = 5
hidden_size = 32


# Generate training data
def create_sequences(data, window_size, forecast_horizon):
    X, y = [], []
    for i in range(len(data) - window_size - forecast_horizon + 1):
        X.append(data[i : i + window_size])
        y.append(data[i + window_size : i + window_size + forecast_horizon])
    return np.array(X), np.array(y)


# Create sequences for different hierarchical levels
X_orig, y_orig = create_sequences(x, input_window, forecast_horizon)
X_level2, y_level2 = create_sequences(
    hierarchical_data["Level 2"], input_window // 2, forecast_horizon // 2
)

# Convert to tensors
X_orig_tensor = torch.FloatTensor(X_orig)
y_orig_tensor = torch.FloatTensor(y_orig)
X_level2_tensor = torch.FloatTensor(X_level2)
y_level2_tensor = torch.FloatTensor(y_level2)

print(
    f"Original level - Input shape: {X_orig_tensor.shape}, Target shape: {y_orig_tensor.shape}"
)
print(
    f"Level 2 - Input shape: {X_level2_tensor.shape}, Target shape: {y_level2_tensor.shape}"
)
Out[8]:
Console
Original level - Input shape: torch.Size([986, 10]), Target shape: torch.Size([986, 5])
Level 2 - Input shape: torch.Size([494, 5]), Target shape: torch.Size([494, 2])
Out[9]:
Visualization
Original time series showing detailed temporal patterns.
Original time series segment showing fine-grained temporal patterns that will be processed through the hierarchical decomposition.
Coarsely downsampled version showing smoothed trend information.
Downsampled representation at Level 2.
Interpolation showing coarse values are repeated to match original resolution.
Interpolation process showing coarse-scale values are expanded back to original resolution through repetition.
Comparison of original and reconstructed signals from hierarchical processing.
Hierarchical reconstruction comparing the original signal with the reconstructed version from upsampled coarse predictions.