From Scratch
Sebastian Raschka

#Reasoning_Model
#AI
#LLM
#RL
💬 «یک بررسی عمیق و استثنایی از مرز بعدی AI.»
—امان چادا، Google
📘 این کتاب یک راهنمای عملی برای فهمیدن اینه که LLMهای مدرنِ استدلالمحور چطور کار میکنن؛ اون هم با ساختن روشهای اصلیشون، قدمبهقدم. کتاب یک داستان مهندسی روشن تعریف میکنه: از یک LLM پیشآموزشدیده معمولی شروع میکنی، یاد میگیری تولید متن چطور کار میکنه، ابزارهای ارزیابی قابلاعتماد میسازی، استدلال رو با روشهای زمان اینفرنس بهتر میکنی، و بعد میری سراغ رویکردهای مبتنی بر آموزش، مثل یادگیری تقویتی و تقطیر.
🧭 مسیر کتاب کاملاً حسابشده جلو میره. فصلهای اول، مدل پایه رو میسازن و تولید متن، KV Caching و ارزیابی با وریفایرهای ریاضی رو توضیح میدن. فصلهای میانی نشون میدن چطور میشه بدون تغییر دادن وزنهای مدل، استدلال رو بهتر کرد؛ با استفاده از Chain-of-Thought Prompting، نمونهگیری، خودسازگاری، امتیازدهی پاسخ و خودبهبوددهی. فصلهای بعدی وارد تغییر دادن خود مدل میشن؛ از یادگیری تقویتی با پاداشهای قابلراستیآزمایی گرفته تا بهبودهای GRPO، پاداشهای فرمت، و در نهایت تقطیر از مدلهای استدلالی قویتر به مدلهای کوچکتر.
🛠️ چیزی که این کتاب رو مخصوصاً کاربردی میکنه اینه که روشهای اصلی رو از صفر پیادهسازی میکنه، نه اینکه اونها رو مثل کالهای آماده و جعبهسیاهِ کتابخونهها رد کنه. خواننده میبینه خودسازگاری، خودبهبوددهی، Best-of-N و روشهای مبتنی بر آموزش واقعاً چطور کار میکنن، از جمله اینکه چه بدهبستانهایی از نظر هزینه و تأخیر دارن. کتاب همینطور درباره حالتهای شکست رایج هم حرف میزنه؛ از جمله جاهایی که خودبهبوددهی میتونه جواب رو حتی بدتر کنه. کانسپتهای سختی مثل softmax، temperature و نمونهگیری top-p با توضیحهای متصل به کد و دیاگرام روشن میشن، و ورکفلوهای تصویری کمک میکنن پایپلاینها و روشهای امتیازدهی راحتتر دنبال بشن.
🧩 خواندن کتاب بیشتر شبیه دنبال کردن یک ساخت فنی هدایتشده است، نه یک مرور پراکنده از موضوعهای AI. هر کانسپت دقیقاً وقتی معرفی میشه که پروژه بهش نیاز پیدا میکنه. دیاگرامها، نقشهراهها، لیستینگهای کد، تمرینها و خلاصههای تکرارشونده از ورکفلوها کمک میکنن خواننده وسط مطالب پیشرفته گم نشه. این ساختار دقیقاً نقطه قوت حرفهای سباستین راشکا رو نشون میده: توضیح دادن موضوعهای پیچیده یادگیری ماشین با ملموس کردن هر جزئیات و نشان دادن اینکه هر بخش دقیقاً کجای داستان بزرگتر قرار میگیره. او مکانیزمهایی مثل ارزیابی، Log-Probabilityها، رگولاریزیشن KL یا تقطیر رو بهعنوان مفاهیم جداافتاده توضیح نمیده؛ همه رو به هدف اصلی وصل میکنه: اینکه مدلهای استدلالی قابلفهم و قابلپیادهسازی بشن.
📚 از نظر ساختار فیزیکی و سازماندهی، کتاب هشت فصل و هفت پیوست مفصل داره. این طراحی باعث میشه روایت اصلی متمرکز بمونه، و مطالب پشتیبان مثل منابع، جواب تمرینها، سورسکد مدل، مدلهای بزرگتر، بچینگ، جایگزینهای ارزیابی و رابطهای چت، به پیوستهای مرتب منتقل بشن. نتیجه، کتابیه که منطقی جلو میره، همچنان دستبهکد باقی میمونه، راحت قابلدنبالکردنه و از نظر فنی عمیقه، بدون اینکه مدام مسیر اصلی ساخت رو قطع کنه.
📦 داخل کتاب چی هست
🧱 پیادهسازی از صفر برای بهبودهای اصلی استدلال در LLMها
✅ روشهای ارزیابی مبتنی بر وریفایر
🧮 یادگیری تقویتی با وریفایرهای خودکار برای تسکهای ریاضی
👤 درباره خواننده
👨💻 برای خوانندههایی که Python بلدن و کمی هم با یادگیری ماشین آشنا هستن.
📖 فهرست مطالب
۱. فهم مدلهای استدلالی
۲. تولید متن با یک LLM پیشآموزشدیده
۳. ارزیابی مدلهای استدلالی
۴. بهبود استدلال با اسکیلینگ در زمان اینفرنس
۵. اسکیلینگ در زمان اینفرنس از طریق خودبهبوددهی
۶. آموزش مدلهای استدلالی با یادگیری تقویتی
۷. بهبود GRPO برای یادگیری تقویتی
۸. تقطیر مدلهای استدلالی برای استدلال کارآمد
پیوست A. منابع و مطالعه بیشتر
پیوست B. جواب تمرینها
پیوست C. سورسکد Qwen3 LLM
پیوست D. استفاده از LLMهای بزرگتر
پیوست E. بچینگ و اجرای متمرکز بر توان عملیاتی
پیوست F. رویکردهای رایج برای ارزیابی مدل
پیوست G. ساخت یک رابط چت
👤 درباره نویسنده
👨🔬 سباستین راشکا بیش از یک دهه است که روی یادگیری ماشین و AI کار میکنه. سباستین در سال ۲۰۲۲ به Lightning AI پیوست و حالا تمرکزش روی پژوهش در AI و LLM، توسعه نرمافزار متنباز و ساخت محتوای آموزشی است. قبل از آن، در University of Wisconsin-Madison بهعنوان استادیار در دپارتمان آمار کار میکرد و تمرکزش روی پژوهش در یادگیری عمیق و یادگیری ماشین بود. او علاقه جدی به آموزش داره و بیشتر بهخاطر کتابهای پرفروشش درباره یادگیری ماشین با استفاده از نرمافزارهای متنباز شناخته میشه.
"An exceptional deep dive into the next frontier of AI.”
—Aman Chadha, Google
This book is a practical guide to understanding how modern reasoning-oriented LLMs work by building their core methods step by step. The book tells a clear engineering story: start with a conventional pre-trained LLM, learn how text generation works, build reliable evaluation tools, improve reasoning through inference-time methods, then move into training-based approaches such as reinforcement learning and distillation.
The progression is deliberate. Early chapters establish the baseline model and explain text generation, KV caching, and evaluation with math verifiers. The middle chapters show how reasoning can be improved without changing model weights, using chain-of-thought prompting, sampling, self-consistency, response scoring, and self-refinement. Later chapters move to changing the model itself through reinforcement learning with verifiable rewards, GRPO improvements, format rewards, and finally distillation from stronger reasoning models into smaller ones.
The book is especially useful because it implements the core methods from scratch rather than treating them as black-box library calls. Readers see how self-consistency, self-refinement, Best-of-N, and training-based methods actually work, including their cost and latency trade-offs. It also discusses common failure modes, including cases where refinement can make answers worse. Difficult concepts such as softmax, temperature, and top-p sampling are clarified with code-linked explanations and diagrams, and visual workflows make pipelines and scoring methods easier to follow.
Reading the book feels like following a guided technical build rather than a loose survey of AI topics. Each concept is introduced because the project now needs it. Diagrams, roadmaps, code listings, exercises, and repeated workflow summaries help readers stay oriented through advanced material. This structure reflects Sebastian Raschka’s professional strength: explaining complex machine learning topics by making every detail concrete and showing exactly where each section fits in the larger story. He does not treat mechanisms like evaluation, log-probabilities, KL regularization, or distillation as isolated abstractions; he connects them to the goal of making reasoning models understandable and implementable.
Physically and organizationally, the book has eight chapters and seven substantial appendixes. That design keeps the main narrative focused while moving supporting material like references, exercise solutions, model source code, larger models, batching, evaluation alternatives, and chat interfaces into ordered appendixes. The result is a logically flowing book that remains hands-on, navigable, and technically deep without constantly interrupting the central build.
What's inside
• From-scratch implementations of core LLM reasoning improvements
• Verifier-based evaluation methods
• RL with automatic verifiers for mathematics tasks
About the Reader
For readers who know Python and have some knowledge of machine learning.
Table of Contents
1. Understanding reasoning models
2. Generating text with a pretrained LLM
3. Evaluating reasoning models
4. Improving reasoning with inference-time scaling
5. Inference-time scaling via self-refinement
6. Training reasoning models with reinforcement learning
7. Improving GRPO for reinforcement learning
8. Distilling reasoning models for efficient reasoning
A. References and further reading
B. Exercise solutions
C. Qwen3 LLM source code
D. Using larger LLMs
E. Batching and throughput-oriented execution
F. Common approaches to model evaluation
G. Building a chat interface
Sebastian Raschka has been working on machine learning and AI for more than a decade. Sebastian joined Lightning AI in 2022, where he now focuses on AI and LLM research, developing open-source software, and creating educational material. Prior to that, Sebastian worked at the University of Wisconsin-Madison as an assistant professor in the Department of Statistics, focusing on deep learning and machine learning research. He has a strong passion for education and is best known for his bestselling books on machine learning using open-source software.









