Published on

# Machine Learning Part 2: Loss Functions & Convexity

Authors
• Name
Jyotir Sai
Engineering Student

Loss functions tell us how far our predicted value is from the ground truth. What are some important properties that loss functions should have? Why do we use squared error loss functions? The purpose of this blog is to answer those questions. Before diving into loss functions and convexity, I want to talk about the math notation we'll be using.

## Vector Representation

The input matrix, $\mathbf{X}$, can be written as:

$\mathbf{X} = \begin{pmatrix} - \mathbf{x^{(1)}} -\\ - \mathbf{x^{(2)}} -\\ ...\\ - \mathbf{x^{(M)}} -\\ \end{pmatrix} \epsilon \mathbb{R}^{M}$

In the above representation, each row vector corresponds to a sample and we have $M$ samples/rows. The values in this vector are an element of the $\mathbb{R}^{M}$ space because if we have $M$ samples then we have $M$ dimensions. The values in a single row vector correspond to each feature i:

$\mathbf{x^{(1)}} = [ x_{1}^{(1)}, x_{2}^{(1)}, ... , x_{i}^{(1)}]$
$\mathbf{x^{(2)}} = [ x_{1}^{(2)}, x_{2}^{(2)}, ... , x_{i}^{(2)}]$
$...$
$\mathbf{x^{(M)}} = [ x_{1}^{(M)}, x_{2}^{(M)}, ... , x_{i}^{(M)}]$

It is also common to write the matrix with the rows as features and columns as samples:

$\mathbf{X} = \begin{pmatrix} | & | & & | \\ \mathbf{x^{(1)}} & \mathbf{x^{(2)}} & ... & \mathbf{x^{(M)}} \\ | & | & & | \end{pmatrix}$

In machine learning, we usually use matrices / vectors to represent our variables so we can take advantage of vectorization.

Let's talk about how to use linear regression when dealing with vectors. The equation for linear regression with a matrix of input values is written below.

$h(\mathbf{X}) = \mathbf{w}^{T}\mathbf{X}+b$

Instead of a single slope value, we have a vector of values, $\mathbf{w}$, known as the weights.

$\mathbf{w} = \begin{pmatrix} w_{1}\\ w_{2}\\ ...\\ w_{i}\\ \end{pmatrix} \epsilon \mathbb{R}^{i}$

Each corresponding input feature $x_{i}^{m}$, has a corresponding weight $w_{i}$. The y-intercept, $b$, is still represented with $b$ except we call this term the bias.

For aesthetic reasons, the bias term is sometimes absorbed into the weight vector so we can write:

$h(\mathbf{X}) = \mathbf{w}^{T}\mathbf{X}$

where $\mathbf{X}$ and $\mathbf{w}$ are now:

$\mathbf{X} = \begin{pmatrix} 1\\ x_{1}\\ ...\\ x_{d}\\ \end{pmatrix} \epsilon \mathbb{R}^{d+1}, \mathbf{w} = \begin{pmatrix} b\\ w_{1}\\ ...\\ w_{d}\\ \end{pmatrix} \epsilon \mathbb{R}^{d+1}$

The above vector representation is the same as writing:

$h(x) = b+w_{1}x_{1}+w_{2}x_{2}+...+x_{d}w_{d}$

## Loss Function

We previously discussed the squared error loss function:

$J = \frac{1}{2}\sum_{m=1}^{M}\left ( y_{m}-h(x_{m}) \right )^2$

Another loss function we can use is the mean squared error:

$J = \frac{1}{M}\sum_{m=1}^{M}\left ( y_{m}-h(x_{m}) \right )^2$

Now, let's express the loss function in a matrix-vector form. The L2 norm of a vector is defined as:

$\left \| \nu \right \|^{2} = \sum_{i=1}^{d}\nu _{i}^{2}$

The L2 norm can be used to express the summation term in the loss function in vector form:

$\sum_{m=1}^{M}\left ( y_{m}-h(x_{m}) \right )^2 \rightarrow \left \| \mathbf{y}-\mathbf{h} \right \|^{2}$

As seen above, $h$ is simply equal to $\mathbf{w}^{T}\mathbf{X}$.

$\left \| \mathbf{y}-\mathbf{h} \right \|^{2} \rightarrow \left \| \mathbf{y}-\mathbf{w}^{T}\mathbf{X} \right \|^{2}$

The matrix-vector version of the mean squared error is therefore:

$J = \frac{1}{M} \left \| \mathbf{y}-\mathbf{w}^{T}\mathbf{X} \right \|^{2}$

or

$J = \frac{1}{M} \left \| \mathbf{X}\mathbf{w}-\mathbf{y} \right \|^{2}$

Why do we use these loss functions in particular? Why do we square the error instead of cubing it or raising it to a higher power? Since we want the minimum of a loss function, we want to differentiate the loss function and find where its derivative is 0. Therefore, we want to choose a loss function that is differentiable. We also want a loss function where the point at which the derivative is 0 corresponds to the global minimum. More formally, we want a loss function that is both smooth and convex.

## Convex Sets

Before talking about convex functions, we'll first have to cover convex sets. A set, $S$, is convex if and only if

$\forall \: x, y \: \epsilon \: S \: \: , \: \: \forall \: \lambda \: \epsilon \: (0,1)$
$\lambda x + (1-\lambda)y \: \epsilon \: S$

In plain english, the above means that for all $x, y$ that are an element of $S$, and for all $\lambda$ values between 0 and 1, the equation on the second line yields a value that is also apart of the set. Let's look at some visual examples. The example on the left is a convex set because for all points $x$ and $y$, the line that connects them will always be inside the set. The example on the right is not a convex set since the line between $x$ and $y$ goes outside of the set. Our value of $\lambda$ picks a point on this line. For example, $\lambda = 0.5$ results in the red point in the middle of the line.

$\lambda x + (1-\lambda)y = 0.5x+(1-0.5)y = \frac{1}{2}(x+y)$

Simple sets like the empty set, lines, and hyperplanes are all considered convex. Discontinuous sets are not convex.

## Convex Functions

A real-valued function, $f$, is convex if the domain of $f$ is a convex set. For all $x$ and $y$ in the domain of $f$, and for all $\lambda \: \epsilon \: (0, 1)$, we have the following relation

$\lambda f(x) + (1-\lambda)f(y) \geq f(\lambda x + (1-\lambda)y)$

The above relation holds true for a convex function. A quadratic is a convex function, so the green point will always lie above the red point. The line between the points $f(x)$ and $f(y)$ will always lie above the function $f$. The cubic function shown above is not a convex function since the green point lies below the red point, violating the above relation.

There are two more properties of convex functions that you should know. Let's start with the 1st-order condition which states that for all $x,y$ in the domain of $f$

$f(y) \geq f(x) + \nabla_{x} f(x)^{T} (y-x)$

The term on the right is the 1st-order Taylor polynomial expansion where

$\nabla_{x} f(x) = \begin{pmatrix} \frac{\partial}{\partial x_{1}}f(x)\\ ...\\ \frac{\partial}{\partial x_{i}}f(x) \end{pmatrix}$

Let's again look at a visual example. The blue line is the 1st-order Taylor expansion (tangent line). According to the 1st-order condition, this line will never cross "inside" the function. It will always be less than or equal to $f(y)$. All the points on the graph are "above" the tangent line.

The 2nd-order condition states that

$\nabla_{x}^{2}f(x) \geq 0$

where $\nabla_{x}^{2}f(x)$ is the Hessian matrix. The 2nd-order condition says that if the 2nd-order derivative of a function is greater than or equal to 0 (positive semi-definite), then the function is convex. A matrix is positive semi-definite if its eigenvalues are greater than or equal to 0.

To summarize, if a function, $f$, is twice differentiable, then the following conditions are equivalent.

1. $\lambda f(x) + (1-\lambda)f(y) \geq f(\lambda x + (1-\lambda)y)$ ($f$ is convex)

2. $f(y) \geq f(x) + \nabla_{x} f(x)^{T} (y-x)$

3. $\nabla_{x}^{2}f(x) \geq 0$

## Revisiting Loss Functions

We previously looked at the mean squared error loss function:

$J = \frac{1}{M} \left \| \mathbf{X}\mathbf{w}-\mathbf{y} \right \|^{2}$

We add a factor of 1/2 to the above function so that when we take the derivative it cancels out the 2 from the exponent. It is done purely for aesthetic reasons.

$J = \frac{1}{2M} \left \| \mathbf{X}\mathbf{w}-\mathbf{y} \right \|^{2}$

Now, let's work on finding the derivative for this function. The L2-norm can be rewritten as

$J = \frac{1}{2M} \left ( \mathbf{X}\mathbf{w}-\mathbf{y} \right )^{T}\left ( \mathbf{X}\mathbf{w}-\mathbf{y} \right )$

Remember that $(AB)^{T}=B^{T}A^{T}$

$J = \frac{1}{2M} \left ( \mathbf{w}^{T} \mathbf{X}^{T}-\mathbf{y} \right )\left ( \mathbf{X}\mathbf{w}-\mathbf{y} \right )$
$J = \frac{1}{2M} \left [ \mathbf{w}^{T} \mathbf{X}^{T} \mathbf{X}\mathbf{w}-\mathbf{w}^{T} \mathbf{X}^{T} \mathbf{y}-\mathbf{y}^{T}\mathbf{X}\mathbf{w}+\mathbf{y}^{T}\mathbf{y} \right ]$

Inside the brackets, the two terms in the middle are actually the same since

$\mathbf{y}^{T}\mathbf{X}\mathbf{w}=\left (\mathbf{y}^{T} \mathbf{X} \mathbf{w} \right)^{T}=\mathbf{w}^{T}\mathbf{X}^{T}\mathbf{y}$

The above term actually results in a scalar, that's why it's equal to its transpose.

$J = \frac{1}{2M} \left [ \mathbf{w}^{T} \mathbf{X}^{T} \mathbf{X}\mathbf{w}-2\mathbf{y}^{T}\mathbf{X}\mathbf{w}+\mathbf{y}^{T}\mathbf{y} \right ]$

Taking the derivative

$\frac{\partial J(\mathbf{w})}{\partial \mathbf{w}} = \frac{1}{2M}\left [2 \mathbf{X}^T \mathbf{X} \mathbf{w} - 2 \mathbf{X}^T \mathbf{y} \right]$

The above derivative uses the following identities

$\frac{\partial }{\partial x} \mathbf{x}^T \mathbf{S} \mathbf{x} = 2\mathbf{S}\mathbf{x}$
$\frac{\partial }{\partial x} \mathbf{A}\mathbf{x} = \mathbf{A}^{T}$

Where $\mathbf{S}=\mathbf{X}^T \mathbf{X}$ and $\mathbf{A}=\mathbf{y^{T}} \mathbf{X}$. The $\mathbf{y}^T \mathbf{y}$ term has no $\mathbf{w}$ term so its derivative is just 0. The derivative can be further simplified to

$\frac{\partial J(\mathbf{w})}{\partial \mathbf{w}} = \frac{1}{M} \mathbf{X}^{T} \left (\mathbf{X}\mathbf{w}-\mathbf{y} \right)$

Taking the 2nd derivative

$\frac{\partial^{2} J(\mathbf{w})}{\partial \mathbf{w}^{2}} = \nabla_{w}^{2} J(\mathbf{w}) = \frac{1}{M} \mathbf{X}^{T} \mathbf{X}$

We want to know whether the above equation satisfies the 2nd-order condition ($\nabla_{w}^{2}f(w) \geq 0$). How do we tell whether the matrix $\mathbf{X}^T\mathbf{X}$ is positive semi-definite? A matrix $\mathbf{M}$ is positive semi-definite if the number produced by $\mathbf{z}^{T}\mathbf{M}\mathbf{z}$ is non-negative where $\mathbf{z}$ is a nonzero column vector.

$\mathbf{z}^T \left (\mathbf{X}^T \mathbf{X} \right ) \mathbf{z}$
$\left (\mathbf{Xz} \right )^{T} \left (\mathbf{Xz} \right ) = \left \| \mathbf{Xz} \right \|^{2} \geq 0$

The above shows that the mean-squared error function is indeed convex.