How to deal with unbalanced datasets
I'm currently using the AlexNet network to train my models. Some of my datasets are quite unbalanced (1:20 -> ratio positives vs negatives) and I realized that the error rate or the positive samples is a lot higher than for the negative samples: 25% vs 1% !
Is there a parameter in CNTK that I can change so that the model can be trained by giving more weight to the positive samples ? Or should I remove most negative samples to have a balanced datatset ? I looked at the wiki&issues but found nothing about that kind of problem.
Yes with caveats, and you will need BrainScript if you are not using that yet. Then you can directly include weights into the criterion:
ce1 = ReduceLogSum (z) - TransposeTimes (labels, z) # same as CrossEntropyWithSoftmax (labels, z)
ce = ce1.* weight # weighted
where weight is a per-sample factor that depends on the labels, e.g. (warning: untested Outlook code):
weightTable = ConstantFromString ('1 5') # assuming 2 classes
weight = weightTable * labels # look up weightTable value for class index
Here, weightTable is a constant row vector (1, 5) of weights applied to each class. It gets, by means of the matrix product, indexed by the label index. The gradient will get the same factor, which I believe you have in mind. However, I have no idea how this will affect convergence.
The corresponding manual expression for error counting is this:
errs1 = BS.Constants.One - TransposeTimes (labels, Hardmax (z)) # same as ErrorPrediction()
errs = errs1 .* weight
but this shows a drawback of the above method: You do not get a reweighting of the denominator of the value that is printed. Each error in the second class is now counted as 5 errors, but not as 5 occurrences. This is, I think, mostly OK for the training criterion, but will be confusing for error rates, so please be aware.
An alternative approach could be to change the prior of the model after the fact, instead of the training weight. This is a common technique in speech recognition, where the network's state posteriors are converted into a "scaled likelihood" by dividing it by the prior probability (and then multiplying it by a different prior arising from the more complicated HMM structure). This will not change the way you train the model, so the model will be sub-optimal w.r.t. your use case, but it has worked very well in speech, and may circumvent problems with convergence of the other method.
The easiest way, of course, would be to modify your training set by duplicating samples. That should remove all hackiness. It would also open the possibility of combining this with random perturbations of the samples.
I have not tried any of the above, please do let me know if you find that any of them do or do not work. Thanks!
Ok, I'm not very comfortable with BrainScript and in my case I am training many models with the same network configuration and my training data changes often. So this would force me to hardcode different weights for training.
As you say, duplicating the data is easiest. The only drawback is that it will slow down the training but I can train a model in about two hours so it is not that bad.
I'll let you know if it solves my issue (I don't see why it wouldn't) and if I finally try your first suggestions.
You can also load the initial weights of the weightTable from a file that you store with each data set.
But it seems the first thing I would try is the scaled-likelihood approach where you compute the prior distribution from the data, and subtract their log before the Softmax(), something like
P = Softmax (z - logPrior, tag="output")
where z is the output of the top hidden layer, and logPrior can be estimated like this automatically from your training set with this:
logPrior = Log (Mean (labels))
This will not give you weighting of training samples, but at least the correct priors. Let us hope that the priors are the dominant problem in this case.