🤖 AI Math Unit 3 — Gradient Descent OptimizationApplied STEM · Loss functions: Mean Squared Error (MSE) · Partial derivatives & the gradient vector ∇L · Learning rate η and convergence · Gradient descent update rule w ← w - η ∇L · Convex vs non-convex loss landscapes
Machine learning models learn by minimizing a loss function via gradient descent. Calculus provides the directional derivative pointing toward the steepest descent in parameter space.
▲ Theorem 3.1 — Gradient Descent Update Equation
Let L(w)L(w) be a differentiable loss function over weight parameters ww. The gradient \( \nabla L(\mathbf{w}) = \left[ \frac{\partial L}{\partial w_1}, \dots, \frac{\partial L}{\partial w_d} \right]^T \) points in the direction of greatest rate of increase. To minimize loss, parameter updates step in the opposite direction scaled by learning rate \( \eta > 0 \): \[ \mathbf{w}_{t+1} = \mathbf{w}_t - \eta \nabla L(\mathbf{w}_t) \]
التقريب الخطي المحلي وهندسة أشد انحدار: وفق مفكوك تايلور من الدرجة الأولى، \(L(\mathbf{w} + \Delta\mathbf{w}) \approx L(\mathbf{w}) + \nabla L(\mathbf{w})^T \Delta\mathbf{w}\). وبتطبيق متباينة كوشي-شفارتز على جميع الاتجاهات الوحدوية \(\|\mathbf{u}\|_2 = 1\)، تبلغ المشتقة الاتجاهية أدنى قيمة سالبة لها عندما نتحرك بعكس اتجاه التدرج \(-\nabla L(\mathbf{w})\)، مما يضمن تناقص دالة الخسارة محلياً بشرط اختيار معدل تعلم مناسب \(0 < \eta < \frac{2}{L_{\text{lip}}}\).
خطوات الانحدار التدريجي \(w_{t+1} = w_t - \eta L'(w_t)\) بمعدل تعلم \(\eta = 0.3\) نزولاً على دالة الخسارة المحدبة \(L(w) = (w - 2)^2 + 1\) نحو القيمة المثلى \(w^* = 2\).
Updates weights on mini-batches of size \( B \ll N \).
◆ لماذا تعمل القاعدة — شرط التقارب الدقيق على دالة الخسارة التربيعية
تحليل التقارب التربيعي: نعتبر دالة الخسارة \(L(w) = \frac{1}{2}\lambda (w - w^*)^2\) ذات التقعر \(\lambda > 0\). تعطي خطوة التدرج \(w_{t+1} - w^* = (1 - \eta\lambda)(w_t - w^*)\)، وبعد \(t\) تكراراً نحصل على \(|w_t - w^*| = |1 - \eta\lambda|^t |w_0 - w^*|\). يتقارب المتسلسل إلى الصفر إذا وفقط إذا كان \(|1 - \eta\lambda| < 1 \iff 0 < \eta < \frac{2}{\lambda}\)، أما إذا تجاوز \(\eta\) العتبة \(\frac{2}{\lambda}\) فتتباعد التحديثات بالتناوب.
Worked Example 3.A — Single Step of Gradient Descent
المسألة. Consider a simple linear model \( \hat{y} = wx \) with squared loss for a single data point \( (x, y) = (2, 8) \): \[ L(w) = (y - wx)^2 = (8 - 2w)^2 \] If the current weight is \( w_0 = 1.0 \) and the learning rate is \( \eta = 0.05 \), compute the gradient \( \frac{dL}{dw} \) and the updated weight \( w_1 \).
▸ عرض خطوات الحل بالتفصيل
Apply the chain rule to differentiate \( L(w) \): \[ \frac{dL}{dw} = 2(8 - 2w) \cdot (-2) = -4(8 - 2w) = -32 + 8w \] Evaluate at \( w_0 = 1.0 \): \[ \left.\frac{dL}{dw}\right|_{w=1.0} = -32 + 8(1) = -24 \] Apply gradient descent update: \[ w_1 = w_0 - \eta \left(\frac{dL}{dw}\right) = 1.0 - (0.05)(-24) = 1.0 + 1.2 = 2.2 \]. Notice that with \( w_0 = 1.0 \), prediction \( \hat{y} = 2 \) was too low (target was 8). The gradient step increased \( w \) to 2.2, giving \( \hat{y} = 4.4 \), moving closer to 8. Answer: Gradient = \( -24 \); updated weight \( w_1 = 2.2 \).
Worked Example \(B\) — Loss Functions, Gradients & Optimization (Medium Application)
المسألة. Consider a univariate regression model where the predicted output \( \hat{y} \) is given by \( \hat{y} = ax + b \). The loss function is the Mean Squared Error (MSE), defined as \( L = \frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2 \). For a single data point \( (x_1, y_1) = (2, 5) \), the loss is \( L = (5 - (2a + b))^2 \). The gradient of the loss function with respect to \( a \) is \( \frac{\partial L}{\partial a} = -2(5 - (2a + b)) \cdot 2 \). If during gradient descent, with a learning rate of \( \eta = 0.1 \), the parameters \( a \) and \( b \) are updated from \( a_{old} = 1 \) and \( b_{old} = 1 \), to \( a_{new} = 0.8 \) and \( b_{new} = 0.8 \), what is the value of \( b \) that would have resulted in \( \frac{\partial L}{\partial b} = 0 \) for this single data point, assuming \( a=1 \) and \( b=1 \) were the values *before* the gradient calculation that led to the update to \( a_{new}=0.8, b_{new}=0.8 \)?
▸ عرض خطوات الحل بالتفصيل
The loss function for a single data point is \( L = (y_1 - (ax_1 + b))^2 \). Given \( (x_1, y_1) = (2, 5) \), so \( L = (5 - (2a + b))^2 \). The gradient of the loss with respect to \( b \) is \( \frac{\partial L}{\partial b} = 2(5 - (2a + b)) \cdot (-1) = -2(5 - 2a - b) \). We are asked to find the value of \( b \) for which \( \frac{\partial L}{\partial b} = 0 \), assuming \( a=1 \) and \( b=1 \) were the parameters *before* the update step that led to \( a_{new}=0.8, b_{new}=0.8 \). This means we evaluate the gradient at \( a=1 \) and the *unknown* value of \( b \). Setting \( \frac{\partial L}{\partial b} = 0 \): \( -2(5 - 2(1) - b) = 0
\)\( 5 - 2 - b = 0
\)\( 3 - b = 0
\)\( b = 3 \). The provided gradient update information \( \frac{\partial L}{\partial a} = -2(5 - (2a + b)) \cdot 2 \) and the learning rate \( \eta = 0.1 \) are distractors. The question specifically asks for the value of \( b \) that makes \( \frac{\partial L}{\partial b} = 0 \) *at the point where \( a=1 \)*. The update rule to \( a_{new}=0.8, b_{new}=0.8 \) implies that the gradient of \( a \) was \( \frac{a_{old} - a_{new}}{\eta} = \frac{1 - 0.8}{0.1} = 2 \), and the gradient of \( b \) was \( \frac{b_{old} - b_{new}}{\eta} = \frac{1 - 0.8}{0.1} = 2 \). Let's verify: If \( a=1 \) and \( b=3 \), then \( \frac{\partial L}{\partial b} = -2(5 - 2(1) - 3) = -2(5 - 2 - 3) = -2(0) = 0 \). If \( a=1 \) and \( b=3 \), \( \frac{\partial L}{\partial a} = -2(5 - 2(1) - 3) \cdot 2 = -2(0) \cdot 2 = 0 \). This scenario implies that the point \( a=1, b=3 \) is a minimum for the loss function, but the question is about finding the value of \( b \) that makes the gradient of \( b \) zero at \( a=1 \), which is \( b=3 \). The provided update \( a_{new}=0.8, b_{new}=0.8 \) from \( a_{old}=1, b_{old}=1 \) is a different step in the optimization process and is not directly used to find the value of \( b \) that makes the gradient of \( b \) zero at \( a=1 \).
Worked Example \(C\) — Loss Functions, Gradients & Optimization (Hard Application)
المسألة. Consider the following quadratic loss function for a regression problem: \( L(y, \hat{y}) = (y - \hat{y})^2 \). If the predicted value \( \hat{y} = 5 \) and the true value \( y = 3 \), what is the value of the loss function?
▸ عرض خطوات الحل بالتفصيل
The quadratic loss function is defined as \( L(y, \hat{y}) = (y - \hat{y})^2 \). Given the true value \( y = 3 \) and the predicted value \( \hat{y} = 5 \), we substitute these values into the loss function:
\( L(3, 5) = (3 - 5)^2 \)
\( L(3, 5) = (-2)^2 \)
\( L(3, 5) = 4 \)
Therefore, the value of the loss function is 4.
Worked Example \(D\) — Loss Functions, Gradients & Optimization (Easy Application)
المسألة. Consider the quadratic loss function \( L(w) = w^2 - 4w + 3 \). Find the value of \( w \) where the function reaches its minimum.
▸ عرض خطوات الحل بالتفصيل
The given loss function is a quadratic function \( L(w) = w^2 - 4w + 3 \). This represents a parabola opening upwards. The minimum value of a quadratic function of the form \( aw^2 + bw + c \) occurs at \( w = -\frac{b}{2a} \). In this case, \( a = 1 \) and \( b = -4 \). Therefore, the minimum occurs at \( w = -\frac{-4}{2(1)} = \frac{4}{2} = 2 \). Alternatively, we can find the minimum by taking the derivative and setting it to zero: \( L'(w) = 2w - 4 \). Setting \( L'(w) = 0 \) gives \( 2w - 4 = 0 \), which implies \( 2w = 4 \), so \( w = 2 \).
⚠️ أخطاء شائعة في الاختبار — الانحدار التدريجي والتحسين
1. جمع التدرج بدلاً من طرحه: التحرك في اتجاه \(+\nabla L\) يؤدي إلى تعظيم الخسارة لا تقليلها؛ الصيغة الصحيحة للتصغير هي دائماً \(\mathbf{w}_{t+1} = \mathbf{w}_t - \eta \nabla L(\mathbf{w}_t)\). 2. الخلط بين النقاط الحرجة والنهاية الصغرى الشاملة: الشرط \(\nabla L(\mathbf{w}) = \mathbf{0}\) يتحقق أيضاً عند نقاط السرج، لذا يجب التحقق من إيجابية مصفوفة هيسيان \(\nabla^2 L(\mathbf{w}) \succ 0\).
Claiming \(+25\%\) followed by \(-25\%\) returns to \(100\%\)
The \(25\%\) reduction applies to the larger base \(1.25 Q_0\), not \(Q_0\).
Multiply scale factors: \(1.25 \times 0.75 = 0.9375\) (a \(6.25\%\) net decrease).
Solving \(-3x > 12\) as \(x > -4\)
يؤدي إلى فقدان جذور صحيحة أو إدخال جذور دخيلة لا تحقق المعادلة الأصلية.
Flip the inequality symbol whenever multiplying or dividing by a negative: \(x < -4\).
Computing round-trip average speed as \(\frac{v_1 + v_2}{2}\)
يؤدي إلى فقدان جذور صحيحة أو إدخال جذور دخيلة لا تحقق المعادلة الأصلية.
Use total distance over total time: \(v_{\text{avg}} = \frac{2v_1 v_2}{v_1 + v_2}\).
▲ استراتيجية الحل والتحقق السريع — دوال الخسارة والانحدار التدريجي
التحقق الفوري من صحة التحديث: بعد حساب \(\mathbf{w}_1 = \mathbf{w}_0 - \eta \nabla L(\mathbf{w}_0)\) لمعدل تعلم صغير، تحقق دائماً من أن \(L(\mathbf{w}_1) < L(\mathbf{w}_0)\) وأن الوزن الجديد اقترب فعلياً من نقطة السكون \(\nabla L(\mathbf{w}^*) = \mathbf{0}\).
[Practice 1 · Medium] Consider the following quadratic loss function: \( L(w) = 3w^2 - 12w + 10 \). What is the value of \( w \) that minimizes this loss function?
💡 Hint 1 of 3
To find the minimum of the quadratic loss function \( L(w) = 3w^2 - 12w + 10 \), we can find the derivative with respect to \( w \) and set it to zero.
💡 Hint 2 of 3
The derivative of \( L(w) \) with respect to \( w \) is: \( \frac{dL}{dw} = 6w - 12 \). Setting the derivative to zero to find the critical point: \( 6w - 12 = 0 \) \( 6w = 12 \) \( w = \frac{12}{6} \) \( w = 2 \).
💡 Hint 3 of 3
The second derivative is \(\frac{d^2L}{dw^2} = 6\), which is positive, confirming that \(w\) corresponds to a minimum.
To find the minimum of the quadratic loss function \( L(w) = 3w^2 - 12w + 10 \), we can find the derivative with respect to \( w \) and set it to zero. The derivative of \( L(w) \) with respect to \( w \) is: \( \frac{dL}{dw} = 6w - 12 \). Setting the derivative to zero to find the critical point: \( 6w - 12 = 0 \) \( 6w = 12 \) \( w = \frac{12}{6} \) \( w = 2 \). The second derivative is \( \frac{d^2L}{dw^2} = 6 \), which is positive, confirming that \( w=2 \) corresponds to a minimum.
[Practice 2 · Hard] Evaluate the output of the standard ReLU activation function \( f(z) = \max(0, z) \) for inputs \( z_1 = -4 \) and \( z_2 = 6 \).
💡 Hint 1 of 3
By definition of the Rectified Linear Unit: \( \text{ReLU}(z) = z \) if \( z > 0 \).
💡 Hint 2 of 3
And \( 0 \) if \( z \le 0 \).
💡 Hint 3 of 3
Thus \(f(-4) = 0\) and \(f(6)\).
By definition of the Rectified Linear Unit: \( \text{ReLU}(z) = z \) if \( z > 0 \), and \( 0 \) if \( z \le 0 \). Thus \( f(-4) = 0 \) and \( f(6) = 6 \).
[Practice 3 · Easy] In gradient descent, a weight currently has value \( w = 60 \). If the gradient of the loss is \( \frac{\partial L}{\partial w} = 24 \) and the learning rate is \( \eta = 0.6 \), find the updated weight \( w' \).
💡 Hint 1 of 3
Using the gradient descent update rule: \(w' = w - \eta \frac{\partial L}{\partial w}\).
💡 Hint 2 of 3
Substitute and simplify: \(w' = 60 - (0.6)(24)\).
💡 Hint 3 of 3
Compute the final value from: \(60 - 14.40\).
Using the gradient descent update rule: \( w' = w - \eta \frac{\partial L}{\partial w} = 60 - (0.6)(24) = 60 - 14.40 = 45.60 \).
[Practice 4 · Medium] Consider the scalar loss function \( L(w) = 5w^2 - 8w + 5 \). Find the analytical gradient \( \nabla L(w) \) evaluated at \( w = 8 \).
💡 Hint 1 of 3
Differentiate with respect to \( w \): \( \frac{dL}{dw} = 2(5)w - 8 = 10w - 8 \).
💡 Hint 2 of 3
At \( w = 8 \): \(\nabla L = 10(8) - 8\). Substitute and simplify: \(\nabla L = 80 - 8\).
💡 Hint 3 of 3
Compute the final value from: \(80 - 8\).
Differentiate with respect to \( w \): \( \frac{dL}{dw} = 2(5)w - 8 = 10w - 8 \). At \( w = 8 \): \(
\nabla L = 10(8) - 8 = 80 - 8 = 72 \).
[Practice 5 · Hard] Two document embeddings \( \mathbf{a} = [6, 12]^T \) and \( \mathbf{b} = [12, 6]^T \) are compared. If \( \|\mathbf{a}\| = \|\mathbf{b}\| = \sqrt{180} \), calculate their exact cosine similarity.