question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation for ConfusionMatrix is thin on details, and error messages not informative

See original GitHub issue

🐛 Bug description

Confusion matrix requires that y_pred and y are to have different dimensions (batch,num_classes) and (batch,), respectively. Getting this wrong results in the following error message, which makes no sense (batch size is 5 here, and both y_pred and y have shape (5,1):

y_pred must have shape (batch_size, num_categories, ...) and y must have shape of (batch_size, ...), but given torch.Size([5, 1]) vs torch.Size([5, 1])

I believe that the torch.Size([5, 1]) vs torch.Size([5, 1]) message should probably read torch.Size([5, 1]) vs torch.Size([5,]) to illustrate the problem.

Separately, it would be good to mention in the ConfusionMatrix docs that one-hot encoded vectors are required, and that this can be implemented using from ignite.utils.to_onehot (I stumbled upon it after realizing I had to one-hot encode, since most people use BCEWithLogitsLoss() anyway, and hence their model predictions are not one-hot encoded).

Environment

  • PyTorch Version (e.g., 1.4): 1.4.0
  • Ignite Version (e.g., 0.3.0): 0.4.2
  • OS (e.g., Linux): MacOS
  • How you installed Ignite (conda, pip, source): pip
  • Python version: 3.8
  • Any other relevant information:

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vfdev-5commented, Nov 14, 2020

@nsinenian thanks for the details. Yes, that’s true that you have binary classification use-case and yes, it definitely makes sense to mention that how to deal with that in the docs !

1reaction
nsineniancommented, Nov 14, 2020

@vfdev-5 The comment was with respect to predictions, not targets. For the predictions, I have (relevant excerpt):

__NUM_CLASSES__ = 2

def binary_one_hot_output_transform(output):
    """ Ouptuts a one-hot encoded vector with 0 or 1 values """
    [y_pred,y] = output
    [y_pred,y] = [torch.sigmoid(y_pred).round().long(),y]    
    return (to_onehot(y_pred,__NUM_CLASSES__), y.long())

valuation_metrics = {
        'confusion_matrix' : ConfusionMatrix(num_classes=__NUM_CLASSES__,output_transform=binary_one_hot_output_transform),
}

evaluator = create_supervised_evaluator(model,metrics=valuation_metrics,output_transform=lambda x,y,y_pred: (y_pred,y))
trainer = create_supervised_trainer(model,optimizer,criterion)

My model output (forward function) is:

    def forward(self,features):
      
        y = self.fc1(features)
        y = self.fcd(y)
        y = self.fc2(F.relu(y))

        return y

For binary classification, I use the BCEWithLogitsLoss() loss function which does not use one-hot encoded vectors (and hence the model output being what it is above). Perhaps my use case is atypical.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ignite ConfusionMatrix Metric Error - PyTorch Forums
is not clear enough as it intends to say the following: ... Documentation for ConfusionMatrix is thin on details, and error messages not...
Read more >
Confusion Matrix error - Stack Overflow
I want to build a predictive model using decision tree classification in R. but I got an error msg (Error: could not find...
Read more >
caret: Classification and Regression Training
Description Misc functions for training and plotting classification and regression models.
Read more >
19 Hidden Sklearn Features You Were Supposed to Learn ...
Learn about 19 Sklearn features you have never seen that are direct and elegant replacements to ... 6 Grievous Sklearn Mistakes That Give...
Read more >
CONFUSION MATRIX (TYPE I & TYPE II ERROR) - LinkedIn
The Evaluation of Model becomes uneven if the Accuracy of Data Set is not Correct. Suppose we Train a Binary Classification (Two Possibilities) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found