-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from keras import ops | ||
from k3_addons.utils.keras_utils import LossFunctionWrapper | ||
from k3_addons.api_export import k3_export | ||
|
||
|
||
@k3_export("k3_addons.losses.pinball_loss") | ||
def pinball_loss(y_true, y_pred, tau=0.5): | ||
y_pred = ops.convert_to_tensor(y_pred) | ||
y_true = ops.cast(y_true, y_pred.dtype) | ||
|
||
tau = ops.expand_dims(ops.cast(tau, y_pred.dtype), 0) | ||
one = ops.cast(1, tau.dtype) | ||
|
||
delta_y = y_true - y_pred | ||
pinball = ops.maximum(tau * delta_y, (tau - one) * delta_y) | ||
return ops.mean(pinball, axis=-1) | ||
|
||
|
||
@k3_export("k3_addons.losses.PinballLoss") | ||
class PinballLoss(LossFunctionWrapper): | ||
def __init__( | ||
self, | ||
tau=0.5, | ||
reduction=None, | ||
name="pinball_loss", | ||
): | ||
super().__init__(pinball_loss, reduction=reduction, name=name, tau=tau) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import numpy as np | ||
from keras import ops | ||
from k3_addons.losses.quantiles import PinballLoss | ||
|
||
|
||
def test_pinball_loss(): | ||
loss = PinballLoss(tau=.1)([0., 0., 1., 1.],[1., 1., 1., 0.]) | ||
assert np.allclose(ops.convert_to_numpy(loss), 0.475)ß |