24 / 90

第 24 日:テンソル、ブロードキャスト、reshape とメモリ台帳

ベクトルと行列を、名前付き軸(バッチ・時間・チャネル)を持つ階数 n のテンソルへ一般化する。ブロードキャストの規則と失敗例、reshape とビュー対コピーの挙動、実際のモデルのテンソルのメモリ台帳を検証する。各活動で予測、観察、復習、回復を480分の台帳に記録する。

数学とアルゴリズム基礎 480 アクティブ分(8時間の集中学習)
01 120 分

テンソル:軸・階数・モデルの重みブロック

予測と観察の証跡を記録した後のみ支援を利用できる。

前提条件

第 23 日を完了すること。行列の shape が不安なら記載の回復リンクを使う。

学習目標

テンソルを軸の数を階数とする n 次元配列として記述し、モデルのテンソルのバッチ・時間・チャネル軸を名付け、shape (V x d) のテーブル E からの埋め込み参照が shape (B, T, d) の出力になることを追跡する。

手順

スカラーは階数 0、ベクトルは階数 1、行列は階数 2、埋め込み系列のバッチは shape (B, T, d) の階数 3 テンソル:バッチ軸・時間軸・チャネル軸。埋め込み参照は行の gather であり、shape (B, T) の ids が shape (V, d) のテーブル E を参照して (B, T, d) の出力を作る。重み行列は階数 2 のテンソル、バッチと時間を持つ隠れ状態は階数 3 である。

演習と予測記録

語彙 V = 50000、埋め込み幅 d = 512 の小さなモデルがある。予測せよ:(a) テーブル E のスカラー重み数;(b) shape (B=2, T=3) の ids を参照したときの出力 shape;(c) その出力が保持する float32 の値の個数とバイト数。

独立習得基準

台帳が (V, d) と (B, T) から (B, T, d) を自力で導き、2つの要素数を正確に計算し、各軸の物理的意味(どの項目・どの位置・どの特徴か)を述べられる。

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

linking BSD 3-Clause License

NumPy Reference: The N-dimensional array (ndarray)

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

該当セクション・タイムスタンプ: ndarray.ndim / ndarray.shape and axis ordering

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Primary semantics for ndarray shape, strides, reshape orders, and view-versus-copy behavior used in tensor memory accounting.

ソース前提知識:

Basic Python

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

Shape, stride, and memory-ledger exercises are fully explained with local runnable examples.

比較対象候補リソースと選定評価 (2)
PyTorch Documentation: torch.Tensor ↗ compared_alternative [Modified BSD]

Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.

NumPy User Guide: Broadcasting ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.

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

Essence of Linear Algebra

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

該当セクション・タイムスタンプ: Chapter 3 review: a matrix is a rank-2 tensor

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

選定理由:

Unequaled geometric and visual intuition for vectors, linear transformations, matrix multiplication, and inverses before tensor operations.

ソース前提知識:

High-school coordinate geometry

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

Geometric explanations, worked formulas, and coordinate exercises are fully provided as original lesson text.

比較対象候補リソースと選定評価 (2)
MIT OCW 18.06 Linear Algebra (Prof. Gilbert Strang) ↗ compared_alternative [CC BY-NC-SA 4.0]

Established textbook/lecture approach, strong mathematical rigor alongside 3Blue1Brown visual intuition.

Linear Algebra Done Right (Sheldon Axler) ↗ compared_rejected_for_curriculum [CC BY-NC (4th edition, Open Access)]

Theoretical determinant-free proof structure is mathematically elegant but less aligned with computational tensor engineering.

02 120 分

ブロードキャスト規則と失敗の診断

独立演習:結果確認前に予測を記録し、証跡を記した後のみ支援を利用。

前提条件

前の活動を完了すること。詰まったら記載の回復リンクを使う。

学習目標

ブロードキャスト規則——末尾の軸から揃え、各次元ペアが等しいか片方が 1 であること——を適用し、コードを実行する前に出力と失敗を予測・診断する。

