Search

Search papers and lessons

Weight Sharing

Level 3 · 13 min read

Weight sharing reuses one set of weights at many positions, time steps or branches, so a pattern is learned once and the parameter count stays fixed as the input grows.

On this page

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

TL;DR

Weight sharing means that one set of weights is used in several places: at every position of an image, at every step of a sequence, or in every branch of a network. Each use sees a different input, so the network can detect the same pattern wherever it appears, and the number of parameters does not grow with the length of the input. The gradient of a shared weight is the sum of its contributions from every place it was used.

Builds on: Convolutional Neural Network, Recurrent Neural Network · Leads to: Siamese Network, Efficient Neural Architecture Search (ENAS), Graph Neural Network

Where it's used

Image recognition. A Convolutional Neural Network applies one small filter at every position of a picture. The 2012 ImageNet network that began the deep learning wave used this design, and its filters were reused across the whole image rather than learned separately for each location.

Language and sequence models. A Recurrent Neural Network applies the same update rule at every time step. A character-level model uses the same weights for every character it reads, so the model can read text of any length without growing.

Signature and face verification. A Siamese Network passes two inputs through two copies of one network that share their weights, then compares the outputs. In the early 1990s researchers used this design to check whether a handwritten signature matched a stored one, and face verification systems today use the same idea. Because both inputs pass through identical weights, their outputs live in the same space and a distance between them is meaningful.

Neural architecture search. Searching for a good network design normally means training thousands of candidate networks from scratch. Efficient Neural Architecture Search (ENAS) trains one large network that contains all the candidates as sub-graphs, and each candidate reuses the shared weights. The search then costs a small fraction of what training every candidate separately would cost.

How it works

Take one weight w=0.5w = 0.5 that is used at two places, with inputs x1=1x_1 = 1 and x2=2x_2 = 2. The output is y=wx1+wx2y = w x_1 + w x_2, and the loss is L=12(y−t)2L = \tfrac{1}{2}(y - t)^2 with target t=4t = 4.

  1. Use the weight at the first place. Multiply the first input by ww: wx1=0.5×1=0.5w x_1 = 0.5 \times 1 = 0.5. Check: this is the only weight the step touches, and it is the same ww that the second place will use.
  2. Use the same weight at the second place. Multiply the second input by the same ww: wx2=0.5×2=1.0w x_2 = 0.5 \times 2 = 1.0. Check: no new parameter was introduced; the number 0.5 appears twice because the weight appears twice.
  3. Add the two uses and measure the error. The output is y=0.5+1.0=1.5y = 0.5 + 1.0 = 1.5. The loss is 12(1.5−4)2=12×6.25=3.125\tfrac{1}{2}(1.5 - 4)^2 = \tfrac{1}{2} \times 6.25 = 3.125. Check: the output is too small, so the loss is large.
  4. Send the error back to each use. The error signal at the output is y−t=−2.5y - t = -2.5. Each use passes it through its own input: −2.5×1=−2.5-2.5 \times 1 = -2.5 at the first place and −2.5×2=−5.0-2.5 \times 2 = -5.0 at the second. Check: both uses see the same error, but a larger input gives a larger share of the blame.
  5. Add the contributions to get the gradient for the one shared weight. The gradient is −2.5+(−5.0)=−7.5-2.5 + (-5.0) = -7.5. Check: the shared weight should go up, since increasing it raises the output toward the target.
  6. Update the weight once. With learning rate η=0.1\eta = 0.1, the new weight is w=0.5−0.1×(−7.5)=1.25w = 0.5 - 0.1 \times (-7.5) = 1.25. Check: with w=1.25w = 1.25 the output is 1.25×1+1.25×2=3.751.25 \times 1 + 1.25 \times 2 = 3.75, closer to 4 than 1.5 was.
flowchart LR
  x1["input 1 = 1"] -- "times w (same w)" --> y["output y = 0.5 + 1.0 = 1.5"]
  x2["input 2 = 2"] -- "times w (same w)" --> y
  y --> L["loss = 3.125"]

The network stored one number, but it was used twice and its gradient collected both uses. That is the whole idea in miniature.

The math

