The summation notation Σ represents the sum of several similar terms based on a range of consecutive integers. Σₙ₌₂¹⁰ n = 2+3+…+10; Σₘ₌₁⁵ xᵐ = x¹+x²+…+x⁵. It is especially useful for a variable, or an infinite, number of terms: Σᵢ₌₀ⁿ aᵢxⁱ = a₀x⁰ + a₁x¹ + ⋯ + aₙxⁿ; Σᵢ₌₁^∞ 1/xⁱ = 1/x¹ + 1/x² + 1/x³ + ⋯
核心概念Σ 的三个部件:下标变量(i)、下限(i=0)、上限(n)。读作"把 i 从下限取到上限,每一项相加"。
详细讲解
读法:Σₙ₌₂¹⁰ n → "n 从 2 到 10,把 n 加起来" → 2+3+4+5+6+7+8+9+10 = 54(等差数列 (2+10)×9/2 = 54,已复算)。
下标变量会"代入":Σₘ₌₁⁵ xᵐ → m 依次取 1,2,3,4,5,代入 x 的指数 → x¹+x²+x³+x⁴+x⁵。
An artificial neuron, or neuron, performs summation and activation. xᵢ (i = 1…n) are inputs, wᵢ (i = 1…n) are weights, b is bias; x₀ ≡ 1, w₀ ≡ b. The summation z is z = w₁x₁ + w₂x₂ + ⋯ + wₙxₙ + b = Σᵢ₌₁ⁿ wᵢxᵢ + b = Σᵢ₌₀ⁿ wᵢxᵢ. With an activation function f, the output y of the neuron is y = f(z) = f(Σᵢ₌₀ⁿ wᵢxᵢ).
Linear algebra is the study of linear equations and their representations using vectors and matrices. Vectors and matrices are often used for storing 1D and 2D structured data in machine learning. They provide concise notations for representing the data and their operations, and match the conventions of the NumPy library.
易错点 · 理解提示"match the conventions of the NumPy library"是个提示:考试写代码时,数学式里的 𝐀𝐱 直接写成 A @ x 即可。
p8Vectors —— 什么是向量原图
Vector (x) is an n-tuple of values, i.e. containing n values. n is the dimension of the vector, n ≥ 1. Represent a point in n-dimensional space, or a directed line segment with magnitude (∥x∥) and direction in that space. E.g. a 2D vector (1, 2) and a 3D vector (1, 2, 2).
A vector x contains n elements xᵢ, i = 1…n; xᵢ is the i-th element of x. x can be written in row form x = [x₁ x₂ … xₙ] or in column form x = [x₁ x₂ ⋮ xₙ]ᵀ. NumPy: np.array(data_list).
Keras input_shape=(3,) 对应最后一行——它只描述"每个样本有 3 个数",不指定行列。
易错点 · 理解提示1D 数组 (3,) 与 2D 列向量 (3,1) 在 NumPy 里不相等:前者 a.T 还是 (3,);后者 a.T 是 (1,3)。Lab 3 Q1a 的报错 size 5 is different from 3(p16 会解释)就是形状惹的祸。
p10Matrices定义
An m × n matrix, A_{m×n}, is a 2D array, or table, of values in m rows and n columns; aᵢⱼ denotes the element in the i-th row and j-th column. A vector is a special case of a matrix, with one of the two dimensions being one. NumPy: np.array(data_list).
核心概念矩阵(matrix):m 行 n 列的数表,形状 m×n;aᵢⱼ = 第 i 行第 j 列的元素(先行后列)。向量是矩阵的特例(某一维为 1)。
The row form and column form of a vector are transposes (T) of each other. E.g. the transpose of the row vector x = [10 20 30] is the column vector xᵀ = [10 20 30]ᵀ. The transpose of the transpose of a vector x is equal to x: (xᵀ)ᵀ = x. NumPy: a.T, np.transpose(a).
易错点 · 理解提示① T 是上标:xᵀ 不是 x 的 T 次方。② 1D 数组转置"无效"是个经典坑——做 np.dot(a, b) 时 a、b 都是 (3,) 会得到标量(点积),而 (3,1) 与 (1,3) 相乘会得到 3×3 矩阵(外积)。Unit 2 复现 Keras 时我就踩过这个坑(y 没 reshape 导致广播成 4×4)。
p12Transposes of matrices🧮 会算
The transpose of a matrix, denoted by T, swaps the columns and rows of the matrix. The transpose of an m × n matrix is an n × m matrix. E.g. the transpose of A = [[10 20 30],[4 5 6]] is Aᵀ = [[10 4],[20 5],[30 6]]. NumPy: a.T, np.transpose(a).
核心概念矩阵转置:第 i 行变成第 i 列;形状 m×n → n×m;元素 aᵢⱼ 换到 aⱼᵢ 的位置。
p13Element-wise operations on vectorsLab 3 Cell 10
Vector addition means adding the corresponding elements of two vectors: x + y = [x₁+y₁ x₂+y₂ … xₙ+yₙ]. Scalar multiplication of a vector means multiplying a scalar (i.e. a number, not a vector) to each element: c·x = [c·x₁ c·x₂ … c·xₙ]. NumPy: a1+a2, c*a1.
易错点 · 理解提示* 与 @ 的区别是 Lab 3 的核心考点:a * b = [10 40 90](逐元素,向量),a @ b = 140(点积,标量)。写错符号结果类型完全不同。
p14Element-wise operations on matricesLab 3 Cell 18
Matrix addition means adding the corresponding elements of the two matrices. Scalar multiplication of a matrix means multiplying the scalar to each element. If A = [aᵢⱼ], B = [bᵢⱼ], c is a scalar, then A + B = [aᵢⱼ + bᵢⱼ], c·A = [c·aᵢⱼ]. NumPy: a1+a2, c*a1.
The dot product is obtained by multiplying the corresponding elements of the two vectors and adding the resulting products. If x = [x₁ … xₙ], y = [y₁ … yₙ], then x·y = x₁y₁ + x₂y₂ + ⋯ + xₙyₙ = Σᵢ₌₁ⁿ xᵢyᵢ. Alternatively, use the magnitudes ∥x∥, ∥y∥ and the angle θ between them: x·y = ∥x∥∥y∥ cos θ. NumPy: a1.dot(a2), np.dot(a1,a2), a1@a2.
The matrix-vector product is obtained by the dot products of the matrix's rows and the vector. The column (second) dimension of the matrix must equal the dimension of the vector. If A_{m×n} = [aᵢⱼ] and x_{n×1} = [xᵢ], then Ax = C_{m×1} = [cᵢ], where cᵢ = aᵢ₁x₁ + aᵢ₂x₂ + ⋯ + aᵢₙxₙ = Σₖ₌₁ⁿ aᵢₖxₖ. E.g. [[1 2 3],[4 5 6]]·[7 8 9]ᵀ = [1×7+2×8+3×9 ; 4×7+5×8+6×9] = [50 ; 122]. NumPy: a1.dot(a2), np.dot, @, np.matmul.
核心概念矩阵 × 向量:矩阵的每一行与向量做点积,得到一个新向量(每行一个数)。形状规则:矩阵的列数 n 必须等于向量的维度 n。
详细讲解
算法:结果的第 i 个元素 = A 的第 i 行 · x。所以 Ax 就是"对 A 的每一行做一次 p15 的点积"。
形状:(m×n) · (n×1) → (m×1)。口诀"内维相等,外维定形"——中间的两个 n 必须相等,结果的形状是外层的 m×1。
易错点 · 理解提示① 顺序不能换:Ax 可以不代表 xA 可以——(n,)·(m,n) 形状不对。② 结果的行数 = 矩阵的行数,与向量无关。③ NumPy 对 1D 向量 A @ x(x 是 (n,))返回 (m,) 而非 (m,1)——数学上是列向量,代码里是 1D。写 Keras 时要注意。
p17Matrix multiplication★ 必考🧮 会算Lab 3 Cell 16
The matrix product of two matrices is obtained by the dot products of the corresponding rows and columns. If A_{m×n} = [aᵢⱼ] and B_{n×p} = [bᵢⱼ], then AB = C_{m×p} = [cᵢⱼ], where cᵢⱼ = aᵢ₁b₁ⱼ + aᵢ₂b₂ⱼ + ⋯ + aᵢₙbₙⱼ = Σₖ₌₁ⁿ aᵢₖbₖⱼ. E.g. [[1 2 3],[4 5 6]]·[[7 8],[9 10],[11 12]] = [[1×7+2×9+3×11, 1×8+2×10+3×12],[4×7+5×9+6×11, 4×8+5×10+6×12]] = [[58 64],[139 154]].
核心概念矩阵乘法:结果第 (i, j) 个元素 = A 的第 i 行 · B 的第 j 列。形状 (m×n)·(n×p) → m×p。
易错点 · 理解提示① 不是"对应元素相乘"——那是 A * B(逐元素,要求同形状)。A @ B 是行乘列。② 结果的 (i, j) 来自 A 的第 i 行和 B 的第 j 列,别用成 A 的第 j 行。③ 试卷 Q2 的 A·B,左上元素用的是 A 的第 1 行 [2 −1] 和 B 的第 1 列 [1 2]ᵀ,不是 B 的第 1 行。
p18Some remarks on matrix multiplication★ 试卷 Q2 直接考
The column (second) dimension of the first matrix must equal the row (first) dimension of the second matrix: A_{m×n}B_{n×p}. Matrix multiplication is associative: A(BC) = (AB)C. Matrix multiplication is not commutative in general: AB ≠ BA. The transpose of a matrix product equals the product of the transposed matrices in reverse order: (AB)ᵀ = BᵀAᵀ.
核心概念四条性质:① 形状条件(内维相等);② 结合律成立;③ 交换律不成立;④ 转置反序。
详细讲解
① 形状条件:A_{m×n} B_{n×p}——A 的列数 = B 的行数 = n。这是"能不能乘"的判据,试卷 Q2c/Q2d 就在问这个。
必要扩展另两条常考性质: 分配律:A(B + C) = AB + AC,(A + B)C = AC + BC(成立)。 单位矩阵:AI = IA = A(I 像数字 1)。 没有消去律:AB = AC 推不出 B = C(A 可能"压缩"了信息)。这与 Unit 6 讲的"矩阵不可逆/奇异"有关,课程不深究。
易错点 · 理解提示"不能交换"的最常见后果:写代码时 x @ W 与 W @ x 形状不同。Keras 的 Dense 权重存成 (input_dim, units),内部算的是 x @ W + b(x 是行向量批次);而本讲 p26 的数学约定是 Wx(x 是列向量)——所以 Keras 权重矩阵是本讲 W 的转置。Lab 3 Q2 的 weights.reshape((3, 1)) 就是在对齐这个约定。
A dataset can be shown or stored in a table or a 2D array. Each row is a labelled example, and each column is a feature or label. The dataset and its parts can be denoted as matrices and vectors for applying linear algebra operations.
讲义图中的表格:5 列——Name、Age、Height、Years of education、Speak French、Income,3 行——Peter、Mary、David。
标注:"An example"(虚线框住 Peter 那一行)、"3 examples"(大括号括住三行)、"5 features"(下方大括号:Name、Age、Height、Years of education、Speak French)、"A feature"(虚线框住 Years of education 这一列)、"Label"(虚线框住 Income 列)。
所以:行 = 样本,列 = 特征(5 个)或标签(1 个)。表格共 3×6 = 18 个格子。
这和 Keras 的 X_train.shape = (20000, 8)(试卷 Q7a)是同一回事:20000 行样本、8 列特征。
易错点 · 理解提示讲义说"each row is a labelled example"——注意 labelled:在监督学习里,每个样本带着它的标签;标签本身占一列(或单独一个数组)。
p21Denoting data as matrices and vectors —— 特征矩阵 X★ 必考原图
Consider a dataset of 3 examples, 5 features, 1 label. The features of all examples can be denoted as a 3 × 5 matrix X₃ₓ₅ = [x₁⁽¹⁾ x₂⁽¹⁾ x₃⁽¹⁾ x₄⁽¹⁾ x₅⁽¹⁾; x₁⁽²⁾ x₂⁽²⁾ … x₅⁽²⁾; x₁⁽³⁾ … x₅⁽³⁾].
The features of a general example can be denoted as a feature vector x, in the form of a column or row vector: x = [x₁ x₂ x₃ x₄ x₅]ᵀ or x = [x₁ x₂ x₃ x₄ x₅]. The features of a particular example, e.g. example 1, is denoted as x⁽¹⁾ = [x₁⁽¹⁾ x₂⁽¹⁾ x₃⁽¹⁾ x₄⁽¹⁾ x₅⁽¹⁾].
核心概念特征向量(feature vector) x:一个样本的所有特征组成的向量。可以写成列向量或行向量。第 i 个样本记作 x⁽ⁱ⁾。
The weights and inputs of a neuron are denoted as a row vector and a column vector (x₀ ≡ 1, w₀ ≡ b): w = [w₀ w₁ … wₙ], xᵀ = [x₀ x₁ … xₙ]. Then the summation z can be computed as a dot product: z = w₀x₀ + w₁x₁ + ⋯ + wₙxₙ = Σᵢ₌₀ⁿ wᵢxᵢ = [w₀ w₁ … wₙ]·[x₀ x₁ ⋮ xₙ] = w·x. With an activation function f, output of the neuron is y = f(w·x).
核心概念一个神经元的求和 = 一次点积z = w·x,输出 y = f(w·x)。权重是行向量(1×(n+1)),输入是列向量((n+1)×1),偏置藏在 w₀、x₀=1。
易错点 · 理解提示① y = f(w·x)——f 在点积外面,先算完 z 再激活,不是 f(w)·f(x)。② 点积要求 w 和 x 同维:n+1 个权重对 n+1 个输入(含 x₀)。
p25Dense layer operations —— 权重矩阵 W★ 必考原图
Consider a dense layer of m neurons and n inputs. The inputs are denoted as a vector (x₀ ≡ 1): xᵀ = [x₀ x₁ x₂ … xₙ]. The weights of the m neurons are denoted as a matrix (wᵢ₀ ≡ bᵢ): W = [w₁₀ w₁₁ w₁₂ … w₁ₙ; w₂₀ w₂₁ w₂₂ … w₂ₙ; ⋮; wₘ₀ wₘ₁ wₘ₂ … wₘₙ].
Then the summation z of the m neurons can be computed as a matrix-vector product: z = [z₁ z₂ … zₘ]ᵀ = W·[x₀ x₁ x₂ ⋮ xₙ] = Wx. With an activation function f, the outputs y of the m neurons are y = [y₁ y₂ … yₘ]ᵀ = f(Wx).
ML acquires statistical information about data to build a model for prediction or analysis of future data. Some ML techniques and algorithms employ statistical techniques, e.g. standardization in data preprocessing.
核心概念ML 从数据中提取统计信息来建模;统计技术直接用于 ML 的例子:标准化(standardization)。
为什么要标准化:若特征 A 范围 0–1、特征 B 范围 0–10000,梯度下降会被大尺度特征主导、收敛慢。标准化后所有特征"同一尺度",训练快得多。Unit 6 会考"如何处理输入"(Course Review p7 "The parameters used by techniques when processing inputs"——即 mean 和 std)。
题目 / Tut / Lab 关联试卷 Q6 提到 "features are scaled to the range of 0-1"——那是 min-max 缩放(另一种归一化),与本页的标准化(z-score)是两种方法;Unit 6 会区分。
Consider a group of values x = {x₁, …, xₙ}. The mean (arithmetic mean) is the average: mean(x) = x̄ = (x₁ + ⋯ + xₙ)/n = (1/n)Σxᵢ. E.g. mean(1, 2, 3, 10) = 4.0. The median is the middle value when sorted: x_{(n+1)/2} when n is odd; (x_{n/2} + x_{n/2+1})/2 when n is even. E.g. median(1,2,3) = 2; median(1,2,3,10) = 2.5. NumPy: a.mean(), np.mean(a), np.median(a).
The spread, or dispersion, of a group of values describes the typical distance from a value to the mean of the values — an indication of how wide the range of majority values is. Standard deviation (SD) is the square root of the mean of the squares of the deviations (xᵢ − x̄): stdev(x) = σ = √[(1/n) Σ (xᵢ − x̄)²]. E.g. stdev(1, 2, 3, 4) = 1.12. NumPy: a.std(), np.std(a).
Variance is the square of the standard deviation, i.e. the mean of the squares of the deviations: variance(x) = σ² = (1/n) Σ (xᵢ − x̄)². E.g. variance(1, 2, 3, 4) = 1.25. NumPy: a.var(), np.var(a).
Distribution of a variable describes the number of occurrences, or frequencies, of the data values; can be visualized using histograms and frequency curves. Normal distribution (Gaussian distribution) is very common and important in statistics: bell-shaped frequency curve symmetrical about the mean; describes many natural phenomena such as people's heights and IQ scores.
Probability is a measure of how likely that an event will occur — a value between 0.0 (impossible) and 1.0 (certain). With equal likelihood, probability of an event = number of outcomes for the event ÷ total number of all possible outcomes. E.g. throwing a fair die, probability of an even number is 3/6 = 0.5.
Entropy of a variable is the amount of information contained in the variable — the amount of "surprise", or uncertainty, when you read the content of the variable. How difficult it is to guess something. E.g. the outcome of the Mark Six tomorrow has a large entropy; whether the sun rises tomorrow has a small entropy. The entropy E(X), in bits, of a variable X with n possible outcomes xᵢ of probabilities P(xᵢ): E(X) = −Σᵢ₌₁ⁿ P(xᵢ) · log₂ P(xᵢ).
Calculus, in particular multivariable differential calculus, is heavily used in deep learning optimizers. For a function f(x), the derivative f′(x) at a point is the slope of the tangent line at that point: f′(x) = Δy/Δx = change in y / change in x. Also written as f′, df(x)/dx, df/dx. (Figure: tangent line to a curve, source Wikimedia.)
Multivariable calculus refers to applying calculus to functions that involve multiple variables. For a multivariable function, the partial derivative is the derivative with respect to the variable of interest with all other variables held "fixed" during the differentiation — i.e. the other variables are treated as constants. E.g. f(x, y) = x²y³: ∂f/∂x = ∂(x²y³)/∂x = y³ · ∂(x²)/∂x = y³(2x) = 2xy³; ∂f/∂y = ∂(x²y³)/∂y = x² · ∂(y³)/∂y = x²(3y²) = 3x²y².
易错点 · 理解提示① 对 x 求偏导时 y³ 整体是系数,不要对 y³ 求导;② 结果 2xy³ 仍是 x 和 y 的函数,不是数——代 入具体 (x, y) 才得数;③ "partial"是"部分"——只对部分变量求导。
p43Gradient选修★ 梯度下降的核心
Gradient is a vector of all partial derivatives with respect to all variables of a function. E.g. f(x, y) = x²y³, the gradient of f is (∂f/∂x, ∂f/∂y) = (2xy³, 3x²y²).
p46Selected rules of (partial) derivatives选修🧮 四条规则
c, n are constants (independent of x); f, g, y are functions of x; z is a function of y. Constant multiplication rule: ∂(cf)/∂x = c ∂f/∂x. Power rule: ∂(xⁿ)/∂x = n xⁿ⁻¹. Sum rule: ∂(f+g)/∂x = ∂f/∂x + ∂g/∂x. Chain rule: ∂z/∂x = ∂z/∂y · ∂y/∂x.
核心概念四条规则:常数倍、幂、和、链式。前几条管"怎么拆",链式法则管"复合函数怎么穿过去"。
详细讲解
① 常数倍规则:∂(c·f)/∂x = c·∂f/∂x——常数可以提到导数外面。如 ∂(5x²)/∂x = 5·2x = 10x。
p47Gradient descent in linear regression —— 写出损失函数选修
The predicted result y′ = mx + b. The loss function of linear regression, MSE, is MSE = (1/n) Σ (yᵢ − yᵢ′)² = (1/n) Σ (yᵢ − (mxᵢ + b))². Gradient descent minimizes the MSE using the gradients of the parameters m and b, i.e. taking partial derivatives of MSE = f(m, b).
With the gradient of m, ∂f/∂m = (2/n)(y′ − y)·x, gradient descent updates m in code as follows. With the gradient of b, ∂f/∂b = (2/n)(y′ − y), gradient descent updates b in code as follows. Code: m_gradient = 2 * np.dot(x, predicted - y) / len(x); new_m = m - learning_rate * m_gradient; b_gradient = 2 * (predicted - y).sum() / len(x); new_b = b - learning_rate * b_gradient.
m_gradient = 2 * np.dot(x, predicted - y) / len(x) # ∂MSE/∂m = (2/n) Σ (y′−y)·x
new_m = m - learning_rate * m_gradient # m ← m − η·∂MSE/∂m
b_gradient = 2 * (predicted - y).sum() / len(x) # ∂MSE/∂b = (2/n) Σ (y′−y)
new_b = b - learning_rate * b_gradient # b ← b − η·∂MSE/∂b
题目 / Tut / Lab 关联Unit 1 的 Lab 1(t01)用过这段代码;试卷 Q3(b) 的更新式 w₇₆⁺ = w₇₆ − η ∂C/∂w₇₆ = w₇₆ − η δ₇a₆ 是同一模板:参数 ← 参数 − η × 梯度。
必要扩展整段放进循环就是完整的梯度下降(Unit 1 的骨架):
for i in range(epochs):
predicted = m * x + b
m_gradient = 2 * np.dot(x, predicted - y) / len(x)
b_gradient = 2 * (predicted - y).sum() / len(x)
m = m - learning_rate * m_gradient
b = b - learning_rate * b_gradient
Keras 的 model.fit 对 Dense 层做的事在结构上完全一样,只是梯度由反向传播(链式法则)自动算出。
易错点 · 理解提示① 两个梯度要用同一轮的 predicted 算——先算完两个 gradient 再更新 m、b,不能更新了 m 再算 b_gradient(否则就不是同步的梯度下降了)。② predicted - y 的顺序(y′−y)与更新式的负号配套:顺序反了,更新式就要变加号。
Σ represents the sum of several similar terms based on a range of consecutive integers.
p4
Vector
An n-tuple of values; n is the dimension; represents a point in n-dimensional space or a directed line segment with magnitude ∥x∥ and direction.
p8
Matrix
A 2D array of values in m rows and n columns; aᵢⱼ is the element in the i-th row and j-th column.
p10
Transpose
Swaps the rows and columns; the transpose of an m×n matrix is n×m; (Aᵀ)ᵀ = A.
p11–12
Element-wise operation
Adding / multiplying the corresponding elements of two vectors or matrices (scalar multiplication: multiply the scalar to each element).
p13–14
Dot product
Obtained by multiplying the corresponding elements of two vectors and adding the resulting products; equals ∥x∥∥y∥cos θ.
p15
Matrix (vector) product
Obtained by the dot products of the corresponding rows and columns; the column dimension of the first must equal the row dimension of the second.
p16–17
Mean / Median
Mean: the average value (1/n)Σxᵢ. Median: the middle value when the values are sorted.
p29
Standard deviation / Variance
SD is the square root of the mean of the squares of the deviations; variance is the square of the SD.
p30–31
Normal distribution
A bell-shaped frequency curve symmetrical about the mean; ~68% within 1 SD, 95% within 2 SD, 99.7% within 3 SD.
p32–34
Probability
A measure of how likely an event will occur, between 0.0 (impossible) and 1.0 (certain); with equal likelihood, outcomes for the event ÷ all possible outcomes.
p36
Entropy
The amount of information / "surprise" / uncertainty in a variable — how difficult it is to guess; E(X) = −Σ P(xᵢ) log₂ P(xᵢ) in bits.
p37
Derivative
For f(x), f′(x) at a point is the slope of the tangent line at that point, Δy/Δx.
p41
Partial derivative
The derivative with respect to the variable of interest with all other variables held fixed (treated as constants).
p42
Gradient
A vector of all partial derivatives with respect to all variables of a function.
p43
Chain rule
∂z/∂x = ∂z/∂y · ∂y/∂x.
p46
4 · 易错点 Top 12
* 与 @:a*b 逐元素(向量),a@b 点积(标量);A*B 要同形状,A@B 要内维相等。
矩阵积不可交换:AB ≠ BA(形状常不同,方阵时值不同);但满足结合律。
(AB)ᵀ = BᵀAᵀ,顺序要反。
1D 数组没有转置:(3,) 的 .T 仍是 (3,);要列向量得 reshape(-1, 1)。
aᵢⱼ 先行后列;wᵢⱼ 的 i 是本层神经元、j 是输入,j=0 是偏置。
W 的形状 m×(n+1),参数 = m(n+1) = mn 权重 + m 偏置;Keras 存的是转置 (n, m) + (m,)。