liveBook · Manning

Transformers

As explained in Learn Generative AI with PyTorch

A software library for working with pretrained transformer models and tokenizers from Hugging Face.

Transformers is a Hugging Face software library for using pretrained transformer models and tokenizers. It can turn multilingual text into subword tokens or provide pretrained parameters for a model implemented in PyTorch. section 10.1.1 section 11.3.1

Common applications include:

  • Tokenize English and French with XLMTokenizer, including subword pieces such as “fr” and “ench,” then map tokens to vocabulary indexes. section 10.1.1
  • Load GPT-2XL through GPT2LMHeadModel.from_pretrained and extract its state dictionary for a compatible model. section 11.3.1

Loading a pretrained tokenizer

Install the library, create an XLM tokenizer with XLMTokenizer.from_pretrained("xlm-clm-enfr-1024"), and call tokenizer.tokenize("I don't speak French."). The result separates words and punctuation into reusable subword tokens.

Loading GPT-2XL parameters

Create GPT2LMHeadModel.from_pretrained('gpt2-xl'), obtain model_hf.state_dict(), and transpose selected Conv1d weights before copying them into a separately implemented linear-layer model.

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains installation, tokenizer calls, vocabulary indexing, state-dictionary extraction, and selected weight transpositions.
Examples
Uses English-French phrases, XLM token sequences, vocabulary indexes, and GPT-2XL weight-shape comparisons.
What it assumes
Assumes Python, pandas, PyTorch, and a custom transformer model when transferring weights.
What you can do after
Apply it via Hugging Face APIs to tokenize multilingual text or load pretrained GPT-2XL weights.
How it connects
Connects pretrained models with tokenization, subword tokenization, PyTorch, GPT-2, and machine translation.

Also explained in

Compare how 6 books explain transformers →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.