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 that is used at two places, with inputs and . The output is , and the loss is with target .
- Use the weight at the first place. Multiply the first input by : . Check: this is the only weight the step touches, and it is the same that the second place will use.
- Use the same weight at the second place. Multiply the second input by the same : . Check: no new parameter was introduced; the number 0.5 appears twice because the weight appears twice.
- Add the two uses and measure the error. The output is . The loss is . Check: the output is too small, so the loss is large.
- Send the error back to each use. The error signal at the output is . Each use passes it through its own input: at the first place and at the second. Check: both uses see the same error, but a larger input gives a larger share of the blame.
- Add the contributions to get the gradient for the one shared weight. The gradient is . Check: the shared weight should go up, since increasing it raises the output toward the target.
- Update the weight once. With learning rate , the new weight is . Check: with the output is , 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 be applied at places with inputs . The output is
Here indexes the places, and is the input seen at place . 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 be the loss, and let be the output of the -th use, so that . Changing changes every at once, so the total effect of 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:
The second equality holds because is the sum of the , so the error reaching each use is the same, . In the walkthrough, and , so the gradient is . 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 (a small positive number that sets how far each step moves):
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 parameters is applied at places. With sharing the total is . Without sharing, where each place has its own copy, the total is . Sharing therefore keeps the count fixed as 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 to an output of length , the count is . A shared kernel of size 3 with one bias, applied at every position, has parameters whatever is.
- dense layer, input length = output length
- shared kernel of size 3 with one bias
Worked examples
Take the input pair and , a shared weight , and the loss , so the error signal is .
- Forward, first use: .
- Forward, second use: .
- Add: .
- Gradient from the first use: .
- Gradient from the second use: .
- Add the two uses: .
Check: nudge up by 0.1 to . Then , which is up by , 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 .
Take the input and a kernel with three weights. The kernel slides over the input with stride 1 and no padding, so there are positions, and every position uses the same three weights. Suppose the error arriving at each output is .
- The gradient for collects the input seen by at each position: . Multiply by and add: .
- The gradient for collects . Multiply by and add: .
- The gradient for collects . Multiply by and add: .
- The gradient for the kernel is , 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, would get instead of , 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 parameters, while this kernel needs , with bias.
Consider a recurrent cell with input size and hidden size , as used in many speech and sensor models. Its parameters are the input matrix of size , the recurrent matrix of size , and a bias of length .
- Input matrix: weights.
- Recurrent matrix: weights.
- Bias: values.
- Total: parameters.
- Check: this total is the same whatever the sequence length, because the same cell is reused at each step.
- Compare with unshared copies: a sequence of steps with one copy per step would need parameters.
Interpretation: sharing reduces the cost by a factor of , 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 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 parameters, since sharing does not depend on . Unshared copies would need . 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 instead of for .
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.
| Situation | What goes wrong | Better choice |
|---|---|---|
| Position carries meaning, such as a face's features at fixed places | Shared filters ignore where a pattern is | Add positional information, or use unshared local layers |
| Each position has its own fixed role, such as the fields of a form | Sharing forces one rule where many are needed | Unshared layers, or a small set of learned rules per group |
| Long-range links between distant positions | A local shared rule needs many layers to connect them | Attention over all positions, as in a Transformer |
| Graph data where nodes have different neighbourhoods | Fixed grids do not apply | A Graph Neural Network with the same update at each node |
| Two inputs that must be compared | Separate networks produce outputs in different spaces | A 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
- Weight sharing applies one set of weights at many positions, time steps or branches, so the network learns a pattern once and uses it everywhere.
- The parameter count of a shared block does not depend on how many places it is used, which is why a Convolutional Neural Network or a Recurrent Neural Network can take inputs of any length.
- The gradient of a shared weight is the sum of its contributions from every use, and the update is applied once.
- Shared weights make a Siamese Network output comparable embeddings, and they make the search in Efficient Neural Architecture Search (ENAS) affordable.
- What to read next: Siamese Network for comparing two inputs, Efficient Neural Architecture Search (ENAS) for sharing across architectures, and Graph Neural Network for sharing across the nodes of a graph.
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 weights and biases, so parameters. The shared kernel has weights and one bias, so 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
- Goodfellow, Bengio, Courville, Deep Learning, Chapter 9: https://www.deeplearningbook.org/
- Pham, Guan, Zoph, Le, Dean (2018), Efficient Neural Architecture Search via Parameter Sharing: https://arxiv.org/abs/1802.03268
- Siamese neural network (Wikipedia)