المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط
🤖 AI Math Unit 2 — Neural Matrix OperationsApplied STEM · Matrix multiplication as linear transformation · Dense layer forward pass y = Wx + b · Dimensions, shapes & batching · Non-linear activations: ReLU, Sigmoid, Softmax · Universal approximation theorem intuition
Deep neural networks are sequences of matrix multiplications alternated with non-linear activation functions. Here we formulate the algebraic forward pass of dense feedforward neural layers.
● Definition 2.1 — Dense Neural Layer Formulation
A fully connected (dense) neural network layer with n_{in}nin inputs and n_{out}nout neurons computes an affine linear transformation followed by an element-wise activation \( \sigma \): \[ \mathbf{z} = \mathbf{W}\mathbf{x} + \mathbf{b}, \quad \mathbf{a} = \sigma(\mathbf{z}) \] where \( \mathbf{W} \in \mathbb{R}^{n_{\text{out}} \times n_{\text{in}}} \) is the weight matrix, \( \mathbf{x} \in \mathbb{R}^{n_{\text{in}}} \) is the input vector, \( \mathbf{b} \in \mathbb{R}^{n_{\text{out}}} \) is the bias vector, and \( \mathbf{a} \in \mathbb{R}^{n_{\text{out}}} \) is the layer activation vector.
Linear Structure, Inner Products & Geometric Transformations: In \(\mathbb{R}^n\), the dot product \(\mathbf{u}\cdot\mathbf{v} = \|\mathbf{u}\|\|\mathbf{v}\|\cos\theta = \sum_{i=1}^n u_i v_i\) encodes orthogonality (\(\mathbf{u}\perp\mathbf{v} \iff \mathbf{u}\cdot\mathbf{v} = 0\)) and orthogonal projection \(\operatorname{proj}_{\mathbf{v}}\mathbf{u} = \dfrac{\mathbf{u}\cdot\mathbf{v}}{\|\mathbf{v}\|^2}\mathbf{v}\). For a matrix transformation \(A \in \mathbb{R}^{n\times n}\), \(|\det(A)|\) measures the local volume scaling factor, and \(A\) is invertible if and only if \(\det(A) \ne 0\) (equivalently, all eigenvalues \(\lambda_i \ne 0\)).
الأثر الهندسي لمصفوفة الأوزان \(W = \begin{pmatrix} 2 & 1 \\ 1 & 2.5 \end{pmatrix}\) في تمديد وقص إحداثيات الميزات قبل إضافة الانحياز ودالة التنشيط.
See كيف hidden layer combining piecewise-خطية ReLU activations \(\max(0, w_i \cdot x + b_i)\) ينشئ hinged bends capable من approximating غير خطي دوال.
مقارنة دالة \(\operatorname{ReLU}(z)\) ذات الميل الثابت لـ \(z > 0\) مع دالتي التشبع \(\sigma(z) \in (0, 1)\) و\(\tanh(z) \in (-1, 1)\) المتمركزة حول الصفر.
ReLU (المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط)
\( \text{ReLU}(z) = \max(0, z) \)
Sigmoid (المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط)
\( \sigma(z) = \frac{1}{1 + e^{-z}} \in (0, 1) \)
Softmax (المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط)
For eigenvalues, \(A\mathbf{v} = \lambda\mathbf{v}\) with \(\mathbf{v} \ne \mathbf{0}\) is equivalent to \((A - \lambda I)\mathbf{v} = \mathbf{0}\). A homogeneous linear system has a non-trivial nullspace vector \(\mathbf{v} \ne \mathbf{0}\) if and only if the matrix \(A - \lambda I\) is singular, yielding the characteristic polynomial \(\det(A - \lambda I) = 0\).
Worked Example 2.A — Manual Feedforward Neural Layer
المسألة. A neural network layer has 2 inputs and 3 neurons with weights and biases: \[ \mathbf{W} = \begin{pmatrix} 2 & -1 \\ 0.5 & 3 \\ -1 & 1 \end{pmatrix}, \quad \mathbf{b} = \begin{pmatrix} 1 \\ -2 \\ 0.5 \end{pmatrix} \] For the input \( \mathbf{x} = \begin{pmatrix} 3 \\ 2 \end{pmatrix} \), compute the pre-activation \( \mathbf{z} \) and the activated output \( \mathbf{a} \) using the ReLU function.
Worked Example \(B\) — Matrices, Linear Layers & Activation Functions (Medium Application)
المسألة. In a multiple linear regression model, the Sum of Squared Errors is \( \text{SSE} = 100 \) and the Total Sum of Squares is \( \text{SST} = 500 \). What is the coefficient of determination \( R^2 \)?
▸ عرض خطوات الحل بالتفصيل
The coefficient of determination measures the proportion of variance explained: \( R^2 = 1 - \frac{\text{SSE}}{\text{SST}} = 1 - \frac{100}{500} = 1 - 0.20 = 0.80 \).
Worked Example \(C\) — Matrices, Linear Layers & Activation Functions (Hard Application)
المسألة. A centered feature matrix \( \mathbf{X} \) has shape \( N \times 38 \). What is the size and symmetry property of the sample covariance matrix \( \mathbf{\Sigma} = \frac{1}{N-1}\mathbf{X}^T\mathbf{X} \)?
▸ عرض خطوات الحل بالتفصيل
Since \( \mathbf{X} \) has \( 38 \) columns, \( \mathbf{X}^T \) is \( 38 \times N \) and \( \mathbf{X}^T\mathbf{X} \) is \( 38 \times 38 \). Furthermore, \( (\mathbf{X}^T\mathbf{X})^T = \mathbf{X}^T(\mathbf{X}^T)^T = \mathbf{X}^T\mathbf{X} \), proving it is always symmetric.
Worked Example \(D\) — Matrices, Linear Layers & Activation Functions (Easy Application)
المسألة. Consider a linear layer with a weight matrix \( W \) and an input vector \( \mathbf{x} \). If \( W = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \) and \( \mathbf{x} = \begin{pmatrix} 5 \\ 6 \end{pmatrix} \), compute the output vector \( \mathbf{y} = W\mathbf{x} \).
▸ عرض خطوات الحل بالتفصيل
The output vector \( \mathbf{y} \) is computed by multiplying the weight matrix \( W \) by the input vector \( \mathbf{x} \). This is a standard matrix-vector multiplication. \\ \\ \( \mathbf{y} = W\mathbf{x} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \begin{pmatrix} 5 \\ 6 \end{pmatrix} \)\\\ \\ To compute the first element of \( \mathbf{y} \), we take the dot product of the first row of \( W \) with \( \mathbf{x} \): \( (1)(5) + (2)(6) = 5 + 12 = 17 \).\\ \\ To compute the second element of \( \mathbf{y} \), we take the dot product of the second row of \( W \) with \( \mathbf{x} \): \( (3)(5) + (4)(6) = 15 + 24 = 39 \).\\ \\ Therefore, the output vector is \( \mathbf{y} = \begin{pmatrix} 17 \\ 39 \end{pmatrix} \).
⚠️ أخطاء شائعة في الاختبار — المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط
1. Non-Commutativity of Matrix Multiplication & Cross Products: On Mathematics for Artificial Intelligence & Machine Learning, assuming \(AB = BA\) (so expanding \((A+B)^2\) as \(A^2 + 2AB + B^2\) instead of \(A^2 + AB + BA + B^2\)) or forgetting \(\mathbf{u}\times\mathbf{v} = -(\mathbf{v}\times\mathbf{u})\) invalidates matrix proofs.
2. Using Position Vectors Instead of Direction Vectors for Angles: To find the angle at vertex \(B\) of triangle \(ABC\), you must use tail-to-tail vectors \(\overrightarrow{BA}\) and \(\overrightarrow{BC}\), never \(\overrightarrow{AB}\cdot\overrightarrow{BC}\) (which finds the exterior supplementary angle \(180^\circ - B\)).
تشخيص الأخطاء — مقارنة الخطأ الشائع بالطريقة الصحيحة (المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط)
الخطأ الشائع للطالب
السبب الرياضي للخطأ
الطريقة الصحيحة المعتمدة
Computing angle at \(B\) using \(\overrightarrow{AB}\cdot\overrightarrow{BC}\)
Vectors are head-to-tail instead of tail-to-tail, giving \(180^\circ - \theta\).
Reverse \(\overrightarrow{AB}\) so both emanate from \(B\): use \(\overrightarrow{BA}\cdot\overrightarrow{BC}\).
Using the same parameter \(t\) for two intersecting 3D lines \(\mathbf{r}_1(t) = \mathbf{r}_2(t)\)
Tests for a simultaneous collision at time \(t\) rather than geometric intersection of paths.
Assign distinct parameters \(\lambda\) and \(\mu\) to the two lines before equating components.
Assuming \((AB)^{-1} = A^{-1}B^{-1}\)
Multiplying \((AB)(A^{-1}B^{-1})\) does not cancel inner factors when \(AB \ne BA\).
Use the shoe-sock reversal law: \((AB)^{-1} = B^{-1}A^{-1}\).
▲ استراتيجية الحل السريع والتحقق — المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط
Fast Matrix & Vector Verification (Mathematics for Artificial Intelligence & Machine Learning): Check every \(2\times 2\) or \(3\times 3\) eigenvalue computation using the trace and determinant invariants: \(\sum \lambda_i = \operatorname{tr}(A)\) (sum of diagonal entries) and \(\prod \lambda_i = \det(A)\).
[Practice 1 · Medium] A 3D dataset has covariance matrix eigenvalues \( \lambda_1 = 62 \), \( \lambda_2 = 24 \), and \( \lambda_3 = 14 \). What percentage of total variance is explained by the first principal component \( PC_1 \)?
💡 Hint 1 of 3
Total variance is \(\sum \lambda_i = 62 + 24 + 14\).
💡 Hint 2 of 3
The proportion explained by \( PC_1 \) is: \(\frac{\lambda_1}{\sum \lambda_i}\). Rewrite and substitute: \(\frac{\lambda_1}{\sum \lambda_i} = \frac{62}{100}\).
💡 Hint 3 of 3
Evaluate \(\frac{62}{100}\) to obtain the final answer.
Total variance is \( \sum \lambda_i = 62 + 24 + 14 = 100 \). The proportion explained by \( PC_1 \) is \( \frac{\lambda_1}{\sum \lambda_i} = \frac{62}{100} = 62.0\% \).
[Practice 2 · Hard] Consider a dataset with two features, \( X_1 \) and \( X_2 \). The covariance matrix for this dataset is given by:
\[
\Sigma = \begin{pmatrix}
10 & 5 \\
5 & 20
\end{pmatrix}
\]
What is the variance of the first feature, \( X_1 \)?
💡 Hint 1 of 3
The covariance matrix \( \Sigma \) for a dataset with two features \( X_1 \) and \( X_2 \) is structured as follows: \(\Sigma = \begin{pmatrix} Cov(X_1, X_1) & Cov(X_1, X_2) \\ Cov(X_2, X_1) & Cov(X_2, X_2) \end{pmatrix}\).
💡 Hint 2 of 3
By definition, the variance of a random variable is the covariance of the variable with itself. Therefore, \( Var(X_1) = Cov(X_1, X_1) \) and \( Var(X_2) = Cov(X_2, X_2) \). In the given covariance matrix: \(\Sigma = \begin{pmatrix} 10 & 5 \\ 5 & 20 \end{pmatrix}\).
💡 Hint 3 of 3
Thus, the variance of \(X_1\) is 10. The covariance between \(X_1\) and \(X_2\) is 5, and the variance of \(X_2\) is 20.
The covariance matrix \( \Sigma \) for a dataset with two features \( X_1 \) and \( X_2 \) is structured as follows:
\[
\Sigma = \begin{pmatrix}
Cov(X_1, X_1) & Cov(X_1, X_2) \\
Cov(X_2, X_1) & Cov(X_2, X_2)
\end{pmatrix}
\]
By definition, the variance of a random variable is the covariance of the variable with itself. Therefore, \( Var(X_1) = Cov(X_1, X_1) \) and \( Var(X_2) = Cov(X_2, X_2) \).
In the given covariance matrix:
\[
\Sigma = \begin{pmatrix}
10 & 5 \\
5 & 20
\end{pmatrix}
\]
The element in the first row and first column represents \( Cov(X_1, X_1) \), which is the variance of \( X_1 \).
Thus, the variance of \( X_1 \) is 10. The covariance between \( X_1 \) and \( X_2 \) is 5, and the variance of \( X_2 \) is 20.
[Practice 3 · Easy] Principal Component Analysis (PCA) projects a dataset from \( \mathbb{R}^{50} \) onto its top \( 6 \) principal components. What is the dimension of each transformed data point?
💡 Hint 1 of 3
PCA projection onto \( k \) eigenvectors transforms each sample vector from \( \mathbb{R}^D \) to \( \mathbb{R}^k \).
💡 Hint 2 of 3
Here \( k = 6 \).
💡 Hint 3 of 3
So the reduced dimension is \(6\).
PCA projection onto \( k \) eigenvectors transforms each sample vector from \( \mathbb{R}^D \) to \( \mathbb{R}^k \). Here \( k = 6 \), so the reduced dimension is \( 6 \).
[Practice 4 · Medium] A vector \( \mathbf{v} \) is transformed into a new vector \( \mathbf{v}' \) by a linear transformation represented by the matrix \( \mathbf{M} \). We are given that \( \mathbf{v} = \begin{pmatrix} 2 \\ 3 \end{pmatrix} \) and \( \mathbf{v}' = \begin{pmatrix} 7 \\ 11 \end{pmatrix} \). The transformation matrix is of the form \( \mathbf{M} = \begin{pmatrix} a & 1 \\ 4 & b \end{pmatrix} \). Determine the value of \( a \) and \( b \).
💡 Hint 1 of 3
The transformation is defined by \( \mathbf{v}' = \mathbf{M}\mathbf{v} \). Substituting the given vectors and matrix form: \( \begin{pmatrix} 7 \\ 11 \end{pmatrix} = \begin{pmatrix} a & 1 \\ 4 & b \end{pmatrix} \begin{pmatrix} 2 \\ 3 \end{pmatrix} \).
💡 Hint 2 of 3
Performing the matrix multiplication: \( \begin{pmatrix} 7 \\ 11 \end{pmatrix} = \begin{pmatrix} 2a + 3 \\ 8 + 3b \end{pmatrix} \) Equating the corresponding components, we get a system of two linear equations: 1) \( 2a + 3 = 7 \).
💡 Hint 3 of 3
Solving the second equation for \(b\): \(3b = 11 - 8\).
The transformation is defined by \( \mathbf{v}' = \mathbf{M}\mathbf{v} \). Substituting the given vectors and matrix form:
\( \begin{pmatrix} 7 \\ 11 \end{pmatrix} = \begin{pmatrix} a & 1 \\ 4 & b \end{pmatrix} \begin{pmatrix} 2 \\ 3 \end{pmatrix} \)
Performing the matrix multiplication:
\( \begin{pmatrix} 7 \\ 11 \end{pmatrix} = \begin{pmatrix} 2a + 3 \\ 8 + 3b \end{pmatrix} \)
Equating the corresponding components, we get a system of two linear equations:
1) \( 2a + 3 = 7 \)
2) \( 8 + 3b = 11 \)
Solving the first equation for \( a \):
\( 2a = 7 - 3 \)
\( 2a = 4 \)
\( a = 2 \)
Solving the second equation for \( b \):
\( 3b = 11 - 8 \)
\( 3b = 3 \)
\( b = 1 \)
Thus, \( a = 2 \) and \( b = 1 \).
[Practice 5 · Hard] A medical test for a rare disease has a 99\% accuracy rate in detecting the disease when it is present (True Positive Rate) and a 95\% accuracy rate in identifying individuals without the disease (True Negative Rate). If the prevalence of the disease in the population is 0.5\%, what is the probability that a person who tests positive actually has the disease?
💡 Hint 1 of 3
Let D be the event that a person has the disease, and P be the event that the person tests positive.
💡 Hint 2 of 3
We are given: P(P|D) = 0.99 (True Positive Rate) P(not P|not D) = 0.95 (True Negative Rate), which means P(P|not D) = 1 - 0.95 = 0.05 (False Positive Rate).
💡 Hint 3 of 3
Converting to a percentage, this is approximately 9.05\%, which rounds to 9.1\%.
Let D be the event that a person has the disease, and P be the event that the person tests positive.
We are given:
P(P|D) = 0.99 (True Positive Rate)
P(not P|not D) = 0.95 (True Negative Rate), which means P(P|not D) = 1 - 0.95 = 0.05 (False Positive Rate)
P(D) = 0.005 (Prevalence of the disease)
From P(D), we know P(not D) = 1 - P(D) = 1 - 0.005 = 0.995.
We want to find P(D|P), the probability that a person has the disease given a positive test result.
Using Bayes' Theorem:
P(D|P) = [P(P|D) * P(D)] / P(P)
To find P(P), we use the law of total probability:
P(P) = P(P|D) * P(D) + P(P|not D) * P(not D)
P(P) = (0.99 * 0.005) + (0.05 * 0.995)
P(P) = 0.00495 + 0.04975
P(P) = 0.0547
Now, substitute this back into Bayes' Theorem:
P(D|P) = (0.99 * 0.005) / 0.0547
P(D|P) = 0.00495 / 0.0547
P(D|P) \approx 0.09049
Converting to a percentage, this is approximately 9.05\%, which rounds to 9.1\%.
المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط — مسألة مهيكلة من ٦ درجات
6 نقاط
الآلة الحاسبة مسموحة
في موضوع المصفوفات والطبقات الخطية في الشبكات العصبية ودوال التنشيط، ادرس الدالة \(f(x) = 3x^2 - 5x + 1\) على الفترة \([0, 3]\).
(a)2 نقطة
احسب قيمتي \(f(0)\) و \(f(2)\).
معيار التصحيح والإجابة النموذجية
[M1]يعوّض القيمتين في الدالة(1 نقطة)
[A1]يحصل على 1 و 3(1 نقطة)
(b)2 نقطة
أوجد المشتقة \(f'(x)\) والنقطة الحرجة \(x_c\).
معيار التصحيح والإجابة النموذجية
[M1]يشتق الدالة التربيعية(1 نقطة)
[A1]يحل لإيجاد النقطة الحرجة(1 نقطة)
(c)2 نقطة
احسب متوسط معدل التغيّر من \(x = 0\) إلى \(x = 2\).
معيار التصحيح والإجابة النموذجية
[M1]يطبّق قانون متوسط معدل التغيّر(1 نقطة)
[A1]يبسّط الناتج إلى 1(1 نقطة)
الحل الكامل التفصيلي
(أ) \(f(0)=1\) و \(f(2)=3\). (ب) \(f'(x)=6x-5\). (ج) متوسط معدل التغيّر يساوي \(1\).