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

Quantile regression #845

Closed
StatPal opened this issue Jun 5, 2022 · 2 comments
Closed

Quantile regression #845

StatPal opened this issue Jun 5, 2022 · 2 comments

Comments

@StatPal
Copy link

StatPal commented Jun 5, 2022

Is there any immediate way to incorporate Quantile Loss (and consequently Quantile regression) in torch with R?
I have found something in PyTorch here: pytorch/pytorch#38035.

This issue is opened as a feature request, but I can try to contribute if it is not already done.
Thanks.

@dfalbel
Copy link
Member

dfalbel commented Jun 6, 2022

HI @StatPal !

There's no built-in quantile loss function in torch but you can implement one with something like:

quantile_loss <- torch::nn_module(
  initialize = function(quantiles) {
    self$quantiles <- torch::torch_tensor(sort(quantiles))$unsqueeze(1)$unsqueeze(1)
  },
  forward = function(y_pred, y_true) {
    other <- torch::torch_zeros_like(y_pred)
    error <- y_true - y_pred

    low_res <- torch::torch_max(error, other = other)
    up_res <- torch::torch_max(-error, other = other)

    quantiles <- self$quantiles$to(device = y_true$device)
    torch::torch_mean(quantiles * low_res + (1 - quantiles) * up_res)
  }
)

For instance this is what's used here

@StatPal
Copy link
Author

StatPal commented Jun 7, 2022

Thank you very much for your suggestion.

@StatPal StatPal closed this as completed Jun 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants