Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add precision classification metric #2293

Merged
merged 31 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c73438f
Implement confusion matrix and precision, first draft
Aug 21, 2024
63f4a1d
Implement confusion matrix
Sep 9, 2024
b9d71b6
format :D
Sep 9, 2024
eac29aa
add agg type to cm, reformat debug representation add testing.
Sep 20, 2024
59db68b
formating and tiny refactor
Sep 21, 2024
4261bd8
add ClassificationMetric trait, rename variables and types, move test…
Sep 21, 2024
5431a2f
change unwrap to expect
Sep 21, 2024
fd2e585
update book
Sep 21, 2024
56965e8
remove unused code
Sep 22, 2024
419438a
changes to make reusing code easier
Sep 22, 2024
dfac847
format :D
Sep 22, 2024
ea4b29c
change to static data tests
Sep 24, 2024
e23aa7b
remove classification metric trait, add auxiliary code for classific…
Oct 14, 2024
60a246b
move classification objects to classification.rs, use rstest, remove …
Oct 21, 2024
c145531
review docstring, add top_k for multiclass tasks.
Oct 23, 2024
0c984c4
move class averaging and metric computation to metric implementation,…
Oct 25, 2024
b0a2939
change struct and var names
Oct 25, 2024
f18e321
Merge branch 'main' into add-to-metrics
Oct 26, 2024
386802c
rename params, enforce nonzero for top_k param, optimize one_hot for …
Oct 30, 2024
b525527
add adaptor por classification input, correct one hot function
Nov 1, 2024
ff7611a
define default for ClassReduction, derive new for Precision metric wi…
Nov 8, 2024
4cbcff2
Merge branch 'main' into add-to-metrics
Nov 8, 2024
eeab0d3
expose PrecisionMetric, change metric initialization
Nov 8, 2024
aea207f
check one_hot input tensor has more than 1 classes and correct it's i…
Nov 16, 2024
410f273
Merge branch 'main' into add-to-metrics
Nov 16, 2024
746fa9d
implement adaptor for MultilabelClassificationOutput and Classificati…
Nov 16, 2024
7428b86
change with_top_k to take usize
Nov 18, 2024
58e1902
Merge branch 'main' into add-to-metrics
Nov 18, 2024
d598f00
Add precision config for binary, multiclass and multilabel
laggui Nov 18, 2024
1542ee9
Fix dummy_classification_input
laggui Nov 18, 2024
03ebe1d
make PrecisionMetric public
Nov 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions burn-book/src/building-blocks/metric.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ throughout the training process. We currently offer a restricted range of metric
| ---------------- | ------------------------------------------------------- |
| Accuracy | Calculate the accuracy in percentage |
| TopKAccuracy | Calculate the top-k accuracy in percentage |
| Precision | Calculate precision in percentage |
| AUROC | Calculate the area under curve of ROC in percentage |
| Loss | Output the loss used for the backward pass |
| CPU Temperature | Fetch the temperature of CPUs |
Expand Down
5 changes: 5 additions & 0 deletions crates/burn-tensor/src/tensor/api/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,11 @@ impl TensorCheck {
"Can't create a one hot tensor from ({index_tensor:?}) containing indexes greater or equal to the number of classes ({num_classes})",
)),
);
} else if num_classes <= 1 {
check = check.register(
"One Hot",
TensorError::new("Can't create a one hot tensor with less then 2 classes"),
)
}
check
}
Expand Down
1 change: 1 addition & 0 deletions crates/burn-train/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ ratatui = { workspace = true, optional = true, features = ["all-widgets", "cross
derive-new = { workspace = true }
serde = { workspace = true, features = ["std", "derive"] }
async-channel = { workspace = true }
rstest.workspace = true

[dev-dependencies]
burn-ndarray = { path = "../burn-ndarray", version = "0.16.0" }
Expand Down
25 changes: 24 additions & 1 deletion crates/burn-train/src/learner/classification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::metric::{AccuracyInput, Adaptor, HammingScoreInput, LossInput};
use crate::metric::{AccuracyInput, Adaptor, HammingScoreInput, LossInput, PrecisionInput};
use burn_core::tensor::backend::Backend;
use burn_core::tensor::{Int, Tensor};

Expand Down Expand Up @@ -27,6 +27,23 @@ impl<B: Backend> Adaptor<LossInput<B>> for ClassificationOutput<B> {
}
}

impl<B: Backend> Adaptor<PrecisionInput<B>> for ClassificationOutput<B> {
fn adapt(&self) -> PrecisionInput<B> {
let [_, num_classes] = self.output.dims();
if num_classes > 1 {
PrecisionInput::new(
self.output.clone(),
self.targets.clone().one_hot(num_classes).bool(),
)
} else {
PrecisionInput::new(
self.output.clone(),
self.targets.clone().unsqueeze_dim(1).bool(),
)
}
Comment on lines +39 to +43
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this also transform the targets with .one_hot(...) but force the num classes to 2 (assuming binary classification)?

Copy link
Contributor Author

@tsanona tsanona Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, I think that would be covered by the case above since then num_classes == 2. This, I think, is slightly different from binary classification ( I'm thinking for example classifying binary: spam email vs not spam email and multiclass with 2 labels: trees or bushes).Finally, I don't think it would work as output and targets should have the same shape.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spam vs not spam (i.e., positive vs negative) is still considered binary classification. I think what you are talking about is just that one uses sigmoid to model the positive output (so there is only one score) vs softmax where you have a score for each. But the targets are still represented as 0 or 1.

I'm not sure what self.targets.clone().unsqueeze_dim(1).bool() is supposed to represent 🤔 I could be missing something though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, at the end of the day they all are the same thing. The point I was trying to make is that in binary classification tasks I expect the output of the model to be (batch_size x 1) while in multi class I would expect (batch_size x 2), thus the targets for the first would be transformed from (batch_size) -> (batch_size, 1) and the second (batch_size) -> (batch_size, 2) so they match the shapes of the outputs. Still this is a preference, if you think it doesn't make sense we can just assert that the second dim of output is not less than 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh ok nvm I thought this would lead to some issues because the targets are expected to be one-hot encoded, but in reality this is not entirely true for the binary case for a single scalar output and target. The operations performed will still be valid.

}
}

/// Multi-label classification output adapted for multiple metrics.
#[derive(new)]
pub struct MultiLabelClassificationOutput<B: Backend> {
Expand All @@ -51,3 +68,9 @@ impl<B: Backend> Adaptor<LossInput<B>> for MultiLabelClassificationOutput<B> {
LossInput::new(self.loss.clone())
}
}

impl<B: Backend> Adaptor<PrecisionInput<B>> for MultiLabelClassificationOutput<B> {
fn adapt(&self) -> PrecisionInput<B> {
PrecisionInput::new(self.output.clone(), self.targets.clone().bool())
}
}
75 changes: 75 additions & 0 deletions crates/burn-train/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,78 @@ pub use learner::*;

#[cfg(test)]
pub(crate) type TestBackend = burn_ndarray::NdArray<f32>;

#[cfg(test)]
pub(crate) mod tests {
use crate::TestBackend;
use burn_core::{prelude::Tensor, tensor::Bool};
use std::default::Default;

/// Probability of tp before adding errors
pub const THRESHOLD: f64 = 0.5;

#[derive(Debug)]
pub enum ClassificationType {
Binary,
Multiclass,
Multilabel,
}

/// Sample x Class shaped matrix for use in
/// classification metrics testing
pub fn dummy_classification_input(
classification_type: &ClassificationType,
) -> (Tensor<TestBackend, 2>, Tensor<TestBackend, 2, Bool>) {
match classification_type {
ClassificationType::Binary => {
(
Tensor::from_data(
[[0.3], [0.2], [0.7], [0.1], [0.55]],
//[[0], [0], [1], [0], [1]] with threshold=0.5
&Default::default(),
),
Tensor::from_data([[0], [1], [0], [0], [1]], &Default::default()),
)
}
ClassificationType::Multiclass => {
(
Tensor::from_data(
[
[0.2, 0.8, 0.0],
[0.3, 0.6, 0.1],
[0.7, 0.25, 0.05],
[0.1, 0.15, 0.8],
[0.9, 0.03, 0.07],
],
//[[0, 1, 0], [0, 1, 0], [1, 0, 0], [0, 0, 1], [1, 0, 0]] with top_k=1
//[[1, 1, 0], [1, 1, 0], [1, 1, 0], [0, 1, 1], [1, 0, 1]] with top_k=2
&Default::default(),
),
Tensor::from_data(
[[0, 1, 0], [1, 0, 0], [0, 0, 1], [0, 0, 1], [1, 0, 0]],
&Default::default(),
),
)
}
ClassificationType::Multilabel => {
(
Tensor::from_data(
[
[0.1, 0.7, 0.6],
[0.3, 0.9, 0.05],
[0.8, 0.9, 0.4],
[0.7, 0.5, 0.9],
[1.0, 0.3, 0.2],
],
//[[0, 1, 1], [0, 1, 0], [1, 1, 0], [1, 0, 1], [1, 0, 0]] with threshold=0.5
&Default::default(),
),
Tensor::from_data(
[[1, 1, 0], [1, 0, 1], [1, 1, 1], [0, 0, 1], [1, 0, 0]],
&Default::default(),
),
)
}
}
}
}
9 changes: 9 additions & 0 deletions crates/burn-train/src/metric/classification.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// The reduction strategy for classification metrics.
#[derive(Copy, Clone, Default)]
pub enum ClassReduction {
/// Computes the statistics over all classes before averaging
Micro,
/// Computes the statistics independently for each class before averaging
#[default]
Macro,
}
Loading