rasbt/LLMs-from-scratch

GITHUB中文整理

主语言 Jupyter Notebook · 108,978 星 · 创建于 2023-07-24

大语言模型PyTorch从零实现预训练微调
打开 GitHub 仓库

一本逐步讲解如何用PyTorch从零构建GPT类大语言模型的配套代码仓库,包含开发、预训练和微调的完整实现。

Stars 108,978 累计 star,越多说明被认可得越广
Watching 869 订阅该仓库变动的人数
Forks 16,841 被派生出去的仓库数
Issues 5 当前开放的 Issue 数
语言 Jupyter Notebook GitHub 自动判定的主语言
许可证 — 开源协议,用之前先确认能不能商用
24h 增长 +0 1 天前 108,978 星
7 日增长 +0 7 天前 108,978 星
30 日增长 — 数据积累中

仓库健康度

5 个维度
只看客观信号,不构成安全性结论
维护活跃度 数据完整
2026-11-24 10:51 最近推送

最近推送 2026-11-24 10:51(8 天前),属于活跃维护

社区响应 数据完整
5 开放 Issue

开放 Issue 5 个 · Fork 16,841 个

增长趋势 部分数据
+0 7 日新增

24 小时 +0 · 7 日 +0 · 30 日 积累中

项目规范 部分数据
— 许可证

没有声明开源许可证 · 8 个 topics

版本发布 证据不足
— 发布次数

这一项暂无数据,不代表项目没有发布

「证据不足」表示这项数据暂时没有取到,不能读成「项目有问题」;「部分数据」表示只拿到一部分信号。版本发布要额外调 Releases 接口,下一版补。

Star 趋势

19 天记录
最近 90 天站内记录的星数
108,978当前 Star +546首尾差值 2026-11-15最早记录 2026-12-03最近记录

横轴 2026-11-15 → 2026-12-03,纵轴按这段时间的最小/最大值拉伸。趋势来自站内每天记录一次的星数,页面打开时不会实时请求 GitHub。

它解决什么问题

据 README 归纳
没有它会怎样

很多人想理解大语言模型内部原理,但直接阅读大型模型代码门槛太高,缺少从零逐步搭建的过渡材料。没有它,学习者只能停留在调用现成接口,难以真正掌握模型结构、训练和微调细节。

核心能力

5 条
归纳自 README,不是功能列表照抄
  1. 1按章节提供Jupyter笔记本,逐步实现文本处理、注意力机制和GPT模型结构
  2. 2演示在无标注数据上预训练小型但功能完整的大语言模型
  3. 3展示文本分类和指令跟随两种微调方式
  4. 4提供加载较大预训练模型权重进行微调的代码
  5. 5附有PyTorch入门附录和习题解答,帮助补齐基础

项目画像

6 个角度
决定要不要用之前,先看这六条

项目定位

一本逐步讲解如何用PyTorch从零构建GPT类大语言模型的配套代码仓库,包含开发、预训练和微调的完整实现。

适合谁

适合具备一定Python基础、希望从底层理解并动手实现大语言模型的学习者,也适合作为教学或自学参考。

典型场景

按照章节顺序在Jupyter笔记本中逐段运行代码,理解GPT类模型的构建过程;在无标注数据上预训练一个小型语言模型,观察训练流程;对预训练模型进行文本分类或指令跟随微调;加载较大预训练模型权重并在此基础上做微调练习

部署难度

需要安装Python和Jupyter环境,并具备PyTorch基础;学习路径按章节推进,整体上手门槛中等。

风险限制

需要较强的Python编程基础;内容面向教育目的的小型模型,而非直接生产部署。

最近维护

最近一次推送是 2026-11-24 10:51。 实现方式:以章节为单位组织代码,从文本数据处理开始,逐步实现多头注意力、GPT模型结构,再进行预训练和微调;配套书籍用文字、图表和示例解释每个阶段。

README 原文细节

2 个板块
项目自己写的功能与用法,原文照录

环境与依赖跑它之前要准备什么

Prerequisites

The most important prerequisite is a strong foundation in Python programming. With this knowledge, you will be well prepared to explore the fascinating world of LLMs and understand the concepts and code examples presented in this book.

If you have some experience with deep neural networks, you may find certain concepts more familiar, as LLMs are built upon these architectures.

This book uses PyTorch to implement the code from scratch without using any external LLM libraries. While proficiency in PyTorch is not a prerequisite, familiarity with PyTorch basics is certainly useful. If you are new to PyTorch, Appendix A provides a concise introduction to PyTorch. Alternatively, you may find my book, PyTorch in One Hour: From Tensors to Training Neural Networks on Multiple GPUs, helpful for learning about the essentials.

