In the previous post we've reviewed logistic regression model and designed a simple clustering algorithm based on it. We have managed to get a decent clustering on a subset of MNIST dataset. Yet there were some drawbacks in our approach.

It was hard to describe the model analytically, especially for medians case. As a consequence, reliable expansion of the model to more than two clusters seemed challenging.

#### Why is it hard?

Recall the cost function that we maximized in unsupervised learning:

(1)

Here is a step function, that produces 1 if its argument is greater than threshold and 0 otherwise. The value of the threshold is either mean or median of arguments provided by all the samples.

When deriving weights update rules or other important formulas you often do two things: averaging and differentiation. While the first one does not seem to be a problem, the latter would be an issue. And the reason is that is not smooth. Its derivative in the threshold point is undefined unless we we've done some additional assumptions. Furthermore, location of the threshold point is not fixed and depends on the dataset that you use.

Overall, we can try dealing with difficulties by redefining so that its derivative is a delta-function. The other way is to split expression (1) in two parts: when threshold is exceeded and when it is not. Then derive all the formulas for both cases and combine them. Still, you will need to apply other tricks in order to extend the model to more complex cases.

#### Entropy: measure the chaos

How can we make more friendly to derivatives? The answer is plain: drop the plus sign. 🙂 This can be viewed as an approximation of the step function with a smooth sigmoid . They both share similar features. For arguments with big absolute value sigmoid will act more and more similar to the step function. So let's examine our new cost function:

(2)

It turns out that this is a rather well-known quantity in physics and information theory: entropy. Up to a sign. There are various definitions of entropy depending on the field of application. On a high level, entropy measures how messy the system is. So, high values mean that there is a lot of chaos and low values mean that everything is in order. When searching for weights maximizing (2) function, we, in fact, minimize entropy. Thus we force the algorithm to find clustering that with as little clutter as possible.

Let's check the difinition of entropy from information theory:

(3)

Here describes a set of all possible system states (e.g. set of feature values for a sample + a label) and is a probability to observe a specific state. The value of provides amount of information gained from knowledge that the system is in state. If probabilities are close to extreme values, then entropy gets near zero values. The reason is that with such probabilities you are already almost certain what state the system is in. On the other hand, for probabilities around 50% you get maximum values of , because any state would be surprise for you.

Probabilities are given by a model. By training a model minimizing the entropy you are virtually asking to make a clusterization with the least achievable surprise.

To rewrite expression (3) to form (2) we need to substitute state with labels and features

(4)

Note that is a set of all possible features-label combinations. We limited our model to produce only two clusters, so for every features instance there are two possible combinations: and . We have limited amount of train samples, so exact calculation of is not always feasible. To resolve this problem we can approximate the entropy by summing over all train set. If the dataset had been properly sampled, we would get decent results. Updated formula for the entropy yields

(5)

Expression (5) uses joint features-labels probabilities . On the other hand, our models provide conditional probabilities . Having the distribution for features known, we can use Bayes theorem and get the joint distribution. Unfortunately, often you don't know the marginal distribution. You could get it by bootstraping or simply assuming that all samples are equally likely to appear. Still, there is another, more elegant way.

It would be cool if we substitute in (5) with . The question is what we get then?

(6)

This is called conditional entropy. It shows how much information is needed to assign a cluster label to a sample with known features . A good thing is that and are related

(7)

Also, depends only on features. So for any model it would be the same. As a consequence, model minimizing conditional entropy also minimizes entropy . Using the fact that and substituting with a model estimate in (6) we arrive to the cost function (2) with negative sign.

Now that we know the nature of the model, we can analyze its pros and cons. Good things are that it has a clear physical sense, it is tractable for analysis and it is fairly simple. Nevertheless, it has some issues too. And the main one is the presence of local minimum that produces absolutely useless results. Suppose that all weights of the model are near zero. Then output of the model doesn't care what data is passed in. It will be always the same value determined by bias. Bad and obviously not desired situation. With bias magnitude high enough you get almost perfect 1 or 0 probabilities. That yields a very low value of conditional entropy and thus would be accepted by training routine as a good solution.

Usually, it is enough to initialize weights with small random values before training to feel relatively safe from those local minimums. The model works fine then. Or alternatively we can modify the cost function by adding anti-regularization term. To be honest, I even planned to use this (6) model as a basis and name the post "Clustering via entropy minimization". However, after doing some search I've found a paper describing this approach in more details and one more work with improvement of the algorithm (Grandvalet 2004 and Gomes 2010).

One more remark before going to the next section. If you calculate derivatives , you will get weights update rule

(8)

Here denotes the conditional probability . Similarly to the previous part, weights update rule gets form of sum over samples weighted by gain . In this case gain is

(9)

*Figure 1. Sample gain for entropy minimization model.*

Gain shares similar features with models from previous part: it becomes close to zero when travels to extreme values. Also note zero at . Interpretation is following: samples with circa 50% probability estimate are near the decision boundary and could be either class, so it is much harder to extract information from them. The farther the sample from the decision boundary the more confident the model is in its class. During the learning phase the algorithm changes weights so that on average samples become more and more distant from the decision boundary. This is similar to what SVM does. Also there is a turn point. If a model is around 95% or more certain in a sample's class, utility of that sample decreases. It prevents large grow of weights acting as a regularizer. So, effectively, algorithm uses only part of samples that lie in 60-95% region of probabilities where it is fairly confident in samples class.

#### Mutual information

What is bad with all samples being in one class? Having everything in one basket makes introduction of classes useless because there is no information gained by knowing the class. In other words, class labels are then completely unrelated to features. So, what we really want is not to have everything in order, but to have labels that are closely related to sample features. In math language we would like to maximize mutual information, i.e. mutual dependence between labels and features. In terms of formulas, it is described as a distance between joint features-labels distribution and product of marginals

(10)

Having means that which essentially represents independence of features and labels. So, we want to maximize , i.e. to put as much information from features into labels as possible. One could think of this as of a very lossy compression.

Expression (10) operates with three probabilities: , and . Yet out model provides only conditional probability . Luckily, mutual information is closely related to entropy:

(11)

There is a great image on Wikipedia describing this relation.

*Figure 2. Venn diagram showing relation between entropies and mutual information.*

The new model is pretty the same that we already had. The difference is in the first term : entropy of the labels. Maximization of (11) means minimization of (6) while keeping labels entropy high enough. Note that labels in this case are considered alone from the features

(12)

Factor occurs because we are calculating entropy over all labels in the dataset. In this case all of them act as independent subsystems and thus their entropy is additive. The simplest way to estimate is to assign labels to all the samples in the dataset and then calculate frequencies for ones and zeros.

Suppose that all elements are classified in the same cluster. This leads to equal to either 1 or 0. Thus, becomes zero and decreases the mutual information! Seems like the problem is solved.

Let's move on and derive weights update rule. To do this we need to find gradients . We already have an expression (8) for conditional entropy derivatives. Marginal distribution is not related to features , still we can connect it to the conditional .

(13)

Where stands for averaging over all possible samples. To estimate the expected value we take average over the dataset of samples.

(14)

Combining all together we get an expression for the entropy

(15)

and its derivatives

(16)

We can rewrite the last equation in terms of sums over samples

(17)

The gradient (17) is very similar to the one for conditional entropy . Combining both of them we get the following weights update rule

(18)

Again, the rule (18) is in form of weighted sum over dataset. Expression for gain

(19)

It is close to (9). When expressions (19) and (9) coincide.

*Figure 3. Sample gain for mutual information maximization model.*

The gain function is always zero near extreme points 0 and 1. Also it has one zero in between. The location of the zero is determined by mean value . Interpretation for this is same as for entropy case: points too close or too far from decision boundary provide no new information to the learning algorithm. Also note how the curve form changes favoring more the samples from larger cluster. It gives a hint that this method won't work good if dataset is too skewed, i.e. there are to few samples for one of the classes.

We have cost function (11) and weights update rule (18). With regard to theory we are done. Now it is time to code the equations.

#### Implementation

We'll start with the simplest parts: functions for entropy and mutual information calculation.

function S = get_entropy(p)

Note that mean is used instead of sum. It works as a normalization, moving numbers in the same scale independent from number of samples in the dataset.

Same normalization is used here. Otherwise we had to multiply Ht by number of samples.

