N-HiTS › Visualizing NHITS

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

Section 3 of 8 · 5 min read

N-HiTS › Visualizing NHITSSection view · Sections in this chapter3 / 8 ↓
Reading tools Definitions, links & notes

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

Notes & comments

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

PythonIn [6]
CollapseExpand
use_mls_style()
VisualizationOut [7]
CollapseExpand
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.
PythonIn [8]
CollapseExpand
# 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}"
)
ConsoleOut [8]
CollapseExpand
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])
VisualizationsOut [9]
CollapseExpand
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.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy