-
-
Notifications
You must be signed in to change notification settings - Fork 8
BREAKING_CHANGE: change format for binary classification heads #385
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
Conversation
|
@tdhock let me know if this is what you had in mind :) |
|
The code seems to work as I would expect, thanks! remotes::install_github("mlr-org/mlr3torch@c03d61a18e9785e2dbb5b20e2b6dada74a9b58b8")
stask <- mlr3::tsk("sonar")
po_list <- list(
mlr3torch::PipeOpTorchIngressNumeric$new(),
mlr3torch::nn("head"),
mlr3pipelines::po(
"torch_loss",
loss = torch::nn_bce_with_logits_loss),
mlr3pipelines::po("torch_optimizer"),
mlr3pipelines::po(
"torch_model_classif",
epochs = 1,
batch_size = 3,
predict_type="prob"))
graph <- Reduce(mlr3pipelines::concat_graphs, po_list)
glrn <- mlr3::as_learner(graph)
glrn$train(stask)
glrn$predict(stask)And I got this result: > glrn$predict(stask)
<PredictionClassif> for 208 observations:
row_ids truth response prob.M prob.R
1 R M 0.5324456 0.4675544
2 R M 0.5387607 0.4612393
3 R M 0.5339928 0.4660072
--- --- --- --- ---
206 M M 0.5342165 0.4657835
207 M M 0.5279804 0.4720196
208 M M 0.5211176 0.4788824 |
|
overall looks good! |
|
@tdhock thanks for the review! |
| #' * binary classification: The `factor` target variable of a [`TaskClassif`][mlr3::TaskClassif] is encoded as a | ||
| #' [`torch_float`][torch::torch_float] with shape `(batch_size, 1)` where the positive class is `1` and the negative | ||
| #' class is `0`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, but what is the positive and negative class at the R-level?
In R it is a factor with two levels. Is the first factor level always considered the positive class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mlr3 binary classification tasks have a field $positive. Will add that to the docs!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yes, it is also ensured that the positive class is the first level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks
| #' | ||
| #' Furthermore, the target encoding is expected to be as follows: | ||
| #' * regression: The `numeric` target variable of a [`TaskRegr`][mlr3::TaskRegr] is encoded as a | ||
| #' [`torch_float`][torch::torch_float] with shape `c(batch_size, 1)`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great!
Maybe this is more of a question/issue for mlr3 rather than mlr3torch, but is it possible to do multi-task regression? (more than one output to predict?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not yet, but we could implement a Task where the target is a lazy_tensor, which would allow this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
setting target to lazy_tensor would be specific to torch, right? other non-torch learners can't use lazy_tensors, right?
it would be better if there would be support for any learners (for example glmnet or rpart) and not just torch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah that’s true, if you want this this should be a feature request in mlr3.
Resolves #374
@tdhock With this PR,
mlr3torchwill now expect an output of shape(batch_size, 1)for binary classification problems.t_loss("cross_entropy")will automatically select the appropriate loss depending on the number of classes.In the example below, you see it in action.
We train the same learner once on a binary classification problem (iris subset to two classes) and on a multi-class classification problem (iris).
Depending on the task, the correct output dimension will be set, the targets will be loaded correctly during training, and the correct loss function is instantiated. This is now also respected by all
mlr3torchlearners.Created on 2025-04-16 with reprex v2.1.1
Note that after this is is not possible to have a neural network with output dim
(batch_size, 2)for binary classification. I think this is okay, as(batch_size, 1)is clearly better.Created on 2025-04-16 with reprex v2.1.1