Custom loss function for regression
I am trying to write a custom loss function for a machine learning regression task. What I want to accomplish is following:
- Reward higher preds, higher targets
- Punish higher preds, lower targets
- Ignore lower preds, lower targets
- Ignore lower preds, higher targets
All ideas are welcome, pseudo code or python code works good for me.
This is what I tried so far, it does not work so well I think it is because it does not take high targets into account (just high preds):
def mae_high(inp, targ):
inp, targ = flatten_check(inp, targ)
thresh = np.percentile(inp.detach().numpy(), 50)
mask = inp thresh
high_preds = torch.masked_select(inp, mask)
high_targ = torch.masked_select(targ, mask)
return torch.abs(high_preds - high_targ).mean()
Topic loss loss-function deep-learning python machine-learning
Category Data Science