张量:轴、秩与模型权重块
仅在记录预测与观察证据后才可使用协助。
前置要求
完成第 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.