11 - Principal Component Analysis and Autoencoders

Class: CSCE-421


Notes:

We will use linear algebra only!

Linear Algebra - Matrix multiplication

Matrix times Vector - 2 Ways

image-43.png

At first, you learn (Mv1). But when you get used to viewing it as (Mv2), you can understand Ax as a linear combination of the columns of A. Those products fill the column space of A denoted as C(A). The solution space of Ax=0 is the nullspace of A denoted as N(A).

Notes:

Vector times matrix - 2 Ways

image-44.png432

A row vector y is multiplied by the two column vectors of A and become the two dot product elements of yA.

image-45.png428x154

The product yA is a linear combination of the row vectors of A.

Notes:

Matrix times Matrix - 4 ways

image-46.png

Notes:

Practical Patterns

image-47.png

Burn this into your memories and you can see ...

Notes:

image-48.png606

Notes:

Orthogonal Matrices

(1) An orthogonal matrix is a square matrix whose columns and rows are orthogonal unit vectors, i.e., orthonormal vectors. That is, if a matrix Q is an orthogonal matrix, we have

QTQ=QQT=I.

(2) It leads to Q−1=QT, which is a very useful property as it provides an easy way to compute the inverse.

(3) For an orthogonal n×n matrix Q=[q1,q2,…,qn], where qi∈Rn, i=1,2,…,n, it is easy to see that qiTqj=0 when i≠j and qiTqi=1.

(4) Furthermore, suppose Q1=[q1,q2,…,qi] and Q2=[qi+1,qi+2,…,qn], we have Q1TQ1=I,Q2TQ2=I, but Q1Q1T≠I,Q2Q2T≠I.

Notes:

Notes:

Eigen-Decomposition

Eigen-Decomposition (1)

(1) A square n×n matrix S with n linearly independent eigenvectors can be factorized as

S=QΛQ−1

where Q is the square n×n matrix whose columns are eigenvectors of S, and Λ is the diagonal matrix whose diagonal elements are the corresponding eigenvalues.

(2) Note that only diagonalizable matrices can be factorized in this way.

(3) If S is a symmetric matrix, its eigenvectors are orthogonal. Thus Q is an orthogonal matrix and we have

S=QΛQT.

Notes:

S[q1,q2,⋯,qn]=[λ1q1,λ2q2,⋯,λnqn]

- we can represent this a Sqi
- We then can say:

SQ=QΛ

- We are just equaling each column to each other on Svi=λivi

SQQ−1=QΛQ−1=S

- This happens since QQ−1=I?
- If all columns are orthogonal to each other, they are not linear independent
- (orthogonal means they have 90 degree angle), this is a stronger condition

Question:

Eigen-Decomposition (2)

image-49.png

Notes:

Checkpoint 1 (eigen-decomposition)

To understand Principal Component Analysis (PCA), we first need to understand the mathematical engine that makes it work: Eigen-Decomposition.

Imagine a matrix as a machine that stretches, squashes, and rotates data vectors. Eigen-decomposition is a way of "taking the machine apart" to reveal its fundamental, underlying structure. It tells us the core directions in which the machine stretches the data, and by exactly how much.

1. The Basic Formula (S=QΛQ−1) If you have a square n×n matrix S, you can mathematically factorize (decompose) it into the product of three simpler matrices:

Note: This specific decomposition is only possible for "diagonalizable" square matrices with linearly independent eigenvectors.

2. The Symmetric Magic (S=QΛQT) In machine learning (and especially PCA), we almost always deal with symmetric matrices (like the covariance matrix, where the top-right mirrors the bottom-left). When your matrix S is symmetric, two beautiful mathematical properties occur:

  1. Orthogonal Eigenvectors: The eigenvectors (the fundamental axes) become perfectly perpendicular (orthogonal) to each other.
  2. The Inverse Becomes the Transpose: Because the columns of Q are orthogonal unit vectors, Q becomes an "orthogonal matrix". A magical property of orthogonal matrices is that calculating their inverse is as easy as flipping them on their side: Q−1=QT.

Because of this, the decomposition formula for symmetric matrices simplifies beautifully to: S=QΛQT.

3. Positive Semi-Definite (PSD) Matrices Your professor introduces the concept of a Positive Semi-Definite matrix. By definition, a symmetric matrix S is PSD if for any vector x, the result of xTSx≥0.

4. Eigen-Decomposition (2): The Projection View Instead of looking at S=QΛQT as three giant matrices multiplying each other, we can use linear algebra (Specifically "Practical Pattern 4" from your previous slides) to rewrite it as a sum of smaller pieces: $$S = \sum_{i=1}^n \lambda_i q_i q_i^T$$

Singular Value Decomposition (SVD)

Singular Value Decomposition (1)

image-50.png

Notes:

Singular Value Decomposition (2)

The singular value decomposition (SVD) of an m×n real matrix (without loss of generality, we assume m≥n ) can be written as

R=UΣ~VT,

where U is an orthogonal m×m matrix, V is an orthogonal n×n matrix, and Σ~ is a diagonal m×n matrix with non-negative real values on diagonal. That is,

UTU=UUT=Im×m,VTV=VVT=In×n,Σ~=[Σn×n0]m×n,Σn×n=[σ100…00σ20…000σ3…0000⋱0000…σn],

where σ1≥σ2≥⋯≥σn≥0 are known as singular values. If rank(R)=r(r≤n), we have σ1≥σ2≥⋯≥σr>0 and σr+1=σr+2=⋯=σn=0.

Notes:

Relation to Eigen-Deconposition

The columns of U (left-singular vectors) are orthonormal eigenvectors of RRT, and the columns of V (right-singular vectors) are orthonormal eigenvectors of RTR. In other words, we have

RRT=UΛU−1RTR=VΛV−1

It is easy to verify them as we have

RTR=(U[Σ0]VT)TU[Σ0]VT=V([Σ0][Σ0])VT=VΣ2VT,RRT=U[Σ0]VT(U[Σ0]VT)T=U([Σ0][Σ0])UT=U[Σ2000]U

and VT=V−1,UT=U−1.

Notes:

SVD and eigen-decomposition

(1) Under what conditions are SVD and eigen-decomposition the same? First, R is a symmetric matrix, i.e., R=RT. Second, R is a positive semi-definite matrix, i.e., ∀x∈Rn,xTRx≥0.

(2) The difference between Λ in eigen-decomposition and Σ in SVD is that, the diagonal entries of Λ can be negative, while the diagonal entries of Σ are non-negative. What are the fundamental reasons underlying this difference? Why the requirements on the singular values in SVD (non-negative and in sorted order) do not prevent the generality of SVD?

Compact SVD

 If rank(R)=r(r≤n), we have R=UΣ~VT=[u1,u2,…,ur,…,um][σ1…0⋮⋱⋮⋱0…σr⋱0…00…0][v1Tv2T⋮vrT⋮vnT].

By removing zero components, we obtain

R=UrΣrVrT=[u1,u2,…,ur][σ1…0⋮⋱⋮0…σr][v1Tv2T⋮vrT]=[σ1u1,σ2u2,…,σrur][v1Tv2T⋮vrT]=∑i=1rσiuiviT,

where rank(σiuiviT)=1,i=1,2,…,r.

Notes:

Truncated SVD and Best Low-Rank Approximation

We can also approximate the matrix R with the k largest singular values as

Rk=UkΣkVkT=∑i=1kσiuiviT.

Apparently, R≠Rk unless rank(R)=k. This approximation is the best in following sense:

minB:rank(B)≤k∥R−B∥F=‖R−Rk‖F=∑i=k+1nσi2,minB:rank(B)≤k∥R−B∥2=‖R−Rk‖2=σk+1,

where ∥⋅∥F denotes the Frobenius norm and ∥⋅∥2 denotes the spectral norm, defined as the largest singular value of the matrix. That is, Rk is the best rank- k approximation to R in terms of both the Frobenius norm and spectral norm. Note the difference in terms of approximation errors when different matrix norms are used.

Notes:

Checkpoint 2 (SVD)

To understand Singular Value Decomposition (SVD), we must realize that pure eigen-decomposition has a severe limitation: it only works for square matrices. In machine learning, our raw data is almost always a rectangular matrix (e.g., N samples by D features).

1. Singular Value Decomposition & SVD (2) SVD is the ultimate mathematical tool because it exists for any real matrix, no matter its shape. If you have a rectangular data matrix R (of size m×n), SVD factorizes it into three matrices: R=UΣ~VT.

2. Relation to Eigen-Decomposition If R is rectangular, how do we find U and V? We use a clever trick to force the matrix to become square and symmetric.

3. SVD and eigen-decomposition When are SVD and eigen-decomposition the exact same thing? Because SVD enforces that all singular values (σ) must be positive, an eigen-decomposition only matches the SVD if its eigenvalues (λ) are also guaranteed to be positive. Therefore, the original matrix must be symmetric (R=RT) and Positive Semi-Definite (xTRx≥0).

4. Compact SVD If the rank (r) of your matrix is smaller than its dimensions, you will have singular values that are exactly 0 at the bottom of the Σ matrix. Because anything multiplied by 0 is 0, storing those values and their corresponding columns in U and V is a waste of computer memory. Compact SVD simply throws away the zero components. By rewriting the matrix multiplication as a sum of outer products, we get R=∑i=1rσiuiviT. We only sum up to the rank r, losing absolutely no information.

5. Truncated SVD and Best Low-Rank Approximation Compact SVD throws away exact zeros. But what if we throw away small (but non-zero) singular values to intentionally compress the data? If we stop the summation early at some number k (where k<r), we get a new, compressed matrix Rk=∑i=1kσiuiviT.

Principal Component Analysis (PCA)

What is PCA?

(1) Principal Component Analysis (PCA) is a statistical procedure that can be used to achieve feature (dimensionality) reduction.

(2) Note, feature reduction is different from feature selection. After feature reduction, we still use all the features, while feature selection selects a subset of features to use.

(3) The goal of PCA is to project the high-dimensional features to a lower-dimensional space with maximal variance and minimum reconstruction error simultaneously.

(9) We derive PCA based on maximizing variance, and then we show the solution also minimizes reconstruction error.

(6) In machine learning, PCA is an unsupervised learning technique, and therefore does not need labels.

Notes:

PCA to 1D

(1) To introduce PCA, we start from the simple case where PCA projects the features to a 1 -dimensional space.

(2) Formally, suppose we have np-dimensional ( p>1 ) features x1,x2,…,xn∈Rp.

(3) Let a∈Rp represent a projection that aTxi=zi,i=1,2,…,n where z1,z2,…,zn∈R1.

(4) PCA aims to solve

a∗=arg⁡max∥a∥=11n∑i=1n(zi−z¯)2.

(5) Note that the variance of the reduced data is

1n∑i=1n(zi−z¯)2

which means that PCA tries to find the projection with the maximum variance in reduced data.

Notes:

PCA to 1D (process)

Since

z¯=1n∑i=1nzi=1n∑i=1naTxi=aT(1n∑i=1nxi)=aTx¯,

the problem can be written as

a∗=arg⁡max∥a∥=11n∑i=1n(zi−z¯)2=arg⁡max∥a∥=11n∑i=1n(aTxi−z¯)2=arg⁡max∥a∥=11n∑i=1n(aTxi−aTx―)2=arg⁡max∥a∥=11n∑i=1naT(xi−x―)(xi−x―)Ta=arg⁡max∥a∥=1aT(1n∑i=1n(xi−x―)(xi−x―)T)⏟p×p covariance matrix a=arg⁡max ∥a∥=1aTCa,

where C=1n∑i=1n(xi−x¯)(xi−x¯)T, denotes the covariance matrix.

Notes:

aT[∑i(xi−x¯)(xi−x¯)T]a

- The whole point here is that we need to do something with this matrix: (xi−x¯)
- Note the matrix in the brackets only depends on your input data, this is called the covariance matrix (completely out of your dataset)
- It basically represents the reverse of the reduced data

PCA to k-dimensional space

(1) What if we want to project the features to a k-dimensional space? Then the PCA problem becomes

A∗=arg⁡maxA∈Rp×k:ATA=Iktrace(ATCA),

where A=[a1,a2,⋯,ak]∈Rp×k. Note that when projecting onto k-dimensional space, PCA requires different projection vectors to be orthogonal. Also, the trace above is the sum of the variances after projecting the data to each of the k directions as

