DL0039 Transformer Weight Tying

Explain weight sharing in Transformers.

Answer

Weight sharing (weight tying) reuses the same parameters in multiple places instead of learning separate matrices. The most common form ties the input embedding matrix with the output projection that produces vocabulary logits: one matrix E maps token IDs into d-dimensional space at the bottom and maps hidden states back to logits at the top. A stronger form (ALBERT) shares weights across all Transformer layers, trading capacity for extreme parameter efficiency.

(1) Embedding–Output Tying: The output logits are computed with the transpose of the input embedding, z = Eh, saving an entire K \times d matrix.
(2) Consistency Benefit: Input and output spaces share one geometry: tokens that embed close together also get similar output preferences, which acts as a useful regularizer.
(3) Layer Sharing (ALBERT): One block’s weights are reused for every layer: depth comes from recurrence through the same block, not from new parameters.

Mathematical Formulation:
\mathrm{Softmax}(z_i) = \frac{e^{z_i}}{\sum_{j=1}^{K} e^{z_j}}
z = E\, h

Where:

  • E \in \mathbb{R}^{K \times d} is the shared embedding matrix, used to look up inputs and, transposed, to score outputs.
  • h \in \mathbb{R}^{d} is the final hidden state; z_i = (Eh)_i is the logit of vocabulary token i; K is vocabulary size.
Diagram of the shared embedding matrix E sitting at the input lookup stage and again transposed at the output projection stage, with an arc labeled weight tying connecting the two uses around the Transformer stack.

Figure 1: One matrix, two jobs: E embeds input tokens at the bottom and, transposed, projects hidden states to logits at the top.

How Much It Saves: The untied output projection is a full K \times d matrix; for a 50k vocabulary at d = 1024 that is ~51M parameters, often the single largest matrix in a small model. Tying removes it entirely while typically costing little or no accuracy.

Bar chart comparing parameter counts of untied versus tied configurations, showing the output projection matrix of fifty million parameters eliminated by tying.

Figure 2: Tying deletes the output projection matrix, one of the largest single blocks of parameters in vocabulary-heavy models.

Vocabulary Mismatch Caveat: Tying assumes input and output share a vocabulary. When source and target vocabularies differ (multilingual translation), only the decoder input embedding and output projection can be tied; the encoder embedding stays separate.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *