GithubHelp home page GithubHelp logo

The score computed by `multiclass_f1_score` for binary classification is wrong. It is not f1 score but accuracy. about torcheval HOT 2 CLOSED

LittletreeZou avatar LittletreeZou commented on August 28, 2024
The score computed by `multiclass_f1_score` for binary classification is wrong. It is not f1 score but accuracy.

from torcheval.

Comments (2)

MattBrth avatar MattBrth commented on August 28, 2024

I have the same issue ! Please fix this

from torcheval.

bobakfb avatar bobakfb commented on August 28, 2024

Hi @LittletreeZou and @MattBrth ! Thank you both for contributing to torcheval!

I think there is a misunderstanding of multi-class metrics going on here! In most cases Binary-X and Multiclass-X with 2 classes are not the same thing. In the binary setting there is only 1 class, and each example is either in or not in that class. In the multiclass setting, there are two classes and you compute the f1 score for each (first calling 0 a positive label and computing true positives, false positive, etc... against the label actual = 0, then calling 1 a positive label and doing the same).

In other words, what you're getting is the average of two f1 scores here.

Note: if you pass average=None to multiclass_f1_score, you will recover the binary result for class 1 in one of the outputs

>>> metric = MulticlassF1Score(num_classes=2, average=None)
>>> metric.update(pred, actual)
>>> print(metric.compute())
tensor([0.6256, **0.5820**])

Also note, this is standard behavior, e.g. following sklearn

>>> actual = torch.repeat_interleave(torch.tensor([1, 0]), repeats=torch.tensor([100, 100]))
>>> pred = torch.repeat_interleave(torch.tensor([1, 0, 1, 0]), repeats=torch.tensor([55, 45, 34, 66]))
>>> input_tensors = [torch.argmax(t, dim=1) if t.ndim == 2 else t for t in pred]
>>> target_tensors = list(actual)
>>> target_np = torch.stack(target_tensors).flatten().numpy()
>>> input_np = torch.stack(input_tensors).flatten().numpy()
>>> f1_score(target_np, input_np, average='micro')
0.605

from torcheval.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.