The walkthrough has three parts: a forward pass that uses the weight in several places, a gradient that adds up those uses, and an update that changes the single shared value. The parameter count follows from the same structure.

Step 1 in symbols: one weight, several uses

Let a single weight ww be applied at nn places with inputs x1,…,xnx_1, \dots, x_n. The output is

y=∑k=1nw xk.y = \sum_{k=1}^{n} w\, x_k.

Here kk indexes the places, and xkx_k is the input seen at place kk. In a convolution the places are positions in the image, in a recurrent network they are time steps, and in a siamese network they are the two branches. The formula has the same shape in each case.

Step 2 in symbols: the gradient adds the uses

Let L\mathcal{L} be the loss, and let sk=w xks_k = w\,x_k be the output of the kk-th use, so that y=∑ksky = \sum_k s_k. Changing ww changes every sks_k at once, so the total effect of ww on the loss is the sum of its effects through each use, which is the Chain Rule applied to a weight that appears more than once:

∂L∂w=∑k=1n∂L∂sk ∂sk∂w=∑k=1n∂L∂y xk=∂L∂y∑k=1nxk.\frac{\partial \mathcal{L}}{\partial w} = \sum_{k=1}^{n} \frac{\partial \mathcal{L}}{\partial s_k}\,\frac{\partial s_k}{\partial w} = \sum_{k=1}^{n} \frac{\partial \mathcal{L}}{\partial y}\, x_k = \frac{\partial \mathcal{L}}{\partial y} \sum_{k=1}^{n} x_k.

The second equality holds because yy is the sum of the sks_k, so the error reaching each use is the same, ∂L/∂y\partial \mathcal{L}/\partial y. In the walkthrough, ∂L/∂y=−2.5\partial \mathcal{L}/\partial y = -2.5 and ∑kxk=3\sum_k x_k = 3, so the gradient is −7.5-7.5. This is the rule that Backpropagation computes automatically for any weight that is reused.

Step 3 in symbols: one update for the shared weight

Training with Stochastic Gradient Descent changes the shared weight once per step, using a learning rate η>0\eta > 0 (a small positive number that sets how far each step moves):

w←w−η ∂L∂w.w \leftarrow w - \eta\,\frac{\partial \mathcal{L}}{\partial w}.

There is one update, not one per place. Updating the weight separately at each use would be a different and usually wrong procedure, because each use would then move the weight by only its own share of the error.

Step 4 in symbols: the parameter count

Suppose a block with pp parameters is applied at nn places. With sharing the total is pp. Without sharing, where each place has its own copy, the total is n pn\,p. Sharing therefore keeps the count fixed as nn grows. For a dense layer (a fully connected layer, where every input has its own weight to every output) that maps an input of length LL to an output of length LL, the count is L2+LL^2 + L. A shared kernel of size 3 with one bias, applied at every position, has 3+1=43 + 1 = 4 parameters whatever LL is.

51015200100200300400input length Lparameters
  • dense layer, input length = output length
  • shared kernel of size 3 with one bias
The dense count grows with the square of the input length, while the shared kernel's count stays at 4. The two points mark L = 6.

Worked examples

Example
Tiny: one weight used twice

Take the input pair x1=2x_1 = 2 and x2=−1x_2 = -1, a shared weight w=3w = 3, and the loss L=yL = y, so the error signal is ∂L/∂y=1\partial L/\partial y = 1.

  1. Forward, first use: 3×2=63 \times 2 = 6.
  2. Forward, second use: 3×(−1)=−33 \times (-1) = -3.
  3. Add: y=6+(−3)=3y = 6 + (-3) = 3.
  4. Gradient from the first use: 1×2=21 \times 2 = 2.
  5. Gradient from the second use: 1×(−1)=−11 \times (-1) = -1.
  6. Add the two uses: ∂L/∂w=2+(−1)=1\partial L/\partial w = 2 + (-1) = 1.

Check: nudge ww up by 0.1 to 3.13.1. Then y=3.1×2+3.1×(−1)=6.2−3.1=3.1y = 3.1 \times 2 + 3.1 \times (-1) = 6.2 - 3.1 = 3.1, which is up by 0.10.1, exactly the gradient of 1 times the change of 0.1.