Hardware Requirements

The code in the main chapters of this book is designed to run on conventional laptops within a reasonable timeframe and does not require specialized hardware. This approach ensures that a wide audience can engage with the material. Additionally, the code automatically utilizes GPUs if they are available. (Please see the setup doc for additional recommendations.)

其它说明README 里剩下的内容

Companion Book / Sequel

Build A Reasoning Model (From Scratch), while a standalone book, can be considered as a sequel to Build A Large Language Model (From Scratch).

It starts with a pretrained model and implements different reasoning approaches, including inference-time scaling, reinforcement learning, and distillation, to improve the model's reasoning capabilities.

Similar to Build A Large Language Model (From Scratch), Build A Reasoning Model (From Scratch) takes a hands-on approach implementing these methods from scratch.

  • Amazon link
  • Manning link
  • GitHub repository

Bonus Material

Several folders contain optional materials as a bonus for interested readers: - Setup - Python Setup Tips - Installing Python Packages and Libraries Used in This Book - Docker Environment Setup Guide

More bonus material from the Reasoning From Scratch repository:

  • Chapter 2: Working With Text Data
  • Byte Pair Encoding (BPE) Tokenizer From Scratch
  • Comparing Various Byte Pair Encoding (BPE) Implementations
  • Understanding the Difference Between Embedding Layers and Linear Layers
  • Dataloader Intuition With Simple Numbers
  • BPE From Scratch
  • SimpleTokenizerV3 variant
  • Sliding-window data loading with PyTorch tensor views, based on Dmitry Labazkin's proposal
  • Chapter 3: Coding Attention Mechanisms
  • Comparing Efficient Multi-Head Attention Implementations
  • Understanding PyTorch Buffers
  • Chapter 4: Implementing a GPT Model From Scratch
  • FLOPs Analysis
  • Attention Alternatives

Video Course

A 17-hour and 15-minute companion video course where I code through each chapter of the book. The course is organized into chapters and sections that mirror the book's structure so that it can be used as a standalone alternative to the book or complementary code-along resource.

关于 LLMs-from-scratch 的常见问题

6 个问题
答案来自 README 归纳与 GitHub 实时数据
LLMs-from-scratch 是做什么的?

一本逐步讲解如何用PyTorch从零构建GPT类大语言模型的配套代码仓库,包含开发、预训练和微调的完整实现。 很多人想理解大语言模型内部原理,但直接阅读大型模型代码门槛太高,缺少从零逐步搭建的过渡材料。没有它,学习者只能停留在调用现成接口,难以真正掌握模型结构、训练和微调细节。

LLMs-from-scratch 有哪些功能?

核心能力:按章节提供Jupyter笔记本,逐步实现文本处理、注意力机制和GPT模型结构;演示在无标注数据上预训练小型但功能完整的大语言模型;展示文本分类和指令跟随两种微调方式;提供加载较大预训练模型权重进行微调的代码;附有PyTorch入门附录和习题解答,帮助补齐基础。

LLMs-from-scratch 适合谁用?

适合具备一定Python基础、希望从底层理解并动手实现大语言模型的学习者,也适合作为教学或自学参考。

LLMs-from-scratch 是怎么工作的?

以章节为单位组织代码,从文本数据处理开始,逐步实现多头注意力、GPT模型结构,再进行预训练和微调;配套书籍用文字、图表和示例解释每个阶段。

用 LLMs-from-scratch 有什么门槛或限制?

需要较强的Python编程基础;内容面向教育目的的小型模型,而非直接生产部署。

LLMs-from-scratch 现在还在维护吗?数据怎么样?

目前 108,978 星,16,841 个 Fork,主语言 Jupyter Notebook,创建于 2023-07-24,最近一次推送 2026-11-24 10:51。星数、Fork、推送时间都是 GitHub 返回的实时数据,「最近推送」离现在越近说明越活跃;长期不动要留意是否已停止维护。

这些内容来自哪

读法说明
中文整理据 README 归纳,数据取自 GitHub
中文说明据项目 README 归纳只写 README 能确认的内容,不替作者补功能与数字
关键数据与 GitHub 页面一致星数、Fork、语言与时间都不做二次加工
数据日期2026-11-24 23:25星数与介绍这份数据是哪一天的

项目数据取自 GitHub 官方接口与项目 README;星数、Fork、语言与时间以 GitHub 页面为准,档案更新于 2026-11-24 23:25。