N-HiTS › Visualizing NHITS
Michael BrenndoerferAugust 23, 20255 min read
Machine Learning from ScratchAll chapters Part 8 of 9Time Series
N-HiTS › Visualizing NHITSOne section · Sections in this chapter3 / 8 ↓
Sections in this chapter8
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]:
Codeuse_mls_style()Out[7]:
Visualization
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]:
ConsoleOriginal 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


