34 / 90

第 34 日:MLP の構造と順伝播

歴史的なパーセプトロンから微分可能なニューロンへ進み、projects/autograd のスカラーエンジン上で 2-2-1 の多層パーセプトロンを組み立て、すべての順伝播の値を手計算し、第 35 日に手で導出するパラメータ勾配を集めます。

モデル構造・事前学習・LoRA 480 分(8 時間の集中学習)
01 120 分

パーセプトロンから微分可能な MLP へ:層・重み・活性化関数

エージェント支援エンジニアリングチェック

前提条件

第 33 日の自動微分習得チェックと、検証済みの Value `backward()` セマンティクス。

学習目標

歴史的パーセプトロン(ステップ活性化)と微分可能ニューロン $z = \sum_i w_i x_i + b$、$a = \sigma(z)$ を区別し、XOR のような関数を表すには非線形活性化を持つ層の積み重ねが必要な理由を説明し、本日使う参照用 2-2-1 ネットワークの正確な構造を述べる。

手順

以下の概念分解を読み、計算の前に必ず予測してください。

パーセプトロンは $z = \sum_i w_i x_i + b$ を計算し、$z > 0$ なら $1$、そうでなければ $0$ を出力します。ステップ活性化により入出力写像は区分定数となり、勾配はほぼ至る所で $0$、折れ点では未定義です。したがって勾配ベースの学習では改善できません。微分可能ニューロンはステップを $\tanh$ のような滑らかな活性化に置き換えます。

多層パーセプトロン (MLP) は全結合層を積み重ねます。層 $l$ の各ニューロンは層 $l-1$ のすべての活性化を受け取ります。単一の線形ユニットは直線の決定境界しか引けず XOR を表現できません。非線形活性化を持つ 2 層以上なら可能です。

本日の参照ネットワーク N1:入力 $x=(2.0, -1.0)$。隠れニューロン $h_1$:重み $(0.5, -0.25)$、バイアス $0.1$、$\tanh$。隠れニューロン $h_2$:重み $(-0.5, 1.0)$、バイアス $-0.2$、$\tanh$。出力ニューロン $o$:重み $(1.0, -2.0)$、バイアス $0.3$、線形(活性化なし)。損失 $L = (o - y)^2$、目標 $y = 1.0$。

演習と予測記録

1. 計算を始める前に、$z_1$ と $z_2$ の符号を予測し、飽和に近い隠れニューロンがあるか判断してください。
2. N1 の学習可能パラメータを数え、重みとバイアスに分けてください。
3. ステップ活性化のパーセプトロンが勾配降下で学習できない理由を一文で述べてください。
4. $o$ が目標 $y = 1.0$ より上か下かを予測してください。

独立習得基準

学習者が $z = \sum w_i x_i + b$ と $a = \sigma(z)$ の式を書け、N1 の 9 個のパラメータを正しく数え、勾配ベースの学習に微分可能性が必要な理由を自分の言葉で説明できること。

公式ソースリソースと権利証跡

linking CC BY-NC 3.0 noncommercial

Neural Networks and Deep Learning: How the backpropagation algorithm works

著者・提供機関: Michael Nielsen | オリジナル出所URL ↗

該当セクション・タイムスタンプ: Sections: Perceptrons, Sigmoid neurons, The architecture of neural networks

検証バージョンと日付: Online book (Verified: 2026-10-30)

選定理由:

Clear four-equation derivation of backpropagation with scalar and matrix notation.

ソース前提知識:

Matrix multiplication and the multivariable chain rule from Days 19-24.

安全フォールバック(オフライン回復性):

All four backprop equations and derivations are self-contained within the lesson text.

比較対象候補リソースと選定評価 (2)
Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

Modular computational graph view; used as secondary source on Days 35-36.

linking MIT

micrograd: A tiny scalar-valued autograd engine

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

検証バージョンと日付: Git commit master (Verified: 2026-10-30)

