Search

Search papers and lessons

Hypernetwork

Level 4 · 9 min read

A hypernetwork is a neural network whose output is the weights of another network, so changing its input changes how the target network behaves.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Probability
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

A hypernetwork is a neural network whose output is not a prediction but the weights of a second network, called the target. Feeding the hypernetwork a different code gives the target different weights, so one small model can produce many specialized models. Ha, Dai and Le introduced the term in 2016. The hypernetwork is trained end to end with Backpropagation, the same way any other network is.

Builds on: Backpropagation · Leads to: QMIX, ROME

Where it's used

  • Multi-agent value mixing. QMIX trains agents in reinforcement learning that each choose actions from their own observations, and combines their per-agent action values with a mixing network. QMIX's hypernetworks take the global state and output the weights of that mixing network, which is the hypernetwork pattern. Each hypernetwork passes its output through an absolute value, so the mixing weights are non-negative, and that keeps the mix monotonic in each agent's value.
  • Editing facts in a language model. The ROME paper notes that earlier hypernetwork editors learn a separate model to predict the weight changes needed to update one fact. ROME instead makes a direct rank-one edit to one layer, with no training, and reports that its approach is competitive with fine-tuning and hypernetwork editors on the zero-shot relation extraction (zsRE) benchmark.
  • Per-task or per-context behavior. When a model must behave differently for each task, a hypernetwork can generate the task's weights from a short task code, so the same trained generator serves every task. The price is the size of the generator, covered below.

How it works

A tiny instance first. The target is one linear layer with two weights, w1w_1 and w2w_2, that maps an input (x1,x2)(x_1, x_2) to the output y=w1x1+w2x2y = w_1 x_1 + w_2 x_2. The hypernetwork takes a code cc, which is 0 or 1, and outputs the two weights as (w1,w2)=Mc+b(w_1, w_2) = Mc + b, where M=(2,−1)M = (2, -1) and b=(0.5,1)b = (0.5, 1). The hypernetwork's parameters are MM and bb. Use the input (x1,x2)=(3,4)(x_1, x_2) = (3, 4).

  1. Choose code c=0c = 0. The hypernetwork computes the target weights w=M⋅0+b=(0.5,1)w = M \cdot 0 + b = (0.5, 1). Check: with code 0, the weights are just the bias bb.
  2. Apply the target to the input: the target takes the dot product of its weights with the input, y=0.5×3+1×4=1.5+4=5.5y = 0.5 \times 3 + 1 \times 4 = 1.5 + 4 = 5.5. Check: the output is a number, as any linear layer would give.
  3. Change only the code to c=1c = 1. The hypernetwork computes w=M⋅1+b=(2+0.5,−1+1)=(2.5,0)w = M \cdot 1 + b = (2 + 0.5, -1 + 1) = (2.5, 0). Check: the same hypernetwork, with the same parameters, now gives different weights.
  4. Apply the target to the same input: y=2.5×3+0×4=7.5+0=7.5y = 2.5 \times 3 + 0 \times 4 = 7.5 + 0 = 7.5. Check: the same input gives a different output because the target's weights changed.
  5. Training. Suppose the loss LL depends on yy. The gradient flows from the loss to the target weights ww, then through the hypernetwork's formula w=Mc+bw = Mc + b to MM and bb, as in gradient descent. Check: the hypernetwork is updated by the same chain rule as any network.
00.20.40.60.8102468code coutput y
  • output y for input (3, 4)
The same input gives 5.5 at code 0 and 7.5 at code 1, because the hypernetwork changes the target's weights with the code.

The math

Definition: Hypernetwork

A hypernetwork is a function hϕh_\phi with parameters ϕ\phi that maps a code zz to the weights θ\theta of a target network fθf_\theta. The combined model is fhϕ(z)(x)f_{h_\phi(z)}(x), and training adjusts ϕ\phi so that the loss on fhϕ(z)(x)f_{h_\phi(z)}(x) is small across the codes and inputs used.

Symbols in words: ϕ\phi holds the hypernetwork's learned numbers; θ\theta is the list of target weights; zz is the code; xx is the input to the target. In the linear case below, MM is a matrix of learned numbers and bb is a bias vector.

Step 1 in symbols

The target is fθ(x)=θ1x1+θ2x2f_\theta(x) = \theta_1 x_1 + \theta_2 x_2, so θ=(w1,w2)\theta = (w_1, w_2).

Step 2 in symbols

For a linear hypernetwork, hϕ(z)=Mz+bh_\phi(z) = Mz + b, so ϕ=(M,b)\phi = (M, b). With z=0z = 0, hϕ(0)=b=(0.5,1)h_\phi(0) = b = (0.5, 1).

Step 3 in symbols

With z=1z = 1, hϕ(1)=M+b=(2,−1)+(0.5,1)=(2.5,0)h_\phi(1) = M + b = (2, -1) + (0.5, 1) = (2.5, 0).

Step 5 in symbols

Let LL be the loss and θ=hϕ(z)\theta = h_\phi(z). The chain rule gives ∂L∂M=∂L∂θ z⊤,∂L∂b=∂L∂θ.\frac{\partial L}{\partial M} = \frac{\partial L}{\partial \theta}\, z^{\top}, \qquad \frac{\partial L}{\partial b} = \frac{\partial L}{\partial \theta}. So the update to MM is the gradient on the generated weights times the code, and the update to bb is the gradient on the generated weights themselves.

Worked examples

Example
1. Tiny: one generated weight

