Hypernetworks
A hypernetwork is a neural network that outputs the weights of another network from an input such as a task embedding. Covers uses and a PyTorch example.
A hypernetwork is a neural network whose output is the weights of another network, called the target or main network. Instead of learning one fixed set of model weights, the target network receives weights that the hypernetwork computes from a conditioning input, such as a task identifier, a user profile, a text description, or an example image, so a single trained hypernetwork can produce a different specialized model for each input in one forward pass. In the HyperNetworks paper from Google Brain in 2016, David Ha, Andrew Dai, and Quoc Le used small hypernetworks to generate the weights of deep convolutional and recurrent networks, and the idea now appears in continual learning, model personalization of diffusion models, and neural architecture search.
How Hypernetworks Work#
During training, only the hypernetwork's own parameters are learned. The conditioning input, often a learned embedding per task or layer, passes through the hypernetwork to produce the target network's weights, the target network processes the actual data with those weights, and backpropagation carries the loss gradient through the generated weights back into the hypernetwork:
Ha and colleagues distinguished static hypernetworks, which generate each layer's weights once from a layer embedding, from dynamic hypernetworks, which recompute weights for every input or time step, as their HyperLSTM did for each step of a sequence. Generated weights are ordinary tensors at run time, so the target network can be any architecture, including a transformer layer whose attention projections come from the hypernetwork. The main engineering problem is output size: a layer with millions of weights would need a hypernetwork with millions of outputs per layer. Practical designs generate the weights in chunks, each from its own chunk embedding, generate only small low-rank factors of each weight update, or generate only a per-channel scale and shift, as in feature-wise linear modulation (FiLM).
Hypernetwork Example in PyTorch#
This PyTorch example trains a hypernetwork on a family of tasks, sine waves with different amplitudes and phases. For each task, it generates all 97 weights of a small target network, and only the hypernetwork is trained:
import math
import torch
from torch import nn
torch.manual_seed(0)
x = torch.linspace(-math.pi, math.pi, 64)
# Hypernetwork: maps a task (amplitude, phase) to all 97 weights of a 1-32-1 target network
hyper = nn.Sequential(nn.Linear(2, 128), nn.ReLU(), nn.Linear(128, 97))
optimizer = torch.optim.Adam(hyper.parameters(), lr=1e-3)
def target(x, w):
"""Run the target network with weights w produced by the hypernetwork."""
return torch.relu(x[:, None] * w[:32] + w[32:64]) @ w[64:96] + w[96]
# Train only the hypernetwork, on a new random sine task at every step
for step in range(5000):
task = torch.rand(2) * torch.tensor([2.0, math.pi])
loss = ((target(x, hyper(task)) - task[0] * torch.sin(x + task[1])) ** 2).mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
# Unseen task: one forward pass of the hypernetwork produces the target network's weights
y = 1.5 * torch.sin(x + 1.0)
with torch.no_grad():
mse = ((target(x, hyper(torch.tensor([1.5, 1.0]))) - y) ** 2).mean()
print(f"Unseen task MSE: {mse:.3f} with generated weights, {(y**2).mean():.3f} predicting zero")It prints Unseen task MSE: 0.010 with generated weights, 1.132 predicting zero. The target network for amplitude 1.5 and phase 1.0 was never trained; its weights came from a single call to the hypernetwork, which learned how the right weights change across the whole task family.
Applications#
- Continual learning: A task-conditioned hypernetwork stores one small embedding per task and regenerates that task's weights on demand. Training on a new task includes a penalty that keeps the hypernetwork's outputs for earlier task embeddings unchanged, which counters catastrophic forgetting.
- Personalization of generative models: HyperDreamBooth predicts a small set of personalized weights for a text-to-image diffusion model from a single photo of a face and then refines them briefly, personalizing the model in about 20 seconds, 25 times faster than DreamBooth, with personalized weights 10,000 times smaller than a DreamBooth model.
- Neural architecture search: Graph hypernetworks read a candidate architecture as a graph and predict its weights, so candidates can be ranked by their validation accuracy with predicted weights instead of being trained one by one.
- Zero-shot classification: The authors of CLIP describe its text encoder as a hypernetwork that generates the weights of a linear classifier from the class names, which lets CLIP classify images into categories it never saw as training labels.
Open-vocabulary detectors apply the same idea to detection. Ultralytics YOLOE-26, built on YOLO26, encodes each class name passed to set_classes() once with MobileCLIP2, a compact CLIP-style text encoder, and uses the resulting embeddings as the weights that score each candidate region against each class. Export folds those embeddings into an ordinary 1x1 convolution in the detection head.
Hypernetworks vs. Related Terms#
- Fine-tuning: Adapts one fixed set of weights to a new task with gradient steps on that task's data. A trained hypernetwork produces weights for a new task in a single forward pass, given only the task's conditioning input.
- Meta-learning: The broader goal of learning to adapt quickly to new tasks. Hypernetworks are one way to implement it, mapping a task description or a few examples directly to weights.
- Mixture of experts (MoE): Routes each input to a few of many fixed expert sub-networks. A hypernetwork computes new weights for each input or task instead of selecting among stored ones.
- Fast weight programmers: Networks that write the short-term weights of another network as a sequence is processed, an idea from the early 1990s. Linearized self-attention in transformers is formally equivalent to such a fast weight programmer.