Interpretation: the second use pulls the gradient down, so the two uses partly cancel. Summing them is the only way the shared weight learns the true effect of changing ww.

Example
Standard: a shared 1D kernel over a sequence of six values

Take the input x=[1,2,0,3,1,2]x = [1, 2, 0, 3, 1, 2] and a kernel K=[K0,K1,K2]K = [K_0, K_1, K_2] with three weights. The kernel slides over the input with stride 1 and no padding, so there are 6−3+1=46 - 3 + 1 = 4 positions, and every position uses the same three weights. Suppose the error arriving at each output is g=[1,0,−1,2]g = [1, 0, -1, 2].

  1. The gradient for K0K_0 collects the input seen by K0K_0 at each position: x0,x1,x2,x3=1,2,0,3x_0, x_1, x_2, x_3 = 1, 2, 0, 3. Multiply by gg and add: 1⋅1+0⋅2+(−1)⋅0+2⋅3=71 \cdot 1 + 0 \cdot 2 + (-1) \cdot 0 + 2 \cdot 3 = 7.
  2. The gradient for K1K_1 collects x1,x2,x3,x4=2,0,3,1x_1, x_2, x_3, x_4 = 2, 0, 3, 1. Multiply by gg and add: 1⋅2+0⋅0+(−1)⋅3+2⋅1=11 \cdot 2 + 0 \cdot 0 + (-1) \cdot 3 + 2 \cdot 1 = 1.
  3. The gradient for K2K_2 collects x2,x3,x4,x5=0,3,1,2x_2, x_3, x_4, x_5 = 0, 3, 1, 2. Multiply by gg and add: 1⋅0+0⋅3+(−1)⋅1+2⋅2=31 \cdot 0 + 0 \cdot 3 + (-1) \cdot 1 + 2 \cdot 2 = 3.
  4. The gradient for the kernel is [7,1,3][7, 1, 3], one number per shared weight.

Check: each kernel weight was used at all four positions, so each gradient has four terms. Had we used only the last position, K0K_0 would get 2⋅3=62 \cdot 3 = 6 instead of 77, and the update would be wrong.

Interpretation: the kernel was stored as three numbers, but its gradient is built from twelve products. A dense layer on the same six inputs would need 6×4+4=286 \times 4 + 4 = 28 parameters, while this kernel needs 3+1=43 + 1 = 4, with bias.

Example
Realistic: a recurrent cell over long sequences

Consider a recurrent cell with input size d=128d = 128 and hidden size m=256m = 256, as used in many speech and sensor models. Its parameters are the input matrix WhxW_{hx} of size m×dm \times d, the recurrent matrix WhhW_{hh} of size m×mm \times m, and a bias bh\mathbf{b}_h of length mm.

  1. Input matrix: 256×128=32,768256 \times 128 = 32{,}768 weights.
  2. Recurrent matrix: 256×256=65,536256 \times 256 = 65{,}536 weights.
  3. Bias: 256256 values.
  4. Total: 32,768+65,536+256=98,56032{,}768 + 65{,}536 + 256 = 98{,}560 parameters.
  5. Check: this total is the same whatever the sequence length, because the same cell is reused at each step.
  6. Compare with unshared copies: a sequence of T=1,000T = 1{,}000 steps with one copy per step would need 1,000×98,560=98,560,0001{,}000 \times 98{,}560 = 98{,}560{,}000 parameters.

Interpretation: sharing reduces the cost by a factor of TT, and the savings grow with the sequence. The network reads a 1,000-step sequence with the same 98,560 numbers it would use for a 2-step sequence.

Try it. Sensor logs are now sampled over T=4,000T = 4{,}000 steps with the same cell. How many parameters does the recurrent cell have, and how many would unshared copies need?

Answer: the cell still has 98,56098{,}560 parameters, since sharing does not depend on TT. Unshared copies would need 4,000×98,560=394,240,0004{,}000 \times 98{,}560 = 394{,}240{,}000. Interpretation: the parameter count is set by the cell's size, and the sequence length only changes the amount of computation.

Common mistakes and tradeoffs