The next step is gradients estimation.

function [s, gW] = get_grads(W, data) s = get_probs(W, data); l_expr(isnan(l_expr) | l_expr == 0 | isinf(l_expr)) = 1; gW = gW.';

Gradient in unsupervised learning should not depend on labels, so we pass only weights and samples to *get_grads* function. Assignment in line 5 aims to prevent infinite or NaN values of gradients. Analytically we can show that in those extreme cases *sfun* will take zero values. In spite of that during the calculation phase MATLAB does not take limits or take into account functions order of growth, so line 5 is introduced to deal with this issue.

The rest of the code is very similar to the one from the first part. Here is a GitHub link to a file that takes care of initialization, train loop and tests. Key differences from what we've already written in part 1 is in change of the cost function (*get_mint* instead of *get_logll*) and absence of labels in gradients calculation.

#### Preprocessing and initialization

There are also a few minor tweaks. We will review two of them.

The first one is data preprocessing. You can not be 100% sure in your dataset. It is possible that there are duplicate features or NaNs or useless ones etc. That's why it is important to cleanse your inputs before passing them to the learning algorithm.

function [data, keep_inds, means, stds] = preprocess_data(data, keep_inds, means, stds) keep_inds = stds>0; % no variation => no usefull information means = means(keep_inds); stds = stds(keep_inds); end data = data(keep_inds, :); % z-scoring data = bsxfun(@minus, data, means); data = bsxfun(@times, data, 1./stds);

This is one example of a preprocessing function. It removes constant features, i.e. ones that have the same value across the whole dataset. It could reduce the dimensionality of your dataset and thus speedup the training. After useless entries are removed z-score is applied. Z-scoring is a normaliztion routine that centres features and scales them to have unit variance. Having all the features in the same scale is generally a good idea. Features are combined as a linear combintation in sigmoid so vast discrepencies in scale could lead to shadowing of small-scale features and imprecise models learned. In fact akin ideas are used in MATLAB Neural Networks (see documentation for removeconstantrows and mapminnax functions).

Do you remember how we initialized weights in our early models? We used small random numbers. For supervised models that worked good. Yet, for clustering purposes this approach produced flipping labels issue: initialization determined which class would be marked with which label. This issue is not critical: you can switch labels after reviewing the model trained. Often it is not even important what actual labels would be. Still it would be good to know ahead that for example zero from MNIST dataset would be marked by close to zero outputs of the model. To achieve this we can train a supervised model on a tiny bit of labeled dataset. If you don't have labels, you can add them manually. For subset of MNIST that we used in previous part it is enough to have just as little as 2-3 labeled samples per class. Still leaving thousands of samples unlabeled. Initialization routine is shown above.

function W = init_weights(data, labels) % Argument labels is a Nx2 matrix. % The first column represents labels (e.g. zeros or ones). % The second column: indecies of samples from data corresponding to the labels. % So, if first two samples have "0" label and 10th and 13th have "1" label, then % labels = [0, 1; 0, 2; 1, 10; 1, 13]; else X = data(:, labels(:, 2)); t = labels(:, 1)'; W = train_lr(X, t); end

The supervised model is trained using *train_lr* function that was introduced in part 1. The curious thing is that we've used unlabeled data, added a few labels, passed that to our routines and as a result obtained a semisupervised algorithm for clustering that produces stable labels for clusters. In other words, two runs of training would likely end with models assigning same labels to most of data. Thus, labels flipping issue is resolved!

#### End of the post, at last

Wow! It was a long road, but we've made it till the end. Let's summarize our achievements. We've reviewed logistic regression model and created algorithms for classification and clustering based on this model. Our models expect that there are only two classes/clusters in data. It makes models more simple for understanding and coding, yet limits their power. Using some tricks you can solve multiclass problems even with this dichotomous toolkit. Two of many possible ways: hierarchy of models and one-vs-all approach.

After introducing baseline model for unsupervised learning, we highlighted its drawbacks and eliminated them by changing cost function and adding smart supervised initialization of weights. Most of the code mentioned in this post can be found on GitHub.

At this point you have a decent clustering framework and understanding how and why it works. Good luck in your experiments! 😉

## One thought on “Clustering via mutual information maximization. Part 2”