手順

ブロードキャストは任意階数のテンソルに適用される:shape を右から左に比較する。欠けた軸はサイズ 1 と数え、サイズ 1 の軸はメモリをコピーせず引き伸ばされる。MLP のバイアス加算 (B, T, d_ff) + (d_ff,) はバイアスをバッチと時間にブロードキャストする。(2, 3) と (4,) の組は末尾軸が 3 != 4 で失敗する。

演習と予測記録

各結果または失敗を予測せよ:(a) [[1,2,3],[4,5,6]] + [10,20,30];(b) [[1],[2]] + [[10,20,30]];(c) shape (2,3) + shape (4,);(d) 隠れ状態 (32,128,768) + バイアス (768,)。

独立習得基準

4つの結果(3つの shape と1つの失敗)を無支援で予測し、2つの数値結果を正確に再現し、(2,3) + (4,) が末尾軸規則で失敗しなければならない理由を説明できる。

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

linking BSD 3-Clause License

NumPy User Guide: Broadcasting

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

該当セクション・タイムスタンプ: General broadcasting rules and incompatible-shape errors

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Normative definition of broadcasting rules, shape mismatch errors, and implicit axis expansion shared by NumPy, PyTorch, and other tensor frameworks.

ソース前提知識:

Basic Python and array indexing

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

Broadcasting rules, failure cases, and memory-layout caveats are fully explained with local runnable examples.

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

Equivalent rules for PyTorch tensors; NumPy chosen because Day 24 exercises run without a PyTorch dependency, while the rule wording is identical.

NumPy Reference: The N-dimensional array (ndarray) ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: normative shape/strides/reshape semantics complementing the broadcasting rules page.

linking BSD 3-Clause License

NumPy Reference: The N-dimensional array (ndarray)

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

該当セクション・タイムスタンプ: Implicit dimension expansion for element-wise ops

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Primary semantics for ndarray shape, strides, reshape orders, and view-versus-copy behavior used in tensor memory accounting.

ソース前提知識:

Basic Python

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

Shape, stride, and memory-ledger exercises are fully explained with local runnable examples.

比較対象候補リソースと選定評価 (2)
PyTorch Documentation: torch.Tensor ↗ compared_alternative [Modified BSD]

Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.

NumPy User Guide: Broadcasting ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.

03 120 分

reshape、ビュー対コピー、メモリ台帳

予測と観察の証跡を記録した後のみ支援を利用できる。

前提条件

前の活動を完了すること。詰まったら記載の回復リンクを使う。

学習目標

要素数が不変で順序が行優先(row-major)であることを踏まえて reshape の結果を予測し、ビューとコピーを区別する(転置は非連続ビューなので転置後の reshape はコピーしうる)。実モデルのテンソルで「要素数 × 要素サイズ」のメモリ台帳を付ける。

手順

reshape は同じフラットバッファを C(行優先)順で読み直すので要素数は保存される。多くの reshape とすべての素直な転置はビュー——同じメモリへの新しい軸メタデータ——を返す。非連続テンソル(例:.T の直後)は reshape にコピーを強いることがある。バイト数 = 要素数 * 要素サイズ。float32 は 4 バイト。

演習と予測記録

shape (2,3) の a = [[1,2,3],[4,5,6]] とする。予測せよ:(a) a を (3,2) に reshape;(b) a を (6,) に flatten;(c) a.T.reshape(6) が a のメモリを共有できるか;(d) shape (32, 128, 768) の float32 隠れ状態のバイト数。

独立習得基準

4つすべてを予測し、reshape の要素数不変を述べ、転置の例でビュー対コピーの注意点と 12 MiB の台帳項目を説明できる。

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

linking BSD 3-Clause License

NumPy Reference: The N-dimensional array (ndarray)

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

該当セクション・タイムスタンプ: ndarray.reshape, ndarray.T, strides, and non-contiguous copies

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Primary semantics for ndarray shape, strides, reshape orders, and view-versus-copy behavior used in tensor memory accounting.

