Machine Learning Techniques · IIT Madras BS Degree

Representation learning: compress, project, find the best line

Notes for lectures 3 to 5. Learning is treated as compression: store a representative and one coefficient per point, replace points that miss the line by their projections, and choose the line that loses the least. That line turns out to be an eigenvector.

Part 2 of 18 Instructor Prof. Arun Rajkumar About 68 min of video

At a glance

  • Running theme for the whole course: comprehension is compression. For every algorithm, ask where the compression happens.
  • If all points lie on a line through the origin, store one representative vector plus one coefficient per point: \(d + n\) numbers instead of \(d \times n\), with exact reconstruction.
  • A point off the line is replaced by a proxy on the line. The best proxy is the projection \((\mathbf{x}^\top\mathbf{w})\,\mathbf{w}\) when \(\lVert\mathbf{w}\rVert = 1\).
  • Every line compresses equally well; lines differ in reconstruction error. Minimizing that error is the same as maximizing \(\mathbf{w}^\top C\,\mathbf{w}\), solved by the eigenvector of \(C\) with the largest eigenvalue.
  • Residues can still carry structure, so repeat on the residues to get \(\mathbf{w}_2, \mathbf{w}_3, \dots\). Centre the data first, since every line here passes through the origin.
L3 · 1:03

Unsupervised learning, made precise

The course begins with unsupervised learning, and within it with representation learning. The starting goal is deliberately vague: given a set of data points, understand something useful about them. Three words need pinning down: data points, understand and useful.

Data points are the easy part. Each is a vector of \(d\) real numbers, its features. Record the height, weight and age of 100 people and each person becomes a point in \(\mathbb{R}^3\), one coordinate per feature.

\[ \text{Input: } \{\mathbf{x}_1, \mathbf{x}_2, \dots, \mathbf{x}_n\}, \quad \mathbf{x}_i \in \mathbb{R}^d \qquad \text{Output: a compressed representation of the data set} \]

\(n\) is the number of data points (people, images, …) and \(d\) the number of features recorded for each.

L3 · 3:53

Comprehension is compression

The lecture answers "what does it mean to understand?" with a quote from the computer scientist and philosopher Gregory Chaitin: comprehension is compression. Here, comprehending, understanding and learning are treated as the same thing. If you can compress information so that only its important part remains, and that part is enough to explain the data to someone else, you have learnt from the data.

Ask of every algorithm in this course: where exactly is the compression happening?

L3 · 7:15

One representative, one coefficient per point

How many numbers does it take to store these four points?

\[ \mathbf{x}_1 = \begin{bmatrix}-7\\-14\end{bmatrix},\quad \mathbf{x}_2 = \begin{bmatrix}2.5\\5\end{bmatrix},\quad \mathbf{x}_3 = \begin{bmatrix}0.5\\1\end{bmatrix},\quad \mathbf{x}_4 = \begin{bmatrix}0\\0\end{bmatrix} \]

The naive answer is \(4 \times 2 = 8\). But in every point the second coordinate is twice the first. So store one representative \([1, 2]^\top\) and, for each point, one coefficient: \(-7,\ 2.5,\ 0.5,\ 0\). That is 6 numbers, and the data comes back exactly: \(\mathbf{x}_2 = 2.5 \times [1, 2]^\top = [2.5, 5]^\top\).

x₁ x₂ y = 2x [−7, −14] [2.5, 5] [1, 2] [0, 0], [0.5, 1]
StoredNumbers
Raw points, \(n \times d\)8
Representative \([1, 2]\)2
Coefficients \(-7, 2.5, 0.5, 0\)4
Compressed total, \(d + n\)6
A billion such points10⁹ + 2
Four points, one line. All four lie on \(y = 2x\), so one representative along that line plus a coefficient per point rebuilds every point exactly. For a billion points the saving approaches 50%.

Two points from this example matter later.

  • The representative is not unique. Any nonzero vector on the line works. With \([1/\sqrt5,\ 2/\sqrt5]^\top\) the coefficients become \(-7\sqrt5,\ 2.5\sqrt5,\ 0.5\sqrt5,\ 0\) and reconstruction is still exact. The zero vector is not allowed, because nothing can be rebuilt from it. 14:03
  • The general count. \(n\) points in \(d\) dimensions take \(d \times n\) numbers raw. If they all lie on one line through the origin, \(d + n\) numbers suffice: \(d\) for the representative and one per point.
L3 · 17:22

When one point leaves the line

Take four points on \(y = x\), for example \([-5,-5]\), \([1,1]\), \([7,7]\) and \([10,10]\), and add one more, \(\mathbf{x}_5 = [5, 6]\), which is not on the line. One representative can no longer rebuild every point. There are three ways out:

OptionExact?Numbers for 5 pointsVerdict
Store the raw pointsYes10No compression
Two representatives \([1,0]\), \([0,1]\), two coefficients per pointYes4 + 10 = 14Worse than raw: \(2n + 4\)
One representative; replace \(\mathbf{x}_5\) by a proxy on the lineNo2 + 5 = 7Compresses, at a small loss

Exact reconstruction and fewer representatives cannot both be kept. The lecture gives up exactness 21:26. Pretend the data set contained a proxy for \(\mathbf{x}_5\) on the line instead of \(\mathbf{x}_5\) itself, and compress as before.

L3 · 22:39

The best proxy is the projection

The best proxy is the point on the line where we lose the least: the error vector we would have to add back to the proxy to recover \(\mathbf{x}\) should be as short as possible. A line through the origin is every multiple \(c\,\mathbf{w}\) of a nonzero representative \(\mathbf{w}\), so we choose the scalar \(c\):

\[ \min_{c \in \mathbb{R}} \; \lVert \mathbf{x} - c\,\mathbf{w} \rVert^2 \;=\; \min_{c}\; (x_1 - c\,w_1)^2 + (x_2 - c\,w_2)^2 \]

Setting the derivative with respect to \(c\) to zero (the lecture leaves this as an exercise): \(-2w_1(x_1 - c w_1) - 2w_2(x_2 - c w_2) = 0\), so

\[ c^* = \frac{x_1 w_1 + x_2 w_2}{w_1^2 + w_2^2} = \frac{\mathbf{x}^\top \mathbf{w}}{\lVert \mathbf{w} \rVert^2}, \qquad \text{proxy} = \frac{\mathbf{x}^\top \mathbf{w}}{\lVert \mathbf{w} \rVert^2}\,\mathbf{w} \]

The numerator is the dot product of \(\mathbf{x}\) and \(\mathbf{w}\); the denominator is the squared length of \(\mathbf{w}\). The same formula holds in any dimension.

Convention used from here on

Since any vector on the line can represent it, always pick \(\mathbf{w}\) with \(\lVert\mathbf{w}\rVert = 1\) (a point on the unit circle). The denominator disappears:

\[ \text{proxy of } \mathbf{x} \text{ on the line } \mathbf{w} = (\mathbf{x}^\top \mathbf{w})\,\mathbf{w}, \qquad \text{coefficient} = \mathbf{x}^\top\mathbf{w} \]
2.50
Squared error ‖x₅ − c w‖²–
At c* = x₅ᵀw–
Finding the proxy for \(\mathbf{x}_5 = [5, 6]\). The line is \(y = x\) with unit representative \(\mathbf{w} = [1, 1]/\sqrt2\). Slide the proxy along the line: the dashed error vector is shortest at \(c^* = \mathbf{x}_5^\top\mathbf{w} = 11/\sqrt2 \approx 7.78\), the proxy \([5.5, 5.5]\), where it meets the line at a right angle. The squared error there is 0.5.

Nobody tells us which points lie on the line. Off-line points are the rule, not the exception: plot height against weight for a class of 100 people and perhaps no point sits exactly on the line we would draw. An exact linear relationship between features is a myth outside physical laws. So we always use proxies, and the line itself must be found from the data.

Any line gives the same compression: one representative plus one coefficient per point. What differs is how much we lose. The red line in the lecture beats the blue one because rebuilding the data from its proxies gives a smaller reconstruction error.

Goal: find the line through the origin with the least reconstruction error.

From reconstruction error to \(\mathbf{w}^\top C\,\mathbf{w}\)

For a line given by a unit vector \(\mathbf{w}\), add up each point's squared error. The lecture uses the average rather than the sum; the same \(\mathbf{w}\) minimizes both, and the average is easier to interpret later.

\[ f(\mathbf{w}) = \frac{1}{n}\sum_{i=1}^{n} \big\lVert \mathbf{x}_i - (\mathbf{x}_i^\top\mathbf{w})\,\mathbf{w} \big\rVert^2, \qquad \lVert\mathbf{w}\rVert^2 = \mathbf{w}^\top\mathbf{w} = 1 \]

Write the squared length as a vector dotted with itself and expand. Using \(\mathbf{w}^\top\mathbf{w} = 1\):

\[ \big(\mathbf{x}_i - (\mathbf{x}_i^\top\mathbf{w})\mathbf{w}\big)^\top\big(\mathbf{x}_i - (\mathbf{x}_i^\top\mathbf{w})\mathbf{w}\big) = \mathbf{x}_i^\top\mathbf{x}_i - 2(\mathbf{x}_i^\top\mathbf{w})^2 + (\mathbf{x}_i^\top\mathbf{w})^2\,\mathbf{w}^\top\mathbf{w} = \lVert\mathbf{x}_i\rVert^2 - (\mathbf{x}_i^\top\mathbf{w})^2 \]

The first term does not involve \(\mathbf{w}\), so dropping it does not change the minimizer. Minimizing \(-\tfrac1n\sum(\mathbf{x}_i^\top\mathbf{w})^2\) is the same as maximizing it without the minus sign. Finally, \((\mathbf{x}_i^\top\mathbf{w})^2 = \mathbf{w}^\top\mathbf{x}_i\,\mathbf{x}_i^\top\mathbf{w}\), and \(\mathbf{w}\) does not depend on \(i\), so it comes out of the sum:

\[ \min_{\lVert\mathbf{w}\rVert=1} f(\mathbf{w}) \;\Longleftrightarrow\; \max_{\lVert\mathbf{w}\rVert=1} \frac1n\sum_{i=1}^n (\mathbf{x}_i^\top\mathbf{w})^2 = \max_{\lVert\mathbf{w}\rVert=1} \mathbf{w}^\top \Big(\frac1n\sum_{i=1}^n \mathbf{x}_i\mathbf{x}_i^\top\Big)\mathbf{w} \]
Result \[ \max_{\lVert\mathbf{w}\rVert = 1} \; \mathbf{w}^\top C\,\mathbf{w}, \qquad C = \frac1n\sum_{i=1}^n \mathbf{x}_i\mathbf{x}_i^\top \in \mathbb{R}^{d\times d} \]

\(C\) is an average of \(n\) matrices of size \(d \times d\), one per data point. The lecture calls it the covariance matrix. The maximizer is the eigenvector of \(C\) with the largest eigenvalue 16:00.

\(\frac1n\sum \mathbf{x}_i\mathbf{x}_i^\top\) is the usual covariance matrix only when the data has mean zero. Lecture 5 adds that step (centring) for a different reason. The lecture says more about why a covariance appears in an error-minimization problem later; for now the takeaway is that the problem has a well-understood eigenvector solution.

110°
Avg reconstruction error f(w)–
wᵀCw–
Sum (fixed)–
Synthetic, centred data (80 points), generated for this page. Rotate the line and watch the two readouts trade off. Their sum is always the average of \(\lVert\mathbf{x}_i\rVert^2\), the constant term dropped in the derivation. So the line with the smallest error is the line with the largest \(\mathbf{w}^\top C\mathbf{w}\): the top eigenvector of \(C\).

Treat the best-line solver as a black box: data goes in, a line comes out. That line is not always satisfying. Suppose 3-D data points all lie on a plane. The best line will lie in that plane (a line outside it would cost more to project onto), but the plane is the real structure, and a single line misses half of it.

The part we have been calling "error" may then carry information. Pure noise would point every which way. Here the error vectors, the residues \(\mathbf{x}_i - (\mathbf{x}_i^\top\mathbf{w})\mathbf{w}\), all line up along one direction.

Data on a plane in 3-D w₁ dots: data · rings: proxies on w₁ · dashes: residues Residues, redrawn from one origin w₂ they all fall on one line: more structure to extract
Residues can carry information. When the data really lives on a plane, the leftovers after projecting onto \(\mathbf{w}_1\) all point along a second in-plane direction. Fitting a line to the residues recovers it as \(\mathbf{w}_2\).

A possible algorithm L5 · 7:42

  1. Repeated best-line fitting
  2. Input \(\{\mathbf{x}_1, \dots, \mathbf{x}_n\}\), \(\mathbf{x}_i \in \mathbb{R}^d\).
  3. Find the best line \(\mathbf{w}_1\): minimize the reconstruction error, equivalently maximize \(\mathbf{w}^\top C\mathbf{w}\).
  4. Replace each point by its residue: \(\mathbf{x}_i \leftarrow \mathbf{x}_i - (\mathbf{x}_i^\top\mathbf{w}_1)\,\mathbf{w}_1\).
  5. Repeat on the residue data set to get \(\mathbf{w}_2\), then \(\mathbf{w}_3\), and so on, at most \(d\) times.
L5 · 11:03

Centre the data first

Every line here passes through the origin, because a proxy is a coefficient times the representative and a coefficient of 0 must give the origin. If the data sits far from the origin, the best line through the origin can miss the direction in which the data is spread. The lecture's example: the x-axis can beat the line that follows the data.

The fix is to centre the data before anything else: compute the mean and subtract it from every point. The new data set has its mean at the origin, so lines through the origin now follow the spread.

\[ \boldsymbol{\mu} = \frac1n\sum_{i=1}^n \mathbf{x}_i, \qquad \mathbf{x}_i \leftarrow \mathbf{x}_i - \boldsymbol{\mu} \quad \text{for all } i \]
Not centred best line through 0 direction of spread mean μ 0 the line mostly captures where the mean is Centred: xᵢ − μ best line 0 now the line follows the spread
Why centre? The same eight points before and after subtracting their mean (illustrative points). Before centring, the best line through the origin is pulled toward the mean. After centring, it runs along the direction the data actually varies.
L5 · 14:30

Open questions, answered in Part 3

L5 ends with four questions. Answering them turns the procedure into the first algorithm of the course, a representation learning algorithm for unsupervised learning.

  1. How do we solve \(\max_{\lVert\mathbf{w}\rVert=1} \mathbf{w}^\top C\mathbf{w}\)? (The hint: an eigenvector.)
  2. How many times should the residue step be repeated?
  3. Where exactly is the compression happening once there are several lines?
  4. With several lines and several coefficients per point, what representation are we actually learning?

Key terms

Representation learning
Finding a compact description of data points that keeps what matters.
Comprehension is compression
The course's running theme: learning means keeping only the important part of the data.
Representative
One vector shared by the whole data set that spans the chosen line.
Coefficient
The single number per point that scales the representative, \(\mathbf{x}^\top\mathbf{w}\) for unit \(\mathbf{w}\).
Proxy
The point on the line that stands in for an off-line point: its projection \((\mathbf{x}^\top\mathbf{w})\mathbf{w}\).
Projection
The closest point on a line to a given point; the error is perpendicular to the line.
Reconstruction error
Average squared distance between points and their proxies, \(f(\mathbf{w})\).
Covariance matrix \(C\)
\(\frac1n\sum\mathbf{x}_i\mathbf{x}_i^\top\) for centred data; the best line is its top eigenvector.
Residue
What is left of a point after removing its proxy: \(\mathbf{x} - (\mathbf{x}^\top\mathbf{w})\mathbf{w}\).
Centring
Subtracting the mean from every point so lines through the origin follow the data.

Test yourself

Q1How many numbers does it take to store 1,000 points in \(\mathbb{R}^{50}\) that all lie on one line through the origin?

\(d + n = 50 + 1000 = 1050\), instead of \(d \times n = 50{,}000\).

Q2Why is using the two representatives \([1,0]\) and \([0,1]\) a bad way to handle a point that is off the line?

It is exact, but every point then needs two coefficients, and the representatives add 4 more numbers: \(2n + 4\), which is more than the raw \(2n\).

Q3Project \(\mathbf{x} = [3, 1]\) onto the line spanned by \(\mathbf{w} = [1, 0]\). What is the proxy and the squared error?

\(\mathbf{w}\) already has length 1, so the coefficient is \(\mathbf{x}^\top\mathbf{w} = 3\) and the proxy is \([3, 0]\). The error vector is \([0, 1]\), squared error 1.

Q4Why can the term \(\frac1n\sum\lVert\mathbf{x}_i\rVert^2\) be dropped when choosing \(\mathbf{w}\)?

It does not depend on \(\mathbf{w}\). Adding a constant to every candidate's objective does not change which candidate is smallest.

Q5All lines through the origin give the same compression ratio. Then what makes one line better than another?

Its reconstruction error: how far the points are from their proxies on that line. The best line minimizes it.

Q6Why centre the data before looking for lines?

All candidate lines pass through the origin. If the data is far from the origin, the best such line is pulled toward the mean instead of following the direction the data varies in.