Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Sebastian Raschka

Rating No ratings yet

从零开始构建大型语言模型是为了帮助你理解和创建自己的类似GPT的大型语言 模型(LLMs)。本书首先专注于文本数据处理和编码注意力机制的基础知识,然 后引导你从头实现一个完整的GPT模型。书中还涵盖了预训练机制以及针对特定 任务(如文本分类和遵循指令)的微调。读完这本书后,你将深入了解LLMs的工 作原理,并具备构建自己模型的技能。虽然你创建的模型规模较小,但它们使用相同的概念,作为掌握构建最先进的LLMs核心机制和技术的强大教育工具。

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A hands-on guide that walks you through building a GPT-style large language model from scratch in PyTorch, covering data preparation, attention, pretraining, and fine-tuning. Best for Python-proficient learners, engineers, and researchers who want to understand LLMs by writing the code themselves rather than treating them as black boxes. 【Book Arc】 - **Opening (~0%–14%)**: Introduces what LLMs are, where they sit within AI/ML/deep learning, and the three-stage build plan (architecture + data prep → pretraining → fine-tuning). Frames the Transformer and the encoder-vs-decoder distinction (BERT vs GPT). - **Early (~14%–29%)**: Text processing fundamentals — tokenization, token IDs, special tokens, byte pair encoding, sliding-window sampling, token embeddings, and positional encoding. This stage solves the "how do we turn raw text into model-ready tensors" problem. - **Early–Middle (~29%–43%)**: The attention mechanism, built up step by step: simple self-attention, trainable self-attention, causal masking, dropout, and multi-head attention. This is the conceptual core of the Transformer. - **Middle (~43%–57%)**: Assembling a full GPT model — layer normalization, GELU feed-forward networks, shortcut connections, Transformer blocks, and text generation. Also covers parameter counts and storage requirements. - **Late (~57%–86%)**: Pretraining on unlabeled data — loss evaluation, training loops, decoding strategies (temperature, top-k), saving/loading weights, and loading OpenAI pretrained weights. Then fine-tuning for classification (e.g., spam detection). - **Ending (~86%–100%)**: Instruction fine-tuning — preparing supervised instruction datasets, batching, fine-tuning to follow human instructions, extracting and evaluating responses, plus closing thoughts on next steps and keeping up with the field. 【Key Takeaways】 - **The book follows a three-stage LLM build** (Opening): architecture and data preparation, pretraining to create a base model, then fine-tuning into a classifier or instruction-follower. This structure mirrors how real LLMs are actually developed. - **Tokenization is the foundation of everything downstream** (Early): byte pair encoding, special context tokens, and sliding-window sampling determine what the model actually sees. Getting this wrong undermines all later stages. - **Attention is built incrementally, not assumed** (Early–Middle): the book starts from a training-free self-attention example and progressively adds trainable weights, causal masks, dropout, and multi-head stacking. This makes the hardest concept approachable. - **A GPT model is assembled from understandable parts** (Middle): layer normalization, GELU activations, shortcut connections, and Transformer blocks are each implemented separately before being combined. This demystifies the architecture. - **Pretraining uses self-supervised next-word prediction** (Late): no labels are needed — the model generates its own training signal from raw text. Loss on training and validation sets measures generation quality. - **Decoding strategy controls output randomness** (Late): temperature scaling and top-k sampling let you trade off between deterministic and creative text generation. - **Fine-tuning splits into two practical paths** (Late–Ending): classification fine-tuning (e.g., spam detection with a classification head) and instruction fine-tuning (supervised instruction-answer pairs). Both reuse pretrained weights. - **Models are small but conceptually faithful** (Throughout): the book emphasizes that the small models you build use the same mechanisms as state-of-the-art LLMs, making them powerful educational tools. 【Reading Tips】 - **Read sequentially** — each chapter builds directly on the previous one, and skipping ahead will leave gaps in the implementation chain. - **Deep-read Chapters 2–4** (text processing, attention, GPT assembly) — these are the conceptual backbone; skim Chapter 1 if you already know the AI/ML landscape. - **Code along in Jupyter notebooks** — the book is designed for hands-on execution on a regular laptop, with GPU tips for scaling up. Don't just read the code; run and modify it. - **Use Appendix A if PyTorch is new to you** — it provides a concise introduction sufficient for following the book's examples. - **Treat fine-tuning chapters (6–7) as applied practice** — they show how the same base model adapts to classification and instruction-following, which is where the practical payoff lies. 【Coverage Limits】 The excerpts cover the book's structure, chapter topics, and stated goals in detail, but do not include actual code listings, mathematical derivations, or exercise content. Specific implementation details and results are not covered here.
Excerpt 1
书名: Build a Large Language Model (From Scratch) 从零开始构建大型语言模型 (Sebastian Raschka) (Z-Library) 作者: Sebastian Raschka 从零开始构建大型语言模型是为了帮助你理解和创建自己的类似GPT的大型语言 模型(LL...
View in text
Page 4
使用自注意力关注输入的不同部分 55 一个简单的无训练权重的自注意力机制 56 计算所有输入分词的注意力权重 61 3.4 实现带训练权重的自注意力 64 分步计算注意力权重 65 ■ 实现一个紧凑的自注 意力Python类 70 3.5 使用因果注意力隐藏未来单词 74 应用因果注意力掩码 75 ■ 使用dro...
View in text
Page 8
输入LLM的向量。 第3章专注于LLM中使用的注意力机制。它介绍了基本的自注意力框架,并逐步发 展到增强的自注意力机制。本章还涵盖了因果注意力模块的实现,该模块使LLM 能够逐个生成标记,通过随机选择注意权重并使用dropout进行掩码以减少过拟 合,并将多个因果注意力模块堆叠成多头注意力模块。 第4章专注于编写...
View in text
Excerpt 4
外资源和示例,以补充您的学习。 关于作者 SEBASTIAN RASCHKA,博士,已在机器学习和AI领域工作超过十年。除了是一 名研究员,Sebastian对教育充满热情。他以其畅销的Python机器学习书籍和对开源 的贡献而闻名。 Sebastian目前是Lightning AI的研究工程师,专注于LLM的实...
View in text
Page 16
记本电脑和智能手机,这是 Apple 等 公司目前正在探索的方向。 这种本地实现可以显著减少延迟并降低服务器相关的成本。此外,自定义 LLM 使 开发者拥有完全的自主权,可以根据需要控制模型的更新和修改。 创建 LLM 的一般过程包括预训练和微调。“预”训练指的是初始阶段,其中像 LLM 这样的模型在大型、多样化...
View in text
Page 19
录 B 获取更多阅读建议。 BERT 基于原始 Transformer 的编码器子模块构建,其训练方法与 GPT 不同。虽然 GPT 专为生成任务设计,但 BERT 及其变体专注于掩码词预测,即预测给定句子 中的掩码或隐藏词,如图 1.5 所示。这种独特的训练策略使 BERT 在文本分类任务 (包括情感预测和文档...
View in text
Tags
AI categories
Artificial IntelligenceProgramming LanguagePython
Publish Year: 2025
Language: English
File Format: PDF
File Size: 28.8 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…