trace(ATCA)=∑i=1k(aiTCai).

Notes:

Ky Fan Theorem

(1) Solving the problem in Eqn. (2) requires the follow theorem.
(2) Theorem. (Ky Fan) Let H∈Rn×n be a symmetric matrix with eigenvalues

λ1≥λ2≥⋯≥λn

and the corresponding eigenvectors U=[u1,…,un]. Then

λ1+⋯λk=maxA∈Rn×k:ATA=Iktrace(ATHA).

And the optimal A∗ is given by A∗=[u1,…,uk]Q with Q an arbitrary orthogonal matrix.

Notes:

Solutions to PCA

(1) Note that in Eqn. (2), the covariance matrix C is a symmetric matrix. Given the above theorem, we directly obtain

λ1+⋯λk=arg⁡maxA∈Rn×k:ATA=Iktrace(ATCA),A∗=[u1,…,uk]Q,

where λ1,…,λk are the k largest eigenvalues of the covariance matrix C, and the solution A∗ is the matrix whose columns are corresponding eigenvectors.
(2) It also follows from the above theorem that solutions to PCA are not unique, and they differ by an orthogonal matrix. We used the special case where Q=I, i.e., A∗=[u1,…,uk].

Notes:

Checkpoint 3 (PCA and solutions)

To understand Principal Component Analysis (PCA), imagine you are holding a 3D cloud of data points (like a swarm of bees). If you want to draw this 3D cloud on a 2D piece of paper, you have to squash it flat. If you squash it from the wrong angle, all the bees will overlap and look like a single blob. If you squash it from the right angle, you can still see the distinct shape and spread of the swarm. PCA is the mathematical tool that finds the absolute best angle to squash (project) your data.

1. What is PCA? PCA is an unsupervised dimensionality reduction technique.

2. PCA to 1D Let's start with the simplest case: compressing a p-dimensional dataset down to a single 1D line.

3. PCA to 1D (process) How do we mathematically maximize this variance?

4. PCA to k-dimensional space Usually, we want to compress our data to k dimensions, not just 1.

5. Ky Fan Theorem & Solutions to PCA How on earth do we actually solve arg⁡maxtrace(ATCA)? Your professor introduces the Ky Fan Theorem, which is the magical key to PCA.

Computing the covariance matrix:
To understand this, we have to look at how linear algebra allows us to take a long, tedious summation formula and compress it into a single, highly efficient matrix multiplication.

1. The Original Summation Formula The standard statistical formula for the Covariance Matrix (C) of a dataset is: $$C = \frac{1}{n} \sum_{i=1}^n(x_i - \bar{x})(x_i - \bar{x})^T$$ Let's look at the pieces of this equation:

When you multiply a column vector by a row vector, you get a full matrix (this is an outer product). The summation symbol ∑ means you have to calculate this p×p matrix for every single data point i, and then add all n of those matrices together. Doing this in a loop is incredibly slow.

2. Step 1: Constructing the Centered Data Matrix (X~) Instead of looking at data points one by one, we want to look at the entire dataset at once. Suppose your original data matrix X has all your data points stacked side-by-side as columns. To get rid of the (xi−x¯) part of the formula, we create a new matrix called the centered data matrix (X~). You build this by simply taking your original matrix X and subtracting the mean vector x¯ from every single column.

3. Step 2: The Matrix Multiplication Trick Now we have our centered data matrix X~ (size p×n). If we transpose it, we get X~T (size n×p), where all those centered columns become rows. What happens if we multiply X~ by X~T? If you recall "Matrix times Matrix - 4 ways" (specifically Pattern MM4) from your earlier slides, multiplying a matrix by another matrix is mathematically identical to taking the outer product of their corresponding columns and rows, and summing them all together.

By simply computing X~X~T, the rules of linear algebra automatically perform the exact summation of outer products that our original covariance formula required.

Therefore, the entire messy summation formula perfectly collapses into: $$C = \frac{1}{n}\tilde{X}\tilde{X}^T$$

