23 / 90

第 23 天:矩阵乘法、shape 台账、转置与逆矩阵

把矩阵乘法当作线性映射的复合来计算,为每个乘积写下明确的 (m x k)(k x n) -> m x n shape 台账,并建立转置、单位矩阵与逆矩阵的直觉。把每个乘积与模型内部的权重矩阵联系起来。每项活动都在 480 分钟台账中记录预测、观察、复习与恢复。

数学与算法基础 480 活跃分钟(8 小时专注学习)
01 120 分钟

矩阵乘法即线性映射的复合

仅在记录预测与观察证据后才可使用协助。

前置要求

完成第 22 天;若矩阵-向量乘积不熟练,使用列出的恢复链接。

学习目标

按行乘列的点积逐项计算矩阵乘积,把 A*B 解释为『先施加 B、再施加 A』的复合映射,并实证矩阵乘法不可交换。

操作指引

元素 (A*B)_ij 是 A 的第 i 行与 B 的第 j 列的点积。几何上 (A*B)*x = A*(B*x):乘积矩阵先施加内层映射,所以复合要从右往左读。复合依赖顺序,因此 A*B 与 B*A 一般不相等。

自主演练与记录

设 A = [[1,2],[3,4]],B = [[0,1],[1,0]](B 交换向量的分量)。计算前先预测两个乘积:(a) A*B;(b) B*A。二者相等吗?

独立掌握标准

正确算出两个乘积,解释从右向左的复合读法,并举出一个几何理由说明两种顺序为何不同(先交换再映射 vs 先映射再交换)。

权威源资源与权利证明

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) | 原始出处链接 ↗

对应章节 / 时间戳: Chapter 4: Matrix multiplication as composition

验证版本与日期: 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.

linking CC BY-NC-SA 4.0 noncommercial share-alike

18.06 Linear Algebra (Prof. Gilbert Strang)

作者 / 机构: MIT OpenCourseWare (Prof. Gilbert Strang) | 原始出处链接 ↗

对应章节 / 时间戳: Lecture 3: matrix multiplication and its four viewpoints

验证版本与日期: Spring 2010 (Verified: 2026-10-30)

入选理由:

Established row-picture and column-picture treatment of linear systems, matrix products, and invertibility; rigorous complement to the 3Blue1Brown visual series.

来源前置条件:

High-school algebra and coordinate geometry

安全回退方案(离线弹性):

All row/column-picture derivations and worked examples are written as original lesson text and run offline.

