一からコーディングするLLM:完全コース
Sebastian Raschka氏は、LLMの仕組みを深く理解するための「一からLLMを構築する」詳細なコース(約15時間)を公開し、書籍『Build a Large Language Model (From Scratch)』の補足動画として作成されたこのコンテンツが、スタンドアロンでも有用であると説明している。
キーポイント
LLM理解のための実践的アプローチ
LLMの仕組みを深く理解する最善の方法として、一からコーディングする実践的な学習コースを提供している。
詳細な教育コンテンツの公開
約15時間に及ぶ詳細なビデオコースを公開し、昨年共有した短縮版ワークショップよりも5倍長く、より包括的な内容となっている。
書籍との連携とスタンドアロン利用
『Build a Large Language Model (From Scratch)』書籍の補足コンテンツとして作成されたが、スタンドアロンでも十分に機能する内容である。
学習方法の比喩的説明
カートを一から組み立てることで自動車の基本を学ぶように、シンプルなLLM構築から始めることで基礎を確実に習得できると説明している。
影響分析・編集コメントを表示
影響分析
この記事は、AI/LLM分野の教育コンテンツとして重要な価値を持つが、技術的革新や業界を変えるような新規性はない。LLM開発者や学習者にとって実用的なリソースを提供することで、基礎理解の深化に貢献する可能性がある。
編集コメント
LLMのブラックボックス化が進む中、基礎から学ぶ重要性を説く実践的な教育リソース。技術者育成の観点で評価できるが、新技術の発表ではない点に留意。
タイトル: ゼロからLLMを構築する:完全コース
ここ数ヶ月、私は推論モデルについて多く執筆してきました(連続4記事です!)。「エージェント的」なものに次いで、推論は2025年のLLM分野における最大のトピックの1つです。
しかし今月は、LLMの仕組みを理解する最良の方法の一つである「LLMのコーディング方法」について、より基礎的または「基盤的」なコンテンツを共有したいと思います。
その理由は、昨年共有した要約版LLMワークショップを多くの方に高く評価していただき、有益だったと感じたからです。
そのため、この約5倍の長さでより詳細なコンテンツ(合計約15時間)は、さらに役立つだろうと考えました。
また、不幸にも私は首の重傷を負っており、過去3週間ほとんどパソコン作業ができていません。現在は、提案されている手術を検討する前に、保存的治療を試みているところです。軌道に戻り始めたばかりの時に、また別の困難が訪れるとは、最悪のタイミングです。
ですから、回復期間中に、ここ数ヶ月で録画していたこれらの動画を共有することが、良いつなぎのコンテンツになるだろうと考えました。
お役に立てれば幸いです。ご支援に感謝します。
追記:これらの動画は、元々私の著書『Build a Large Language Model (From Scratch)』の補足コンテンツとして始まりました。しかし、スタンドアロンのコンテンツとしても十分に成立することが分かりました。
なぜゼロから構築するのか?
それはおそらく、LLMが実際にどのように機能するかを学ぶ最良で最も効率的な方法です。さらに、多くの読者から、実際にやってみて非常に楽しかったという声をいただいています。
例えて言うなら:車に興味があり、その仕組みを理解したいなら、ゼロから組み上げるチュートリアルに従うのが学ぶための素晴らしい方法です。もちろん、最初のプロジェクトとしてF1レーシングカーを構築することから始めたいとは思わないでしょう。法外に高価で、初めてのプロジェクトとしては過度に複雑だからです。代わりに、ゴーカートのようなよりシンプルなものから始める方が理にかなっています。
ゴーカートを組み立てる過程でも、ステアリングの仕組みやエンジンの機能などを学べます。プロのレーシングカーに乗る前(あるいはそれを造る会社やチームに加わる前)に、サーキットに持ち込んで練習し、楽しむことさえできます。結局のところ、最高のレーシングドライバーたち(ミハエル・シューマッハやアイルトン・セナを思い浮かべてください)は、しばしば自分でゴーカートを組み立て、改造することからキャリアを始めています。そうすることで、車に対する優れた感覚を養えただけでなく、メカニックに貴重なフィードバックを提供することもでき、それが他のドライバーに対する優位性となったのです。
参考文献
Build an LLM from Scratch 書籍(Manning | Amazon)
Build an LLM from Scratch GitHubリポジトリ
1 - コード環境のセットアップ (0:21:01)
これは、uvを使ってPython環境をセットアップする方法を説明する補足動画です。
特に、このドキュメントで説明されている「uv pip」を使用します。
代替として、ネイティブの「uv add」構文(この動画では言及されていますが詳細には触れていません)についてはこちらで説明しています。
注意点/ヒント:インストールはWindowsの特定のバージョンで問題を引き起こす可能性があります。Windowsマシンでインストールに問題が生じた場合(おそらく動画5でOpenAIのオリジナルGPT-2モデルウェイトをロードするためのTensorFlow依存関係が原因です)、ご心配なく。TensorFlowのインストールはスキップして構いません(requirementsファイルからTensorFlowの行を削除すれば可能です)。
代替案として、私はGPT-2のモデルウェイトをTensorFlowのテンソル形式からPyTorchのテンソルに変換し、Hugging Faceモデルハブで公開しました。動画5のウェイト読み込み部分の代替としてご利用いただけます:https://huggingface.co/rasbt/gpt2-from-scratch-pytorch。
いずれにせよ、このウェイト読み込みのコードについて心配する必要があるのは、動画5の終わりまでありません。
2 - テキストデータの扱い (1:28:01)
この動画では、LLMトレーニングのためのテキストデータ準備ステップ(トークン化、バイトペアエンコーディング、データローダーなど)について解説します。
3 - アテンション機構のコーディング (2:15:40)
これは、アテンション機構(セルフアテンション、因果的アテンション、マルチヘッドアテンション)がどのように機能するかを、ゼロからコーディングすることで説明する補足動画です。
車のエンジンを組み立てる(フレームやシート、ホイールを追加する前の段階)と考えることができます。
4 - LLMアーキテクチャのコーディング (0:21:01)
この動画では、ゼロからLLMアーキテクチャをコーディングする方法を扱います。
5 - ラベルなしデータでの事前学習 (2:36:44)
この動画では、ゼロからLLMを事前学習する方法を説明します。
6 - 分類のためのファインチューニング (2:15:29)
これは、次の動画でLLMを命令ファインチューニングする前に、その穏やかな導入として、LLMを分類器(ここではスパム分類の例を使用)としてファインチューニングする方法を説明する動画です。
7 - 命令ファインチューニング (1:46:04)
最後に、この動画ではLLMを命令ファインチューニングする方法を説明します。
ご視聴と、実際に手を動かすことをお楽しみください!
ボーナス: 過去と現在のLLM (2018年から2025年まで)
有料購読者の皆様への感謝を込めて、4月上旬(Llama 4リリースの約2日後)に録画した、コーディングを含まない2.5時間のボーナス動画を共有します。この講演では、2018年のGPT-2以降、何がどのように変化したかに焦点を当て、2025年現在のLLMの状況について論じています。
独立した自営研究者として、皆様のご支援は私にとって本当に大きな意味があります。ありがとうございます。
今後数週間から数ヶ月のうちに状況が好転することを願っています。今後の記事に向けたアイデアがたくさんあり、それらに取り組むのが待ちきれません!
さらに読む
原文を表示
I wrote a lot about reasoning models in recent months (4 articles in a row)! Next to everything "agentic," reasoning is one of the biggest LLM topics of 2025.
This month, however, I wanted to share more fundamental or "foundational" content with you on how to code LLMs, which is one of the best ways to understand how LLMs work.
Why? Many people really liked and benefited from the abbreviated LLM workshop I shared last year:
So, I thought this ~5× longer and more detailed content (~15 hours in total) would be even more useful.
Also, I'm sadly dealing with a bad neck injury and haven't really been able to work on a computer for the past 3 weeks. I am currently trying a conservative treatment before considering the suggested surgical route. This is the worst timing as I just started to get back on track before life threw another curveball.
So, during my recovery, I thought sharing these videos I recorded in the last couple of months would be a nice in-between content.
I hope you find this useful, and thanks for your support!
PS: The videos originally started as supplementary content for my Build a Large Language Model (From Scratch) book. But it turns out they also work pretty well as standalone content.
Why build from scratch?
It's probably the best and most efficient way to learn how LLMs really work. Plus, many readers have told me they had a lot of fun doing it.
To offer an analogy: if you are into cars and want to understand how they work, following a tutorial that walks you through building one from the ground up is a great way to learn. Of course, we probably wouldn't want to start by building a Formula 1 race car since it would be prohibitively expensive and overly complex for a first project. Instead, it makes more sense to start with something simpler, like a go-kart.
Building a go-kart still teaches you how the steering works, how the motor functions, and more. You can even take it to the track and practice (and have a lot of fun with it) before stepping into a professional race car (or joining a company or team that is focused on building one). After all, the best race drivers often got their start by building and tinkering with their own go-karts (think Michael Schumacher and Ayrton Senna). By doing that, they not only developed a great feel for the car but could also provide valuable feedback to their mechanics, which gave them an edge over the other drivers.
References
Build an LLM from Scratch book (Manning | Amazon)
Build an LLM from Scratch GitHub repository
1 - Set up your code environment (0:21:01)
This is a supplementary video explaining how to set up a Python environment using uv.
In particular, we are using “uv pip”, which is explained in this document.
Alternatively, the native “uv add” syntax (mentioned but not explicitly covered in this video) is described here.
Note / Tip: The installation may cause issues on certain versions of Windows. If you are on a Windows machine and have troubles with the installation (likely due to a TensorFlow dependency to load the original GPT-2 model weights from OpenAI in video 5), please don’t worry about it and feel free to skip the TensorFlow installation (you can do this by removing the TensorFlow line from the requirements file.)
To provide an alternative, I converted the GPT-2 model weights from a TensorFlow tensor format to PyTorch tensors and shared them on the Hugging Face model hub, which you can use as an alternative to the weight loading portion in video 5: https://huggingface.co/rasbt/gpt2-from-scratch-pytorch.
In any case, you don’t have to worry about this weight-loading code until the end of video 5.
2 - Working with text data (1:28:01)
This video goes over text data preparations steps (tokenization, byte pair encoding, data loaders, etc.) for LLM training.
3 - Coding attention mechanisms (2:15:40)
This is a supplementary video explaining how attention mechanisms (self-attention, causal attention, multi-head attention) work by coding them from scratch.
You can think of it as building the engine of a car (before adding the frame, seats, and wheels).
4 - Set up your code environment (0:21:01)
This video covers how to code an LLM architecture from scratch.
5 - Pretraining on Unlabeled Data (2:36:44)
This video explains how to pretrain a LLM from scratch.
6 - Finetuning for Classification (2:15:29)
This is a video explaining how to fine-tune an LLM as a classifier (here using a spam classification example) as a gentle introduction to finetuning, before instruction finetuning the LLM in the next video.
7 - Instruction Finetuning (1:46:04)
Finally, this video explains how to instruction finetune the LLM.
Happy viewing & tinkering!
Bonus: LLMs Then And Now (From 2018 to 2025)
As a big thank you to the paid subscribers, I want to share a 2.5h (non-coding) bonus video I recorded earlier in April, approximately 2 days after the Llama 4 release. In this talk, I discuss the current LLM landscape in 2025 with a focus on what and how things have changed since GPT-2 in 2018.
Thanks for your support, as an independent and self-employed researcher, this really means a lot to me!
Hopefully, things will improve in the next few weeks/months as I have lots of ideas for upcoming articles and can’t wait to work on them!
Read more
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み