The target computes y=wxy = w x. The hypernetwork is w=mz+bw = m z + b with m=3m = 3, b=1b = 1. Input x=2x = 2.

  1. Code z=0z = 0. Then w=3×0+1=1w = 3 \times 0 + 1 = 1.
  2. Output y=1×2=2y = 1 \times 2 = 2.
  3. Code z=2z = 2. Then w=3×2+1=7w = 3 \times 2 + 1 = 7.
  4. Output y=7×2=14y = 7 \times 2 = 14.
  5. Interpretation: one scalar code changes the target's gain from 1 to 7, so the same input gives 2 or 14.
Example
2. Standard: the lesson's two-weight layer with two codes

Use the tiny instance above: M=(2,−1)M = (2, -1), b=(0.5,1)b = (0.5, 1), input (3,4)(3, 4).

  1. For c=0c = 0: w=(0.5,1)w = (0.5, 1), output 0.5×3+1×4=5.50.5 \times 3 + 1 \times 4 = 5.5.
  2. For c=1c = 1: w=(2.5,0)w = (2.5, 0), output 2.5×3+0×4=7.52.5 \times 3 + 0 \times 4 = 7.5.
  3. The difference between codes is 7.5−5.5=2.07.5 - 5.5 = 2.0, which is M⋅x=2×3+(−1)×4=6−4=2M \cdot x = 2 \times 3 + (-1) \times 4 = 6 - 4 = 2.
  4. Interpretation: changing the code by 1 shifts every weight by MM, so the output changes by M⋅xM \cdot x.
Example
3. Realistic: a generator for a large layer

A model uses one 512 by 512 layer per task, and a hypernetwork generates each task's layer from a 16-number task code. Both the target and the hypernetwork are linear.

  1. The target layer has 512×512=262,144512 \times 512 = 262{,}144 weights, the numbers the hypernetwork must output for each task.
  2. The linear hypernetwork maps a 16-number code to 262,144 outputs, so its matrix has 16×262,144=4,194,30416 \times 262{,}144 = 4{,}194{,}304 entries.
  3. Add the bias of 262,144 numbers: the hypernetwork has 4,194,304+262,144=4,456,4484{,}194{,}304 + 262{,}144 = 4{,}456{,}448 parameters.
  4. Interpretation: the generator has about 17 times as many parameters as the layer it generates. Storing the layer directly would be cheaper, so this design only pays off with a smaller generator or a bottleneck layer.

Try it: a target layer of 256 by 256 weights, a hypernetwork from an 8-number code, linear, with bias. How many parameters does the hypernetwork have? Answer: 8×65,536+65,536=524,288+65,536=589,8248 \times 65{,}536 + 65{,}536 = 524{,}288 + 65{,}536 = 589{,}824.

Common mistakes and tradeoffs

It is tempting to think a hypernetwork is just a larger network that already contains the target. In fact the target's weights do not exist until the hypernetwork outputs them for a given code, so the target's behavior is set by the hypernetwork's output, not by stored weights.

It is tempting to think hypernetworks always save parameters. In fact, as the realistic example shows, generating a large layer from a small code can need far more parameters than the layer itself. The saving comes only when the generator is small relative to the target.

It is tempting to think training is different from ordinary networks. In fact it is the same backpropagation, with the gradient passing through the generated weights into the hypernetwork's parameters.

Use a hypernetwork when the target's weights should depend on a context or task, and when the target is small enough that the generator stays manageable. Do not use it when a single model with the code as an extra input would work, because that is simpler; an embedding layer can turn the code into a vector for that purpose. Alternatives include feeding the context as an input, separate heads per task, and low-rank adaptation, which learns a small correction to a frozen weight matrix instead of generating the whole matrix.

History

Networks that write the weights of other networks were explored before 2016, but the 2016 paper gave the idea its name. Ha, Dai and Le named and popularized hypernetworks in 2016, applying them to recurrent networks among other models, and the paper was presented at ICLR 2017. Its citation in later work, including the QMIX and ROME papers, shows the term caught on across reinforcement learning and model editing.

What made it catch on was its simplicity: a standard network, trained with standard backpropagation, produces the weights of another network from a short code. Its cost, the size of the generator, limited it to cases where the generated network is small or the code is used in a bottleneck.

Recap

  • A hypernetwork's output is the weights of another network.
  • Changing the code changes the target's weights, so one trained generator can serve many targets.
  • Training uses ordinary backpropagation; the gradient reaches the hypernetwork through the generated weights.
  • The generator can be larger than the target, so the size trade must be checked.
  • It is used in multi-agent value mixing (QMIX) and in model editing (ROME discusses earlier hypernetwork editors).

Read next: the QMIX paper for a hypernetwork used as a mixing network, and the ROME paper for a comparison with hypernetwork editors.

Check yourself

QuestionRecall: what does a hypernetwork output, and what does the target network do with it?Show answer

A hypernetwork outputs the weights of a target network. The target applies those weights to its input, so the hypernetwork determines the target's behavior.

QuestionComputation: a linear hypernetwork has M=(1,2)M = (1, 2) and b=(0,1)b = (0, 1). What weights does it produce for code c=3c = 3?Show answer

w=Mc+b=(1×3+0,2×3+1)=(3,7)w = Mc + b = (1 \times 3 + 0, 2 \times 3 + 1) = (3, 7).

QuestionWhy: why does a hypernetwork not always save parameters compared with a direct layer?Show answer

The hypernetwork must output every weight of the target, so its size grows with the target's size times the code's size. If the code is not much smaller than the number of generated weights, the generator can have more parameters than the target layer itself.

QuestionApplied scenario: a team wants one model to behave differently for each of 50 customers, with a small customer code. A hypernetwork generating a full 1000 by 1000 layer seems too big. What would you check first?Show answer

Check the generator size. Generating 10610^6 weights from a code of length dd needs about d×106d \times 10^6 parameters for a linear generator, which is large for any useful dd. A better first step is to try a bottleneck, a low-rank correction such as LoRA, or feeding the customer code as an input to the existing layer.

Sources