EXAM TOPIC: How to compute Covariance Matrix (C) from Data (X)
If you are asked to compute the covariance matrix efficiently using matrices (no summations):

  1. Find the Mean: Calculate the mean column vector x¯ from your data.
  2. Center the Data (X~): Subtract the mean vector from every column of your original data matrix X.
    • Matrix Form: X~=X−X¯1nT (where 1nT is a row vector of n ones used to duplicate the mean vector across all columns).
  3. Compute Covariance (C): Multiply the centered matrix by its transpose and divide by the number of samples (n).
    • Matrix Form: C=1nX~X~T.

How to compute PCA efficiently?

(1) We define

X=[x1,x2,…,xn]∈Rp×nX―=1nX1n∈Rp×1X~=(X−X―1nT)∈Rp×n

where 1n is the n-dimensional all-one vector. Here, X~ is the centered X, which is obtained by subtracting the mean from each column.
(2) Then we have

C=1n∑i=1n(xi−x―)(xi−x―)T=1nx~x~T∈Rp×p.

If p is large, it is very costly to compute eigenvectors of C directly. Moreover, if p≫n, there are known computational problems.

Notes:


(1) However, we show that the SVD of X― provides what we need. If the SVD of X~ is X~=UΣ~VT, the columns of U (left-singular vectors) are orthonormal eigenvectors of X~X~T. Note that we have σ1≥σ2≥⋯≥σn≥0 in Eqn. (1) and thus, the first k columns of U correspond to the largest k eigenvalues of X~X~T.

(2) With the SVD of X~, if we want to project features to a k-dimensional (k<p) space, simply take the first k columns of U as the projection matrix. Generally, the process of computing PCA can be described as

X⏟p×n→X~⏟p×n→X~=UΣ~VT→G=Uk∈Rp×k.

Then the projected features are

GTX~=Z∈Rk×n.

It is worth noting that the k-th row in Z is called the k-th principal components (PC). Therefore, PCA achieves feature reduction by keeping only the first k PCs.

Notes:

Centered or not?

(1) Note that we project the centered data matrix X~ instead of the original data matrix X in Eqn (3). Maximal variance can be achieved in both cases, as the the covariance matrix C will not change.

(2) But the minimal reconstruction error can only be achieved when X~ is used, as shown in the next section.

(3) A common practice to avoid any confusion is to center the data before applying PCA, and use the centered data matrix in all computations.

Notes:

Have We Achieved the Minimal Reconstruction Error?

(1) First, we perform the verification in the case where we project and reconstruct the centered data matrix X~.

(2) Given the projection

GTX~=Z,

the reconstruction process is

Xˇ=GZ=GGTX~,

and the reconstruction error is

∥X~−X~∥F=‖X~−GGTX~‖F.

(3) We will show that

GGTX~=B∗=arg⁡minB:rank(B)≤k∥X~−B∥F,

which means Xˇ gives the minimum reconstruction error.

(4) We've already learned that B∗ is the truncated SVD of X~. So, we only need to show that GGTX~ is indeed the truncated SVD of X~ as follows. Note that similar arguments can be made for the spectral

Notes:

- We call the $GG^T$ matrix the reconstructing matrix (exactly the same size as the original matrix) - Note also that he $\hat{X}$ matrix is the truncated SVD of the $X$ matrix - It is a rank-$k$ approximation of $X$ - It is the best you can do if your $k$ cannot change - In some sense this means the model is a linear transformation (the best linear transformation you can take) - To prove this is quite easy ### More details Given $\tilde{\boldsymbol{X}}=\boldsymbol{U} \tilde{\boldsymbol{\Sigma}} \boldsymbol{V}^T$ and $\boldsymbol{G}=\boldsymbol{U}_k$, we have

\begin{aligned}
\boldsymbol{G G}^T \tilde{\boldsymbol{X}} & =\boldsymbol{U}_k \boldsymbol{U}_k^T \boldsymbol{U} \tilde{\boldsymbol{\Sigma}} \boldsymbol{V}^T \
& =\boldsymbol{U}_k \boldsymbol{U}_k^T\left[\boldsymbol{U}k \quad \boldsymbol{U}\right] \tilde{\boldsymbol{\Sigma}} \boldsymbol{V}^T \
& =\boldsymbol{U}_k\left[\begin{array}{ll}
\boldsymbol{I} & 0
\end{array}\right] \tilde{\boldsymbol{\Sigma}} \boldsymbol{V}^T \
& =\left[\begin{array}{ll}
\boldsymbol{U}_k & 0
\end{array}\right] \tilde{\boldsymbol{\Sigma}} \boldsymbol{V}^T \
& =\boldsymbol{U}_k \boldsymbol{\Sigma}_k \boldsymbol{V}k^T \
& =\sum
^k \sigma_i \boldsymbol{u}_i \boldsymbol{v}_i^T
\end

Asaresult,$Xˇ$givestheminimumreconstructionerror,whichis

\min _{\boldsymbol{B}: \operatorname{rank}(\boldsymbol{B}) \leq k}|\tilde{\boldsymbol{X}}-\boldsymbol{B}|_F=\left|\tilde{\boldsymbol{X}}-\boldsymbol{G} \boldsymbol{G}^T \tilde{\boldsymbol{X}}\right|F=\sqrt{\sum^n \sigma_i^2} .

**Notes**: - $U_k$ is the first $k$ columns of the $U$ matrix - Then you do block matrix multiplication - Remember $U_k$ is not an orthogonal matrix, it is a matrix with orthonormal columns - You can simply remove the 0s and you keep the 3 important matrices - 3 matrices multiplied together, the middle one is a diagonal matrix - The sigma and how many zeros it has will tell you how accurate your reconstruction matrix will be - Depends on all the sigmas that you throw away that are not zero - Whenever you throwaway a zero sigma, that means there is no covariance in that direction - This is the most optimal linear transformation - If you want your transformation to be more complex, like a neural network, you can achieve a better reconstruction, that is the idea of autoencoders. ### Connections with Autoencoders ![image-51.png\|334](/img/user/00%20-%20TAMU%20Brain/6th%20Semester%20(Spring%2026)/CSCE-421/Ex2/Visual%20Aids/image-51.png) (1) We assume that $\boldsymbol{X}=\left[\boldsymbol{x}_1, \boldsymbol{x}_2, \ldots, \boldsymbol{x}_n\right] \in \mathbb{R}^{p \times n}$ is already centered, for the simplicity of notations. (2) The outputs of this framework have the same dimension as inputs, and are supposed to reconstruct the inputs exactly. Without any constraint, the reconstruction task can be easily solved by directly copying. However, autoencoders come with a crucial restriction that the dimension of intermediate outputs should be smaller than inputs. **Notes**: - Here you can make the encoder to not be a linear layer, but instead be multi-layer - The only things is that we require E and D to be transpose of each other - Here this is purely unsupervised learning - This is reconstruction, you only need to compute your reconstructed $X$ ### <span style="color:rgb(254, 134, 22)">Checkpoint 4</span> (PCA and Reconstruction Error) **1. How to compute PCA efficiently?** - **The Problem:** The standard way to do PCA involves computing a covariance matrix $C$ of size $p \times p$. If you are processing images, $p$ (the number of pixels) could be in the millions. Creating and finding the eigenvectors for a matrix of a million by a million is computationally impossible for most computers. - **The Solution (SVD):** Instead of calculating the covariance matrix directly, we can just use Singular Value Decomposition (SVD) on the centered data matrix $\tilde{X}$. - If we compute the SVD so that $\tilde{X} = U \tilde{\Sigma} V^T$, the columns of the $U$ matrix automatically give us the exact orthonormal eigenvectors we need. - **The Process:** You take your data $X$, center it to get $\tilde{X}$, compute its SVD, and grab the first $k$ columns of the $U$ matrix to create your projection matrix $G$. Finally, you multiply $G^T \tilde{X}$ to get $Z$, which is your new, beautifully compressed data. **2. Centered or not?** You might wonder if you absolutely _must_ subtract the mean and center your data before running PCA. - Mathematically, you can achieve the "maximum variance" goal using either the raw data $X$ or the centered data $\tilde{X}$. - However, if you want to be able to accurately reconstruct the original data from your compressed data later, **you must use the centered data $\tilde{X}$**. Using the uncentered data will inject the mean into your reconstruction error, making it mathematically flawed. Always center the data! **3. Have We Achieved the Minimal Reconstruction Error? & More details** PCA claims to be the absolute best way to compress data linearly, but how do we prove it minimizes the reconstruction error? - **The Reconstruction:** To uncompress our $k$-dimensional data $Z$ back into its original $p$-dimensional space, we multiply it by our projection matrix $G$. The reconstructed matrix is $\check{X} = G Z = G G^T \tilde{X}$. - **The Proof:** The "More Details" slide breaks down the matrix math of $G G^T \tilde{X}$. When you substitute $G$ with the first $k$ columns of the SVD matrix ($U_k$), the math perfectly collapses into the formula $\sum_{i=1}^k \sigma_i u_i v_i^T$. - **Why is this magical?** That formula is the exact definition of **Truncated SVD**. Because we previously learned that Truncated SVD is mathematically guaranteed to be the absolute best low-rank approximation of a matrix, this proves that PCA achieves the lowest possible reconstruction error (which is equal to the sum of the discarded singular values $\sqrt{\sum_{i=k+1}^n \sigma_i^2}$). **4. Connections with Autoencoders** An autoencoder is a type of neural network trained via unsupervised learning to simply copy its input to its output. - To force the network to learn something useful, the middle "hidden" layer acts as a bottleneck, forcing the network to compress the data to a smaller dimension ($k < p$) before expanding it back out. - **The Connection:** If you build an autoencoder using just one linear layer for the encoder, one linear layer for the decoder, and no bias terms, the network's objective becomes exactly equivalent to PCA. - If you train this simple linear autoencoder using Gradient Descent, it will discover a set of weights that yields the exact same minimal reconstruction error as PCA. _(Note: The exact weight values might differ from PCA's eigenvectors by an orthogonal rotation, but the resulting compression power is identical)_. ### **30 - Efficient PCA & Autoencoders** - **Efficient PCA Computation (via SVD):** - _Problem:_ Covariance matrix $C$ is size $p \times p$. If $p \gg n$, calculating eigenvectors directly is computationally disastrous. - _Solution:_ Use SVD on the centered data matrix $\tilde{X} = U \tilde{\Sigma} V^T$. - _The Shortcut:_ The left-singular vectors (columns of $U$) are the exact eigenvectors of the covariance matrix. - _Steps:_ $X \to \tilde{X} \to \text{SVD}(\tilde{X}) \to G = U_k \to Z = G^T \tilde{X}$. - **Centered Data Requirement:** - PCA can maximize variance on uncentered data $X$, but to achieve **minimal reconstruction error**, the data **MUST** be centered ($\tilde{X}$). - **Reconstruction Error Proof:** - _Reconstructed Data:_ $\check{X} = G G^T \tilde{X}$. - _The Math:_ $G G^T \tilde{X}$ perfectly simplifies to $\sum_{i=1}^k \sigma_i u_i v_i^T$. - _Conclusion:_ PCA reconstruction is exactly equivalent to the **Truncated SVD** ($B^*$). Thus, it guarantees the absolute minimum reconstruction error possible for rank-$k$. - **Autoencoders vs. PCA:** - _Autoencoder:_ Neural net mapping inputs to outputs via a low-dimensional bottleneck to minimize reconstruction error. - A linear autoencoder (one linear hidden layer, no bias, shared weights) is mathematically equivalent to PCA. - Gradient descent will converge to the **exact same minimal reconstruction error** as PCA. ### Exam #### Details *Format similar to the first exam* - Calculate very simple numbers - No code - There will be one bonus question that is 10 points - it requires a little bit of thinking (others are straight forward) - It is not a calculation, but is based on your understanding - Bring cheat sheet both sides #### Topics **Convolutional networks**: - Size (given input and operation) - calculate size of the output and how many parameters on the output - Convolutional layer is the most important - Poll layer has no parameters - Batch normalization layer: number of parameters and size - It is different between fully connected layer and convolutional layer, what is the number of parameters on each - The difference between training and test - Layer norm. is not required - Also ResNet - The important thing about it is the skip connection - Understand what it is and, when we need it, how? parameters? **Attention, Transformer, LLMs** - Know these operations - What is attention - Why we use decoder models - How to make a decoder model: - Causal attention - How to use it to generate **PCA** - Know the $X$, $\tilde{X}$, $C$, SVD and eigen-decomposition - Know how you get your $G$, and then from there compute your $Z$