liveBook · Manning

Transformer architecture

As explained in Hugging Face in Action

The Transformer architecture is a neural-network architecture based on attention mechanisms for processing sequences and other structured inputs.

The Transformer architecture processes sequences with self-attention, allowing models to compare tokens in parallel and capture relationships across long distances. Its encoder builds input representations, while its decoder generates target sequences; either component can also operate alone. section 3.1

from transformers import pipeline

classifier = pipeline('text-classification',
             model = 'distilbert-base-uncased-finetuned-sst-2-english',
             revision = 'af0f99b')

Run a sentiment-classification workflow with Hugging Face’s high-level pipeline API and a pinned pretrained DistilBERT model. section 1.1

The architecture relies on several interacting ideas:

  • Self-attention weighs the importance of different words while encoding each word, capturing dependencies regardless of their distance. section 3.1
  • Token embeddings convert tokens into dense numerical vectors representing semantic and syntactic information. section 3.4
  • Positional encodings preserve word order because parallel processing does not inherently provide token positions. section 3.1
  • Encoder-only models suit sentence classification and named entity recognition; decoder-only models suit text generation; encoder-decoder models suit translation and summarization. section 3.1

Sentiment analysis with a pretrained model

Pass an IMDb review paragraph to the pipeline created with the DistilBERT sentiment model. The returned result identifies the review as positive, without requiring model training or detailed knowledge of text-classification internals. section 1.1

Hugging Face Transformers packages open-source Transformer implementations for text, image, and audio tasks. Its pipeline function wraps complex NLP workflows such as classification, named entity recognition, translation, and summarization behind a small high-level API. section 1.5

How Hugging Face in Action covers it

How deep it goes
Explains self-attention, embeddings, positions, and encoder-decoder roles briefly, while stopping short of detailed Transformer internals.
Examples
Demonstrates IMDb sentiment analysis with a pretrained DistilBERT model and Hugging Face’s pipeline API.
What you can do after
Apply it via Hugging Face pipelines to run pretrained models for classification and other NLP workflows.
How it connects
Connects self-attention, token embeddings, positional encodings, encoder-decoder models, recurrent neural networks, and NLP tasks.

Also explained in

Compare how 28 books explain transformer architecture →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hugging Face in Action. The section links point to the parts of the book it draws on.