[BUG] Fix quantile support in Samformer - #2357
Conversation
There was a problem hiding this comment.
I am confused - I think your initial claim (the models do not support quantiles) is incorrect.
If a quantile loss is used at the last/output layer, then the predictions indeed become quantile predictions.
This is because the argmin of a quantile loss expectation (true distribution vs variable in the armin) is the (generative) quantile. Similarly, if you take multiple quantiles in the quantile loss, you get multiple quantiles in the forecast.
All that needs to be done for that is to use a quantile loss (or a distributional loss such as log-loss) in the output layer. Which, as far as I can see in the current implementation, is exactly what happens? |
|
Please correct me explicitly though if you think I am wrong. |
|
I think this is my mistake @fkiraly. See this comment: #2340 (comment) I had a misconception that for quantile support, just using loss at the output layer is not enough, it should be used somehow in the deeper layers as well. My bad |
|
I thought in the model, the gradients will be diluted by the time they reach the inner layers and it is not necessary that it is completely aware of the quantiles. But now when I think in more detail, it is an issue with extremely deep networks (where we might need to use auxiliary heads to compute the loss in the middle of the layers as well). I didnt think of it by the chain rule and realised these are not that deep models. And by thinking that some models just support point pred losses, It made me feel that my thinking is right. But now I understand, it is just the case of the output layer not supporting the loss |
This was my thinking :) |
Alright, now that we have agreed that above statement is false, I think # blocks of code from samformer
self.linear_forecaster = nn.Linear(
self.max_encoder_length, self.max_prediction_length
) # noqa: E501
...
out = self.linear_forecaster(out)
out = out.transpose(1, 2)
target_predictions = out[:, :, -1] # (batch_size, max_prediction_length)
...
if self.n_quantiles > 1:
target_predictions = target_predictions.unsqueeze(-1).expand(
-1, -1, self.n_quantiles
)
elif self.n_quantiles == 1:
target_predictions = target_predictions.unsqueeze(-1)
return {"prediction": target_predictions}This still doesnt seem right to me, I think its just generating each quantile should have its own parameters and receive its own gradient, allowing the model to learn different quantiles. i.e., self.linear_forecaster = nn.Linear(
self.max_encoder_length, self.max_prediction_length * n_quantiles
) |
Signed-off-by: Faakhir30 <zahidfaakhir@gmail.com>
261b324 to
ff862d4
Compare
Timexer, Dlinear, SamformerSamformer
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2357 +/- ##
=======================================
Coverage ? 87.34%
=======================================
Files ? 171
Lines ? 10075
Branches ? 0
=======================================
Hits ? 8800
Misses ? 1275
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
I see - it may make sense to use different networks or at least heads for different quantiles so the model is better. But it also does not feel strictly necessary, more of a "one can try". However, I think all that matters from the API perspective is whether the last layer reasonably claims to produce quantiles (not whether the model is actually good). For this, having a multi-quantile-loss in the last layer is already sufficient - even if it may or may not produces the best quantile predictions that could be obtained. In this, there is also an idea for an interesting wrapper or operation: split the NN at some point or add heads onto it for different quantiles. Though maybe a lower prio tangent compared to ptf2 release. |
Reference Issues/PRs
Refs ##2340 (comment)
What does this implement/fix? Explain your changes.
Samformer:This doesnt seem right to me, I think its just generating
max_prediction_lengthsingle quantile and.expandis just replicating that single quantile ton_quantiletimeseach quantile should have its own parameters and receive its own gradient, allowing the model to learn different quantiles. i.e.,
PR checklist
pre-commit install.To run hooks independent of commit, execute
pre-commit run --all-files