N-HiTS › Visualizing NHITS
Michael BrenndoerferAugust 23, 20255 min read
Machine Learning from ScratchAll chapters N-HiTS › Visualizing NHITSSection view · Sections in this chapter3 / 8 ↓
Sections in this chapter8
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]Expand
use_mls_style()VisualizationOut [7]Expand

PythonIn [8]Expand
# 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]Expand
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]Expand




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!