Transformer
11 memberslucid.models.text.transformerOriginal Transformer family — Vaswani et al., 2017 (encoder-decoder seq2seq).
Vaswani, Ashish, et al. "Attention Is All You Need." Advances in Neural Information Processing Systems, 2017, pp. 5998–6008.
The Transformer replaces the recurrence of RNN-based seq2seq models with scaled dot-product attention, enabling fully parallel sequence processing. For query/key/value matrices , , and , attention is
where the factor counteracts the variance of dot products in high dimensions and keeps softmax gradients well-conditioned.
Multi-head attention runs parallel attention heads on learned projections of into subspaces, concatenates the outputs, and applies a final linear:
Each encoder layer stacks multi-head self-attention and a position-wise feed-forward network (with hidden width ), wrapped in residual connections and post-LayerNorm. Each decoder layer adds causal self-attention over previously generated targets and cross-attention over the encoder output.
Position information is injected via sinusoidal positional encodings of fixed (untrained) frequencies, which generalise to sequence lengths unseen during training. The paper's base model ( encoder/decoder layers, , ) achieves state-of-the-art WMT 2014 En-De / En-Fr BLEU while training in a fraction of the time of prior recurrent and convolutional seq2seq systems — the architectural foundation for essentially every modern language model.
Classes
TransformerConfig2 methodsConfiguration for every Vaswani-style Transformer variant.
TransformerForSeq2SeqLM3 methodsEncoder-decoder Transformer with a tied seq2seq language-modeling head.
TransformerForSequenceClassification2 methodsEncoder-only sentence classifier — pools the first source-side token.
TransformerForTokenClassification2 methodsEncoder-only per-token classifier — NER / POS / chunking heads.
TransformerModel7 methodsBare Vaswani encoder-decoder Transformer trunk.
Functions
transformer_base→ TransformerModelConstruct a Vaswani-style Transformer "base" encoder-decoder.
transformer_base_cls→ TransformerForSequenceClassificationConstruct a Transformer "base" with a sequence-classification head.
transformer_base_seq2seq→ TransformerForSeq2SeqLMConstruct a Transformer "base" with a tied seq2seq-LM head.
transformer_base_token_cls→ TransformerForTokenClassificationConstruct a Transformer "base" with a per-token classification head.
transformer_large→ TransformerModelConstruct a Vaswani-style Transformer "big" encoder-decoder.
transformer_large_seq2seq→ TransformerForSeq2SeqLMConstruct a Transformer "big" with a tied seq2seq-LM head.