選定理由:

Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

ソース前提知識:

Python object-oriented programming and operator overloading from Day 31.

安全フォールバック(オフライン回復性):

The local projects/autograd engine is runnable offline and matches micrograd semantics.

比較対象候補リソースと選定評価 (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

02 120 分

2-2-1 ネットワークの順伝播を手計算する

独立習得チェック

前提条件

アクティビティ 34-01 のネットワーク定義と tanh の評価。

学習目標

ネットワーク N1 のすべての中間値 — $z_1, a_1, z_2, a_2, o, L$ — を手で計算し、順伝播台帳に記録したうえで、スカラー自動微分エンジンで各項目を照合する。

手順

エンジンに触れる前に、紙または普通の電卓で順伝播を計算してください。ノード名・局所入力・計算値を 1 行ずつ台帳に記入します。

次にエンジンで照合します:

python3 - <<'EOF'
import sys; sys.path.insert(0, '.')
from projects.autograd.engine import Value
x1, x2 = Value(2.0), Value(-1.0)
a1 = (x1*Value(0.5) + x2*Value(-0.25) + Value(0.1)).tanh()
a2 = (x1*Value(-0.5) + x2*Value(1.0) + Value(-0.2)).tanh()
o = a1*Value(1.0) + a2*Value(-2.0) + Value(0.3)
loss = (o - 1.0)**2
print(a1.data, a2.data, o.data, loss.data)
EOF

出力値と台帳を 1 行ずつ比較してください。不一致があればその行の計算が間違っています。最初の相違箇所を特定しましょう。

演習と予測記録

N1 の完全な順伝播台帳を作成してください:$z_1$、$a_1 = \tanh(z_1)$、$z_2$、$a_2 = \tanh(z_2)$、$o$、$L = (o - 1.0)^2$。手計算の値は小数点以下 4 桁まで記録し、エンジン出力が確認・否定のどちらだったかを各項目に記してください。

独立習得基準

台帳の全項目がエンジン値と $10^{-3}$ 以内で一致し、出力ニューロンに tanh を誤適用せず、不一致があった場合は最初にずれた行を特定できること。

公式ソースリソースと権利証跡

linking 3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions No video, audio, transcript or frame redistribution permitted

What is backpropagation really doing? (Chapter 3, Deep Learning)

著者・提供機関: Grant Sanderson (3Blue1Brown) | オリジナル出所URL ↗

該当セクション・タイムスタンプ: The Intuition for Backpropagation

検証バージョンと日付: Online video and article (Verified: 2026-10-30)

選定理由:

Visual and geometric intuition for how nudging weights impacts the final cost function.

ソース前提知識:

Basic derivative intuition from Days 16-18.

安全フォールバック(オフライン回復性):

The lesson derives the network gradient equations step by step without requiring video playback.

比較対象候補リソースと選定評価 (2)
StatQuest Neural Networks Part 3 ↗ compared_alternative [Proprietary]

Step-by-step arithmetic without calculus notation; 3Blue1Brown provides the calculus-visual link needed.

Khan Academy Multivariable Chain Rule ↗ compared_alternative [CC BY-NC-SA 3.0]

Pure math focus; 3Blue1Brown contextualizes the chain rule specifically for neural network graphs.

linking MIT

micrograd: A tiny scalar-valued autograd engine

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

検証バージョンと日付: Git commit master (Verified: 2026-10-30)

選定理由:

Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

ソース前提知識:

Python object-oriented programming and operator overloading from Day 31.

安全フォールバック(オフライン回復性):

The local projects/autograd engine is runnable offline and matches micrograd semantics.

比較対象候補リソースと選定評価 (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

03 120 分

スカラー自動微分エンジン上で Neuron・Layer・MLP クラスを構成する

エージェント支援エンジニアリングチェック

前提条件

アクティビティ 34-02 で照合済みの順伝播台帳。

学習目標

micrograd `nn.py` のパターンと同じく、projects/autograd の `Value` オブジェクトを組み合わせて Module/Neuron/Layer/MLP クラスを実装する:ニューロンは重みとバイアスを持ち、層はニューロンを持ち、MLP は層を持ち、`parameters()` がすべての学習可能 `Value` を返す。

手順

スクラッチ用ディレクトリに(lessons/ や projects/autograd/ の中ではなく)mlp_scratch.py のような小さなモジュールを次の骨格で作成してください:

from projects.autograd.engine import Value

class Module:
def parameters(self):
return []

class Neuron(Module):
def __init__(self, weights, bias):
assert len(weights) > 0
self.w = [Value(w) for w in weights]
self.b = Value(bias)
def __call__(self, x):
assert len(x) == len(self.w), 'input/weight length mismatch'
z = sum((wi * xi for wi, xi in zip(self.w, x)), self.b)
return z.tanh()
def parameters(self):
return self.w + [self.b]

Layer(同一入力を並列に評価するニューロンのリスト)と MLP(順に適用する層のリスト)に拡張してください。N1 を組み立てる際、出力ニューロンには tanh をスキップするフラグを付けて線形出力にします。第 34 日のサンプルで損失を評価し、loss.backward() を呼んで 9 個すべてのパラメータ勾配を表示してください。

単一ニューロンの参照トレースは次で比較できます:

python3 projects/autograd/cli.py forward-backward --preset neuron --json

演習と予測記録

1. 3 つのクラスを実装し、N1 の parameters() がちょうど 9 個を返すようにしてください。
2. loss.data を評価し、手計算の $4.5179$ と一致することを確認してください。
3. loss.backward() の後、9 個すべての .grad を記録してください。第 35 日にこれらを手で再導出します。
4. すべてのパラメータを parameters() 経由で到達可能にしておく利点を 1 つ述べてください。

独立習得基準

学習者のクラスが順伝播の値を再現し、エンジンと $10^{-3}$ 以内で一致する 9 個のパラメータ勾配を生成し、重み付き和の前に明示的な長さチェックがあること。

公式ソースリソースと権利証跡

linking MIT

micrograd: A tiny scalar-valued autograd engine

著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

検証バージョンと日付: Git commit master (Verified: 2026-10-30)

選定理由:

Minimal, readable implementation of scalar autograd and MLP layers directly matched to our project.

ソース前提知識:

Python object-oriented programming and operator overloading from Day 31.

安全フォールバック(オフライン回復性):

The local projects/autograd engine is runnable offline and matches micrograd semantics.

比較対象候補リソースと選定評価 (2)
PyTorch autograd C++ engine ↗ compared_alternative [Modified BSD]

Production scale, but too large and complex for tracing by hand in introductory lessons.

Autograd (HIPS) ↗ compared_alternative [MIT]

Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.

linking MIT

CS336 Assignment 1: Basics (Spring 2025 tagged release)

著者・提供機関: Stanford University CS336 Staff | オリジナル出所URL ↗

該当セクション・タイムスタンプ: README setup; tests/adapters.py; tests/test_optimizer.py; tests/test_nn_utils.py; tests/test_serialization.py

検証バージョンと日付: spring2025 tag; README, tests and MIT LICENSE checked 2026-11-01

選定理由:

Lead assignment-driven source for pretraining component practice and tests. nanoGPT remains a compact historical code-reading companion, not the current recommended training curriculum.

ソース前提知識:

Fluent Python, PyTorch, and linear algebra

安全フォールバック(オフライン回復性):

Local self-contained pretraining scripts run on standard local CPUs or budget GPUs without Stanford cluster dependency.

比較対象候補リソースと選定評価 (2)
Karpathy nanoGPT ↗ compared_alternative [MIT]

Simpler code layout, useful complementary reference for minimal PyTorch Transformer.

Hugging Face Transformers Course ↗ compared_alternative [Apache-2.0]

High-level API focus rather than building from raw tensor fundamentals.

04 120 分

独立習得チェック:MLP 構造と順伝播の計算力

独立習得チェック

前提条件

アクティビティ 34-01 から 34-03 まで。

学習目標

パーセプトロン対 MLP の構造、パラメータ数の把握、完全な数値順伝播を支援なしで示し、下の客観クイズで検証する。

手順

以前のアクティビティや AI ツールを参照せず:一般ニューロンの順伝播の式を書き、重みとバイアスは変更せずに新しい入力 $x = (1.0, 1.0)$ に対する N1 の出力を手で計算し、その後に客観クイズを完了してください。提出前に下書き作業を草稿欄に記録してください。

演習と予測記録

独立タスク:
1. $x = (1.0, 1.0)$ に対し、$z_1, a_1, z_2, a_2, o$ と $L$ を手で計算してください。
2. 新しい入力の評価にパラメータの変更が不要な理由を説明してください。
3. 独立モードで 3 問の客観クイズを 100% で完了してください。

客観的知識チェック:MLP の構造と順伝播

理解度を確認するために次の質問に答えてください。提出前に申告する支援条件を選択してください。

申告する支援条件

注意:AI 不使用は正直な申告条件であり、監視を主張するものではありません。

1.

ステップ活性化を持つ古典的パーセプトロンが勾配降下で学習できないのはなぜですか?

2.

入力 $x = (2.0, -1.0)$ に対するネットワーク N1 の出力 $o$ はいくらですか?

3.

2-2-1 ネットワーク N1 の学習可能パラメータは合計いくつですか?

受験履歴と証拠

    独立習得基準

    新しい入力に対する支援なしの順伝播が $o \approx 0.0538$、$L \approx 0.8954$ と $10^{-3}$ 以内で一致し、独立クイズが 100% であること。

    公式ソースリソースと権利証跡

    linking CC BY-NC 3.0 noncommercial

    Neural Networks and Deep Learning: How the backpropagation algorithm works

    著者・提供機関: Michael Nielsen | オリジナル出所URL ↗

    該当セクション・タイムスタンプ: Sections: Perceptrons, Sigmoid neurons

    検証バージョンと日付: Online book (Verified: 2026-10-30)

    選定理由:

    Clear four-equation derivation of backpropagation with scalar and matrix notation.

    ソース前提知識:

    Matrix multiplication and the multivariable chain rule from Days 19-24.

    安全フォールバック(オフライン回復性):

    All four backprop equations and derivations are self-contained within the lesson text.

    比較対象候補リソースと選定評価 (2)
    Goodfellow et al. Deep Learning (Chapter 6) ↗ compared_alternative [All Rights Reserved (free to view online)]

    Comprehensive graduate reference, but heavier tensor notation than needed for first MLP derivations.

    Modular computational graph view; used as secondary source on Days 35-36.

    linking MIT

    micrograd: A tiny scalar-valued autograd engine

    著者・提供機関: Andrej Karpathy | オリジナル出所URL ↗

    該当セクション・タイムスタンプ: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'

    検証バージョンと日付: 2022 (Verified: 2026-10-30)

    選定理由:

    Deepest bottom-up mechanical clarity for backpropagation engine and computational graph visualization.

    ソース前提知識:

    High-school calculus (derivatives) and basic Python syntax

    安全フォールバック(オフライン回復性):

    Authored mathematical derivations, interactive Pyodide engine, and local tests provide complete offline study without video streaming.

    比較対象候補リソースと選定評価 (2)
    PyTorch Autograd Internals ↗ compared_alternative [Modified BSD]

    Normative production documentation, but micrograd's pure Python implementation provides far superior initial clarity.

    3Blue1Brown: What is Backpropagation? ↗ compared_alternative [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

    High visual quality, pairs cleanly as optional supplementary visual reference.