Misconception 1: shared weights give every position the same output. It is tempting to think that if the filter is the same, the outputs are the same. In fact the weights are shared but the inputs differ, so each position gives a different value. Sharing constrains the function the network can compute, not the numbers it produces.

Misconception 2: the gradient of a shared weight comes from one place. It is tempting to think that only the last use matters, or that each use updates the weight by its own amount. In fact the gradient is the sum over all uses. Example 2 shows that using only one position gives a wrong gradient of 66 instead of 77 for K0K_0.

Misconception 3: sharing is always an improvement. It is tempting to think that fewer parameters always help. In fact sharing assumes that the same pattern matters everywhere. When position carries meaning, such as the eyes being at the top of a face, sharing alone ignores it. Sharing also reduces overfitting (see Overfitting), but a model with too little capacity cannot fit the data at all.

When to use it. Use sharing when the same local rule should apply across positions, times or branches, and when the input length varies. Do not use it when each position has its own role, when the data is too short for even the shared model to learn from, or when you need the same network to treat two inputs differently.

SituationWhat goes wrongBetter choice
Position carries meaning, such as a face's features at fixed placesShared filters ignore where a pattern isAdd positional information, or use unshared local layers
Each position has its own fixed role, such as the fields of a formSharing forces one rule where many are neededUnshared layers, or a small set of learned rules per group
Long-range links between distant positionsA local shared rule needs many layers to connect themAttention over all positions, as in a Transformer
Graph data where nodes have different neighbourhoodsFixed grids do not applyA Graph Neural Network with the same update at each node
Two inputs that must be comparedSeparate networks produce outputs in different spacesA siamese design with shared weights

History

Kunihiko Fukushima's Neocognitron, described in 1980, used the same set of connection weights for every position within a layer, so a detector for one shape worked anywhere in the image. Its training rule was not backpropagation, but the design already had the main idea.

In 1989 Alex Waibel and colleagues used the same idea across time for speech. Their time-delay network applied one set of weights to a sliding window of the input, so a sound was recognised wherever it occurred in the signal. Yann LeCun's convolutional networks, trained with backpropagation, followed the same design for images.

Jane Bromley and colleagues, in 1993, used two copies of one network with shared weights to decide whether two signatures came from the same person. This is the siamese design. Recurrent networks, with their shared update at each time step, are the third early case, and by the 2010s weight sharing was standard in convolutional, recurrent and siamese models.

In 2018 Hieu Pham and colleagues made weight sharing the core of architecture search. Their method, ENAS, trained one network whose sub-graphs stood for candidate designs, which made the search far cheaper than training each candidate from scratch.

Recap

Check yourself

QuestionRecall: what are the two main places where a convolutional network reuses weights, and what does each reuse give the network?Show answer

A convolutional filter is reused at every spatial position of the input, so a pattern is detected wherever it appears. In a recurrent network the same cell is reused at every time step, so one set of weights handles sequences of any length. Each reuse keeps the parameter count fixed as the input grows.

QuestionComputation: a dense layer maps an input of length 10 to an output of length 10, with biases. A shared kernel of size 5 with one bias is used instead. How many parameters does each have?Show answer

The dense layer has 10×10=10010 \times 10 = 100 weights and 1010 biases, so 110110 parameters. The shared kernel has 55 weights and one bias, so 66 parameters.

QuestionWhy: why does the gradient of a shared weight add up the contributions from every place it is used?Show answer

Changing the shared weight changes its output at every place at once, so its total effect on the loss is the sum of those separate effects. The chain rule therefore gives a sum over all the uses. Taking only one use would ignore part of how the weight changes the loss, so the update would point in the wrong direction.

QuestionApplied: a company wants to check whether two product photos show the same item, and it has millions of items with no fixed list of classes. Would you use weight sharing, and what would you check?Show answer

Yes. A siamese design passes both photos through the same network with shared weights, so both are mapped into one embedding space and their distance is meaningful. New items then need no retraining. I would check that the distance threshold is set on held-out pairs of matching and non-matching photos, and that the model works on the lighting and angles the company actually receives, since a shared model can still fail on inputs unlike its training data.

Sources