## Introduction

There’re 3 major methods on working with classification:

- Discriminant Function
- Probabilistic Generative Model
- Probabilistic Discriminative Model

The first method is brute-force method which is what neural networks uses. It consist of least square classification, Fisher’s linear discriminant and perceptron algorithm.

Our focus will be on the following 2 models which involves a probabilistic viewpoint.

## Probabilistic Generative Model

The main goal of this model is to model the density function entirely from the training data points given. By entirely, we mean where is the class number and it’s the joint PDF of class and data point . For the binary case and from bayes theorem we know that . It’ll be which is known as * sigmoid function* for the binary case where . For multi-class problems, it’ll be which is also known as

*.*

**softmax function**As always, we’ll try to solve for optimal parameters given by the posterior probability . We’ll start with

For the multi-class case, we are trying to solve for:

Where by solving for the maximum likelihood for a Gaussian Distribution, we obtain:

For the maximum likelihood solution for a Gaussian Distribution:

*where*

## Probabilistic Discriminative Model

For this model, we are trying to reach the posterior probability without going through all the steps mentioned above (obtaining the * maximum likelihood *and

*) to get there. Instead, we are trying to model the posterior probability directly by finding the optimal . Though we are simplifying the procedure in coming up with a solution to this problem, discriminative models ends up with a better solution than generative models most of the time. The main reason is that there are fewer predictive parameters needed and due to the limitation of computational resources, we are able to come up with a much non-linear model compare to the generative model.*

**prior**Our goal in this model is to optimize for in posterior PDF below: (Logistic Regression)

is a basis function of (to increase the non-linearity)

For a binary classification example, our goal is to minimize the **cross-entropy error function **given below:

The that we are trying to optimize is responsible for the “placement” of the sigmoid function given on the diagram above. It’s also called the decision boundary.

Due to the non-linearity of the logistic sigmoid funciton, we are unable to solve using a closed form solution. Thus, there’re 2 approaches in dealing with this problem.

#### Sequential Learning:

So the term here is basically the learning rate of the discriminative model. The only parameter that is user-defined is here.

#### Newton-Raphson method:

Sequential learning method is actually a simplified version of Newton-Raphson method. The equation used for Newton-Raphson method is shown below:

*Hessian Matrix*

We can see that has been changed to . Current implementation for the training of a model is done using the former method because the computation of the Hessian Matrix is relatively expensive and slows down the time needed for the training of the model. Sequential learning is also called Gradient Descent and it is the most commonly used method in the training of neural networks. The parameter and decides the learning rate of the algorithm thus a large value corresponds to the speed on finding the global minima of the model. There’s a catch here, where a learning rate that’s too large might cause the learning algorithm to oscillate and we might never reach the global minima of our model. We’ll be using the Newton-Raphson method for the rest of the implementation, thus a detailed elaboration on this method will be given below.

Gradient of cross-entropy error function

Hessian Matrix

R: diagonal matrix with

Since , it follows that for an arbitrary vector . The error function is a convex function of and there is a unique minimum.

We can substitute all of the equations above into and obtain (after some algebraic manipulation):

*where *

For the multi-class case:

*where*

From that, we’re able to get the cross-entropy error function:

There’re a few things that we’ll have to pay attention to. One of them is the that is in the Hessian Matrix because now we have multiple class. We would end up with multiple where we have now:

,

## Implementation

### Introduction

We will be working with images from Database of faces (AT&T Laboratories Cambridge) which are of size 30×30. We’ll apply both Generative and Discrimininative models to model our data.

### Reading image using Python

We will be using Python 2.7 for the development of this algorithm. To work with the data given, we’ll have to first read an image file (.bmp) using Python. As from here we will need the library “pillow” which enables us to read image files. The next step would be converting read image files into a single numpy array.

The following code will convert the given image into a numpy array:

img = Image.open("Data_Train/Class1/faceTrain1_755.bmp") data = np.asarray( img, dtype="int32" )

You can also read all image from a directory iteratively using the following method:

imageFolderPath = 'Data_Train/Class1' imagePath = glob.glob(imageFolderPath+'/*.bmp') train_size = 100 im_array_1 = np.array( [np.array(Image.open(imagePath[i])) for i in range(train_size)] )

The library glob is used here for finding pathname matching a specified pattern according to rules used by Unix shell.

### Dimension Reduciton using PCA

To ease on the computation power needed to process the images, we will have do reduce the dimension of the given dataset. Thus Principal Component Analysis (PCA) is used for dimensionality reduction.

The implementation for PCA can be refered here

PCA must only be done for training data to ensure that we have a general model. The **mean** of the training data, the **eigenvector** used for projection and the corresponding **eigenvalue** will be reused later on during testing, so it has to be stored for later use. Here’s a detailed explaination on how PCA is implemented when there’re both training and test dataset. When a test dataset comes in, it is first subtracted by the mean, and then it’s projected on the basis (eigenvectors) and the it’s whiten by dividing by the corresponding eigenvalue.

### Probabilistic Generative Model

By using this model, we are able to model the data completely using Gaussian Distributions. An illustration of a scatter plot + Contour plot and scatter plot + surface plot is shown below:

#### Scatter Plot + Contour Plot

#### Scatter Plot + Surface Plot

It can be observed that red (group 2) and green (group 3) data points are closely clustered together which is a problem when we’re trying to classify them.

We got an accuracy of 81.3%

### Probability Discriminative Model

For this model, we try to solve for the weights directly, so we are unable to come up with the mean and variance of the gaussians that models our data. Though, we are still able to plot the decision boundary for this model. There are two ways to implement this model, one is through Newton-Raphson iterative optimization, the other is through a fixed learning rate by changing the Hessian Matrix here to a fixed value.

#### Newton-Raphson

It can be observed that the boundaries are linear.

Throughout the design of this method, we are able to observe the error function and check for its convergence. Since it’s a convex function, it’ll fall to a global minima.

The final log probability error we got here is: 964.4353

The accuracy we got here is 85%

#### Fixed Learning Rate

The learning rate we used here is 0.002. Finding a good learning rate is really important for this method. By using a bad learning rate, this algorithm might oscillate and will never converge. Even convergence itself takes much longer than the Newton-Raphson method because the slope of the learning rate is now fixed and not optimized by the Hessian Matrix. In the end, it will converge to the same global minima as the previous method if the initial conditions are the same.

### Unbalanced Data

We would now want to try how unbalanced training data affects our performance. Here, we will try to reduce the amount of data points for the 3rd training dataset from 900 to 300 and see how it affects the performance.

#### Probabilistic Generative Model

It can be observed that dataset 2 and 3 (red and green) are closely blended together. This will of course affect the overall performance of this model. The accuracy we got here is 77.3% which is as expected lower.

#### Probability Discriminative Model

The final log probability error we got here is 640.7852 which is lower than before. This is probably caused by the closely clustered dataset 2 and 3 (red and green) where both are assigned a high probability for each posterior classes.

The accuracy we got here is: 76% which is obvious because we didn’t come up with an optimal model.

## References

http://sites.stat.psu.edu/~jiali/course/stat597e/notes2/logit.pdf