torch/nn — pull request #83 by yossibiton (2014-10-08 08:44:30+00:00)
Title: Adding classes weights to ClassNLLCriterion

(Sorry for my mistake but this request should replace my previous one. Now i put my changes in specific branch for that topic - balanceNLL)

Adding class weights is relevant when you have unbalanced training set, adding custom weights can help in these cases.

Example for input weights -

--- compute frequency for each class
class_freqs = torch.Tensor(nLabels):fill(0)
for iImage = 1,trainData:size() do
label = trainData.labels[iImage]
class_freqs[label] = class_freqs[label] + 1
end

--- each class weight is inverse proportional to the number of samples in the class
class_weights = torch.Tensor(nLabels):fill(0)
for iLabel = 1,nLabels do
class_weights[iLabel] = 1 / class_freqs[iLabel]
end
class_weights = class_weights / class_weights:sum()