对比候选资源与选型分析 (3)
3Blue1Brown: Essence of Linear Algebra ↗ compared_selected_for_days_22_24 [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Selected as co-primary: geometric visualizations paired with Strang's algebraic rigor.

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

Determinant-free theoretical treatment is elegant but further from the computational shape accounting required here.

Immersive Linear Algebra (online interactive book) ↗ compared_rejected_for_days_22_24 [Free online (site terms; no OSI/CC license declared)]

Interactive figures are attractive, but the absence of a declared open license makes rights disposition unclear; kept out of the resource set.

02 120 分钟

shape 台账:预测乘积的维度

独立练习:先记录预测再核对结果;记录证据后才可使用协助。

前置要求

完成上一活动;如受阻,使用列出的恢复链接。

学习目标

为每个乘积写书面 shape 台账:(m x k)(k x n) -> (m x n);内侧维度必须匹配,不匹配是要预测的错误,而不是死记的规则。

操作指引

写下两边的 shape,检查内侧维度是否一致,然后在计算任何元素之前先预测输出 shape。同样的台账用于模型模块:MLP 把 shape 为 (batch x d_model) 的 X 依次乘以 W1 (d_model x d_ff) 和 W2 (d_ff x d_model)。每个权重矩阵都是模型内部的 2 秩张量。

自主演练与记录

预测 shape 与数值。(a) C (2x3) = [[1,0,2],[-1,3,1]] 乘 D (3x2) = [[3,1],[2,1],[1,0]]:输出 shape 是什么?算出全部四个元素。(b) Transformer MLP 台账:X (32 x 768) * W1 (768 x 3072),再 * W2 (3072 x 768)。(c) (2x3)(2x3) 合法吗?

独立掌握标准

计算前预测每个输出 shape,精确复现 C*D = [[5,1],[4,2]],并用一个失败例子说明『内侧维度必须匹配』这条规则。

权威源资源与权利证明

linking CC BY-NC-SA 4.0 noncommercial share-alike

18.06 Linear Algebra (Prof. Gilbert Strang)

作者 / 机构: MIT OpenCourseWare (Prof. Gilbert Strang) | 原始出处链接 ↗

对应章节 / 时间戳: Lecture 3: (m x k)(k x n) -> m x n dimension rule

验证版本与日期: Spring 2010 (Verified: 2026-10-30)

入选理由:

Established row-picture and column-picture treatment of linear systems, matrix products, and invertibility; rigorous complement to the 3Blue1Brown visual series.

来源前置条件:

High-school algebra and coordinate geometry

安全回退方案(离线弹性):

All row/column-picture derivations and worked examples are written as original lesson text and run offline.

对比候选资源与选型分析 (3)
3Blue1Brown: Essence of Linear Algebra ↗ compared_selected_for_days_22_24 [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Selected as co-primary: geometric visualizations paired with Strang's algebraic rigor.

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

Determinant-free theoretical treatment is elegant but further from the computational shape accounting required here.

Immersive Linear Algebra (online interactive book) ↗ compared_rejected_for_days_22_24 [Free online (site terms; no OSI/CC license declared)]

Interactive figures are attractive, but the absence of a declared open license makes rights disposition unclear; kept out of the resource set.

linking BSD 3-Clause License

NumPy Reference: The N-dimensional array (ndarray)

作者 / 机构: NumPy Developers | 原始出处链接 ↗

对应章节 / 时间戳: ndarray.shape semantics and matrix product dimensions

验证版本与日期: 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 分钟

转置、单位矩阵与逆矩阵的直觉

仅在记录预测与观察证据后才可使用协助。

前置要求

完成上一活动;如受阻,使用列出的恢复链接。

学习目标

把转置当作沿对角线的镜像使用,掌握 (A*B)^T = B^T * A^T;把单位矩阵 I 当作『什么都不做』的映射;把 A^-1 读作撤销 A 的映射——只有当 A 没有压扁任何方向时才存在。

操作指引

转置交换行与列:(A^T)_ij = A_ji,乘积取转置时顺序反转。单位矩阵 I 满足 I*x = x。逆矩阵 A^-1 满足 A*A^-1 = I。零空间非平凡的矩阵会把某个方向压成零,任何逆矩阵都无法复原它;对 2x2 矩阵,det = ad - bc = 0 就是这种压扁的信号。

自主演练与记录

(a) 用上一活动的 C*D = [[5,1],[4,2]],预测 (C*D)^T 并验证它等于 D^T * C^T。(b) 对 A = [[2,1],[1,1]],通过计算 A * A^-1 验证 A^-1 = [[1,-1],[-1,2]]。(c) S = [[1,2],[2,4]]:求 det(S),并找出使 S*x = 0 的非零 x;S 可逆吗?

独立掌握标准

逐元素验证 A * A^-1 = I,用数值实证转置的乘积规则,并用 S*(2,-1) = 0 解释『奇异就是某个方向被摧毁』。

权威源资源与权利证明

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) | 原始出处链接 ↗

对应章节 / 时间戳: Chapters 5-6: determinants and inverse matrices

验证版本与日期: 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.

linking CC BY-NC-SA 4.0 noncommercial share-alike

18.06 Linear Algebra (Prof. Gilbert Strang)

作者 / 机构: MIT OpenCourseWare (Prof. Gilbert Strang) | 原始出处链接 ↗

对应章节 / 时间戳: Lecture 3: inverses and transposes; Lecture 4: factorization

验证版本与日期: Spring 2010 (Verified: 2026-10-30)

入选理由:

Established row-picture and column-picture treatment of linear systems, matrix products, and invertibility; rigorous complement to the 3Blue1Brown visual series.

来源前置条件:

High-school algebra and coordinate geometry

安全回退方案(离线弹性):

All row/column-picture derivations and worked examples are written as original lesson text and run offline.

对比候选资源与选型分析 (3)
3Blue1Brown: Essence of Linear Algebra ↗ compared_selected_for_days_22_24 [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Selected as co-primary: geometric visualizations paired with Strang's algebraic rigor.

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

Determinant-free theoretical treatment is elegant but further from the computational shape accounting required here.

Immersive Linear Algebra (online interactive book) ↗ compared_rejected_for_days_22_24 [Free online (site terms; no OSI/CC license declared)]

Interactive figures are attractive, but the absence of a declared open license makes rights disposition unclear; kept out of the resource set.

04 120 分钟

独立掌握:乘积、shape 与可逆性

独立掌握检查(无 AI 协助)。

前置要求

完成上一活动;如受阻,使用列出的恢复链接。

学习目标

在无助协助下计算 2x2 乘积、预测注意力 shape 台账、判定给定矩阵是否可逆;任何错误都要写出恢复笔记。

操作指引

闭卷进行:先预测,再计算,后比较。乘之前先记录每个 shape。若结果出乎意料,记录误用了哪条规则并记下恢复链接。

自主演练与记录

任务:(a) P = [[1,1],[0,1]]、M = [[2,0],[1,1]] 时的 P*M;(b) 注意力台账:Q (8 x 64) 乘 K^T(K 为 (8 x 64)),再过 softmax,再乘 V (8 x 64):预测每个输出 shape;(c) [[2,4],[1,2]] 可逆吗?

独立掌握标准

所有预测经无协助验证;用自己的话写出 (8x8) 注意力分数 shape 与奇异性论证,并附完整 shape 台账。

权威源资源与权利证明

linking CC BY-NC-SA 4.0 noncommercial share-alike

18.06 Linear Algebra (Prof. Gilbert Strang)

作者 / 机构: MIT OpenCourseWare (Prof. Gilbert Strang) | 原始出处链接 ↗

对应章节 / 时间戳: Lecture 1-3 problem sets on products and inverses

验证版本与日期: Spring 2010 (Verified: 2026-10-30)

入选理由:

Established row-picture and column-picture treatment of linear systems, matrix products, and invertibility; rigorous complement to the 3Blue1Brown visual series.

来源前置条件:

High-school algebra and coordinate geometry

安全回退方案(离线弹性):

All row/column-picture derivations and worked examples are written as original lesson text and run offline.

对比候选资源与选型分析 (3)
3Blue1Brown: Essence of Linear Algebra ↗ compared_selected_for_days_22_24 [3Blue1Brown copyright (all rights reserved); site FAQ permits only short clips under stated conditions]

Selected as co-primary: geometric visualizations paired with Strang's algebraic rigor.

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

Determinant-free theoretical treatment is elegant but further from the computational shape accounting required here.

Immersive Linear Algebra (online interactive book) ↗ compared_rejected_for_days_22_24 [Free online (site terms; no OSI/CC license declared)]

Interactive figures are attractive, but the absence of a declared open license makes rights disposition unclear; kept out of the resource set.

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) | 原始出处链接 ↗

对应章节 / 时间戳: Chapters 4-6 review

验证版本与日期: 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.