張量:軸、秩與模型權重區塊
僅在記錄預測與觀察證據後才可使用協助。
前置要求
完成第 23 天;若矩陣 shape 不熟練,使用列出的復原連結。
學習目標
把張量描述為秩等於軸數的 n 維陣列;說出模型張量的 batch、time、channel 軸;並追蹤從 shape 為 (V x d) 的表 E 做嵌入查找得到 shape (B, T, d) 輸出的全過程。
操作指引
純量是 0 秩,向量是 1 秩,矩陣是 2 秩,一批嵌入序列是 shape (B, T, d) 的 3 秩張量:batch 軸、time 軸、channel 軸。嵌入查找就是按行 gather:shape (B, T) 的 ids 查表 E (V, d),產出 (B, T, d)。權重矩陣是 2 秩張量;帶 batch 與 time 的隱藏狀態是 3 秩。
自主實作與記錄
某小模型詞表 V = 50000,嵌入寬度 d = 512。預測:(a) 表 E 中的純量權重數;(b) shape (B=2, T=3) 的 ids 查表後的輸出 shape;(c) 該輸出裝了多少個 float32 值以及佔多少位元組。
權重數是 V * d。查找結果為每個 (batch, time) 位置放一個 d 維向量。float32 每值 4 位元組。
(a) E 含 50000 * 512 = 25,600,000 個純量權重。(b) 輸出 shape 為 (2, 3, 512):2*3 = 6 個詞元位置各自換成一個 512 維向量。(c) 輸出含 2*3*512 = 3072 個 float32 值,即 3072 * 4 = 12,288 位元組。batch 與 time 軸是查找在行向量前面新增的兩個軸。
不借助提示從 (V, d) 與 (B, T) 推出 (B, T, d),精確算出兩個元素計數,並能說出每個軸的物理意義(哪個樣本、哪個位置、哪個特徵)。
權威來源資源與權利證明
NumPy Reference: The N-dimensional array (ndarray)
對應章節 / 時間戳: 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)
Equivalent tensor semantics in the framework used later in the curriculum; NumPy reference selected to keep Days 22-24 dependency-free.
Selected as co-primary for Day 24: the broadcasting rules page pairs with the ndarray attribute reference.
Essence of Linear Algebra
對應章節 / 時間戳: 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)
Established textbook/lecture approach, strong mathematical rigor alongside 3Blue1Brown visual intuition.
Theoretical determinant-free proof structure is mathematically elegant but less aligned with computational tensor engineering.