# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: tfg
#     language: python
#     name: tfg
# ---

# +
import pandas as pd
import torch
import torch.nn as nn

from model.losses import PinballLoss
from pathlib import Path
# -

PATH_DATA = Path.cwd() / 'data' / 'files'

# +
data = pd.read_pickle((PATH_DATA / 'tests.pkl'))

y_pred = data['pred'] 
target = data['target']

quantiles = torch.tensor([0.025, 0.05, 0.1, 0.15, 0.85, 0.9, 0.95, 0.975])


# -

# Check quantileloss functions obtained from [here](https://www.yuritan.nl/posts/prediction_uncertainty/)

def LoopLoss(preds, target, quantiles):
    def _tilted_loss(q, e):
        return torch.max((q-1) * e, q * e).unsqueeze(1)

    err = target - preds
    losses = [_tilted_loss(q, err[:, i])  # calculate per quantile
              for i, q in enumerate(quantiles)]

    loss = torch.mean(torch.sum(torch.cat(losses, dim=1), dim=1))
    return loss


criterion = PinballLoss(quantiles)
criterion(y_pred, target)

pinball = PinballLoss(quantiles)
pinball.forward(y_pred, target)



StarcoderdataJupyterScriptsDedupFiltered