In articles that describe neural architectures with multiple attention layers of the same form, are the weight matrices usually the same across the layers? Consider as an example, "Attention is all you need". The authors stack several layers of multi-head self-attention in which each layer has the same number of heads. Each head $i$ involves a trainable weight matrix $W_{i}^{Q}$. There is no subscript, superscript, or any other indication that this matrix is different for each layer. My questions is this: are there separate $W_{i}^{Q}$ for layers $1,2,3,...$ or is this a single matrix shared throughout layers?

My intuition is that the authors of the paper wanted to cut down on notation, and that the matrices are different in different layers. But I want to be sure I understand this, since I see the same kind of thing in many other papers as well.

  • 2
    $\begingroup$ The official implementation of the initial transformer should have been done in tensor2tensor, as stated in the paper (if I remember correctly: please, check the footnotes of the paper, I think they say it there somewhere). Maybe it's this one: github.com/tensorflow/tensor2tensor/blob/master/tensor2tensor/…. You can check what they did there. You can then later provide an answer to your own question below ;) $\endgroup$
    – nbro
    Dec 16, 2020 at 23:06
  • $\begingroup$ It's a good idea, but one I already tried and didn't succeed at. I tried to read the code for both TensorFlow and PyTorch, and couldn't figure it out. $\endgroup$
    – BioBroo
    Dec 16, 2020 at 23:25

1 Answer 1


Weights are not normally shared across Transformer layers in vanilla Transformers. However, there has been research done in testing out sharing weights, and sometimes they improve the scores. Here are some examples:

ALBERT is an improvement on BERT (so only uses the encoding side, no decoder), and shows that sharing the attention weights only $\left\{ W_i^Q, W_i^K, W_i^V \right\}$ across all Transformer layers for large networks either result in the same accuracy or slightly improved accuracy, while significantly reducing model size. Sharing the position-wise FFN layer though hindered performance.

Text-to-Text Transfer Transformer shows that sharing the weights between encoder and decoder layer of the transformer (so e.g. layer 1 encoding weights = layer 1 decoding weights) barely affected accuracy (it dropped by 0.5%), but the model size is halved.

I'm sure there are more papers I have forgotten. Sharing weights is still an active area of research, but for vanilla transformers it is assumed they do not share weights.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .