Andrew Ng is a Computer Science professor at Stanford. He is also one of the co-founders of the Coursera platform. Andrew recently joined the Chinese company Baidu as a chief scientist.

His main research focus is on Machine Learning, Artificial Intelligence and Deep Learning. He teaches the Machine Learning class in Coursera, which lasts 10 weeks and is comprised of videos, multiple choice assignments and programming assingments (with Octave).

Professor Andrew is a very good lecturer and makes the content more accessible by relying more on intuition rather than rigor, but always mentioning that a given result is backed by mathematical proofs. The material is very practical and he often discusses about real world techniques and applications.

Throughout the classes I got curious about the math behind many of the results he presented, so this post is an attempt to help me with that, as well as providing a overview of the content of the course.

### Supervised Learning

Suppose we have a set of data points and each of these points encodes information about a set of features, and for each of these vectors we are also given a value or label, which represents the desired output. We call this set a **training set**.

We want to find a black box that encodes some structure from this data and is able to predict the output of a new vector with certain accuracy.

It’s called supervised because we require human assistance in collecting and labeling the training set. We’ll now discuss some methods for performing supervised learning, specifically: linear regression, logistic regression, neural networks and support vector machines.

### Linear regression

We are given a set of data points, where and a set of corresponding values , where .

Linear regression consists in finding a hyperplane in , which we can represent by (here the constant is represented by and we assume ), such that the sum of the square distances of and the value of in that hyperplane, that is, , for each , is minimized.

More formally, we can define a function:

We want to fit a hyperplane that minimizes . We can do so by finding the optimal parameters :

.

This linear regression with the least square cost function is also known as **linear least squares**.

We can solve this problem numerically, because it’s possible to show that is convex. One common method is gradient descent (we used this method before for solving Lagrangian relaxation).

We can also find the optimal analytically, using what is known as the **normal equation**:

This equation comes from the fact that a convex function has its minimum/maximum value when its derivative in terms of its input is 0. In our case, .

The assumption is that our data lies approximately in a hyperplane, but in reality it might be closer to a quadratic curve. We can then create a new feature that is a power of one of the original features, so we can represent this curve as a plane in a higher dimension.

This method also returns a real number corresponding to the estimated value of , but many real world problems are classification problems, most commonly binary (yes/no). For this case linear regression doesn’t work very well. We’ll now discuss another method called logistic regression.

### Logistic regression

In logistic regression we have and the idea is that after trained, our model returns a probability that a new input belongs to the class .

Here we still train our model to obtain and project new input in the corresponding hyperplane to obtain a numerical value. But this time, we convert this to the range [0-1], by applying the sigmoid function, so it becomes

This represents the probability that given parametrized by , the output will be 1, that is, . The probability of y being 0 is the complementary, that is

The likelihood of the parameter to fit the data can be given by

(1)

We want to find the maximum likelihood estimation (MLE), that is, the parameter that minimizes (1). We can apply the log function to the , because the optimal is the same in both. This get us

(2)

This function doesn’t allow a closed form, so we need a numerical algorithm, such as gradient descent. This function is not necessarily convex either, so we might get stuck in local optima.

After estimating the parameter , we can classify a input point by:

### Neural Networks

Neural networks can be seen as a generalization of the simple form of logistic regression seen above. In there, we have one output () that is a combination of several inputs (). We can use a diagram to visualize it. More specifically, we could have a directed graph like the following:

Here we have nodes representing the input data and the output and each edge as a corresponding parameter . We can partition this graph in layers. In this case, the input and the output layer. In order to generalize that, we could add intermediate layers, which can be called hidden-layers:

In the general case, we have layers. Between layers an , we have a complete bipartite graph. Each edge connecting node at level to node at level is associated to a . The idea at each node it the same: perform the linear combination of the nodes incident to it and then apply the sigmoid function. Note that at each layer, we have one node that is not connected to any of the previous layers. This one acts as the constant factor for the nodes in the following layer. The last node will represent the final value of the function based on the input .

There’s a neat algorithm to solve this, called back-propagation algorithm.

**Forward-propagation** is the method of calculating the cost function for each of the nodes. The idea is the following. For each level we’ll use variables and .

Initialize :

For the following layers we do:

for the simple case of binary classification, the last layer will contain a single node and after running the above procedure, it will contain the cost function for an input :

Then, we can define the cost function to be the average of (2) for the training set.

**Back-propagation** is the algorithm to find the derivatives of the cost function with respect to . In this procedure, we use the helper variables , which we initialize as:

We iterate over the layers backwards :

is defined in such a way that

For an sketch of the derivation of these equations, see the Appendix.

### Support Vector Machine

Support Vector Machine is a method for binary classification and it does so by defining a boundary that separates points from different classes. For this model, our labels are either (as opposed to .

In the simple case, where we want to perform a linear separation, it consists in finding a hyperplane that partitions the space in regions, one containing points with y=-1 and the other y=1; also, it tries to maximize the minimum distance from any point to this hyperplane.

Instead of working with the parameters , we’ll have and , so we can represent our hyperplane as . The classification function is give by:

Where is the sign function, that is, if and otherwise.

#### Margin

For each training example , we can define a margin as:

Intuitively, a function margin represents the confidence that point belongs to the class . Given input points, we define the margin of the training set as

(3)

We then want to maximize the margin, so we can write the following optimization problem:

s.t.

The constraint is to enforce (3). The problem with this model is that we can arbitrarily scale and without changing , so the optimal solution is unbounded.

The alternative is to normalize the by dividing by , so it becomes indifferent to scaling.

#### Primal form

It’s possible to show that in order to maximize the margin, we can minimize the following linear programming with quadratic objective function:

s.t. , for

#### Dual form

For non-linear programming, it’s possible to make use of Lagrangian multipliers to obtain the dual model. In particular, the following model obtained using this idea, together with the primal form, satisfy the Karush–Kuhn–Tucker (KKT) conditions, so the value of the optimal solution for both cases is the same.

s.t.

It’s possible to show that the non-zeros entries of correspond to the input points that will define the margin. We call these points **support vectors**.

Moreover, can be obtained by the following equation:

#### Non-linear SVM: Kernels

Kernel is a way to generate features from the training set. For example, we can generate features from the points in the training set by having feature to be the similarity of a point to it. One common similarity function is the Gaussian distance. Given points and , we can compute it by:

### Conclusion

I’ve completed an Artificial Intelligence class in college and previously did the PGM (Probabilistic Graphical Models). One of the main reasons I enrolled for this course was the topic of Support Vector Machines.

Unfortunately, SVM was discussed in a high level and apart from kernels, it was pretty confusing and mixed up with logistic regression. SVM happened to be a very complicated topic and I ended up learning a lot about it with this post, but I still don’t fully understand all the details.

### References

[1] Wikipedia: Back-propagation

[2] Prof. Cosma Shalizi’s notes – Logistic Regression

[4] Prof. Andrew Ng’s notes – Support Vector Machine

[5] Wikipedia: Support Vector Machine

[6] An Idiot’s guide to Support Vector Machines

### Appendix: Derivation of the backpropagation relations

In this appendix we’ll calculate the partial derivative of in relation to a component to get some insight into the back-propagation algorithm. For simplicity, we’ll assume a single input , so we can get rid of the summation.

By the chain rule we have:

Let’s analyze each of the terms:

For the term , we have that or breaking down by elements:

For , it’s easy to see that . For ,

For the term , we have that , thus,

Finally, for the term , we will assume that

If , then , so it’s the derivative is straightforward:

For the intermediate layers, we can find the derivative by writing it in terms of subsequent layers and then applying the chain rule. Inductively, suppose we know (base is ) and we want to find . We can write:

(4)

Given that , we have:

Replacing back in (4):

If we name , we now have

(5)

“Undoing” one of the chain steps, we have

Replacing (5) here,

With that, we can get the following recurrence:

Finally, replacing the terms in the original equation give us