ソース前提知識:

Basic Python

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

Shape, stride, and memory-ledger exercises are fully explained with local runnable examples.

比較対象候補リソースと選定評価 (2)
PyTorch Documentation: torch.Tensor ↗ compared_alternative [Modified BSD]

Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.

NumPy User Guide: Broadcasting ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.

linking BSD 3-Clause License

NumPy User Guide: Broadcasting

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

該当セクション・タイムスタンプ: Broadcasting never materializes the expanded axes

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Normative definition of broadcasting rules, shape mismatch errors, and implicit axis expansion shared by NumPy, PyTorch, and other tensor frameworks.

ソース前提知識:

Basic Python and array indexing

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

Broadcasting rules, failure cases, and memory-layout caveats are fully explained with local runnable examples.

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

Equivalent rules for PyTorch tensors; NumPy chosen because Day 24 exercises run without a PyTorch dependency, while the rule wording is identical.

NumPy Reference: The N-dimensional array (ndarray) ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: normative shape/strides/reshape semantics complementing the broadcasting rules page.

04 120 分

独立習得:テンソル shape とブロードキャスト監査

独立習得チェック(AI支援なし)。

前提条件

前の活動を完了すること。詰まったら記載の回復リンクを使う。

学習目標

支援なしでレイヤーノーム型の計算を端から端まで監査する:keepdims 付き削減の shape、ブロードキャスト減算、ブロードキャスト失敗、メモリ台帳を予測し、間違いがあれば回復メモを書く。

手順

閉書で行う:まず予測、次に計算、最後に比較。各テンソルの shape を値の横に書く。意外な結果が出たら、どの規則を誤用したかを記録し、回復リンクをメモする。

演習と予測記録

課題:(a) shape (2,3) の x = [[1,2,3],[4,5,6]] について、最後の軸で keepdims 付きの平均とその shape;(b) x - mean のブロードキャスト結果;(c) (2,3)+(3,)、(2,3)+(4,)、(2,1)+(1,3) のうち失敗するのはどれか、なぜか;(d) shape (2, 3, 50000) の float32 logits テンソルのバイト数。

独立習得基準

4つの答えをすべて無支援で検証し、値の横に shape を記し、ブロードキャスト失敗規則と、削減で keepdims が存在する理由を一文で述べられる。

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

linking BSD 3-Clause License

NumPy Reference: The N-dimensional array (ndarray)

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

該当セクション・タイムスタンプ: Full ndarray attribute reference for self-check

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Primary semantics for ndarray shape, strides, reshape orders, and view-versus-copy behavior used in tensor memory accounting.

ソース前提知識:

Basic Python

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

Shape, stride, and memory-ledger exercises are fully explained with local runnable examples.

比較対象候補リソースと選定評価 (2)
PyTorch Documentation: torch.Tensor ↗ compared_alternative [Modified BSD]

Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.

NumPy User Guide: Broadcasting ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.

linking BSD 3-Clause License

NumPy User Guide: Broadcasting

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

該当セクション・タイムスタンプ: Broadcasting rules reference for self-check

検証バージョンと日付: NumPy 2.x documentation (Verified: 2026-10-30)

選定理由:

Normative definition of broadcasting rules, shape mismatch errors, and implicit axis expansion shared by NumPy, PyTorch, and other tensor frameworks.

ソース前提知識:

Basic Python and array indexing

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

Broadcasting rules, failure cases, and memory-layout caveats are fully explained with local runnable examples.

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

Equivalent rules for PyTorch tensors; NumPy chosen because Day 24 exercises run without a PyTorch dependency, while the rule wording is identical.

NumPy Reference: The N-dimensional array (ndarray) ↗ compared_selected_for_days_22_24 [BSD 3-Clause License]

Selected as co-primary for Day 24: normative shape/strides/reshape semantics complementing the broadcasting rules page.