Warning: This project is deprecated. TensorFlow Addons has stopped development,
The project will only be providing minimal maintenance releases until May 2024. See the full
announcement here or on
github.
tfa.losses.PinballLoss
Stay organized with collections
Save and categorize content based on your preferences.
Computes the pinball loss between y_true
and y_pred
.
tfa.losses.PinballLoss(
tau: tfa.types.FloatTensorLike
= 0.5,
reduction: str = tf.keras.losses.Reduction.AUTO,
name: str = 'pinball_loss'
)
loss = maximum(tau * (y_true - y_pred), (tau - 1) * (y_true - y_pred))
In the context of regression, this loss yields an estimator of the tau
conditional quantile.
See: https://en.wikipedia.org/wiki/Quantile_regression
Usage:
pinball = tfa.losses.PinballLoss(tau=.1)
loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.])
loss
<tf.Tensor: shape=(), dtype=float32, numpy=0.475>
Usage with the tf.keras
API:
model = tf.keras.Model()
model.compile('sgd', loss=tfa.losses.PinballLoss(tau=.1))
Args |
tau
|
(Optional) Float in [0, 1] or a tensor taking values in [0, 1] and
shape = [d0,..., dn] . It defines the slope of the pinball loss. In
the context of quantile regression, the value of tau determines the
conditional quantile level. When tau = 0.5, this amounts to l1
regression, an estimator of the conditional median (0.5 quantile).
|
reduction
|
(Optional) Type of tf.keras.losses.Reduction to apply to
loss. Default value is AUTO . AUTO indicates that the reduction
option will be determined by the usage context. For almost all cases
this defaults to SUM_OVER_BATCH_SIZE .
When used with tf.distribute.Strategy , outside of built-in training
loops such as tf.keras compile and fit , using AUTO or
SUM_OVER_BATCH_SIZE will raise an error. Please see
https://www.tensorflow.org/alpha/tutorials/distribute/training_loops
for more details on this.
|
name
|
Optional name for the op.
|
Methods
from_config
@classmethod
from_config(
config
)
Instantiates a Loss
from its config (output of get_config()
).
Args |
config
|
Output of get_config() .
|
get_config
View source
get_config()
Returns the config dictionary for a Loss
instance.
__call__
__call__(
y_true, y_pred, sample_weight=None
)
Invokes the Loss
instance.
Args |
y_true
|
Ground truth values. shape = [batch_size, d0, .. dN] , except
sparse loss functions such as sparse categorical crossentropy where
shape = [batch_size, d0, .. dN-1]
|
y_pred
|
The predicted values. shape = [batch_size, d0, .. dN]
|
sample_weight
|
Optional sample_weight acts as a coefficient for the
loss. If a scalar is provided, then the loss is simply scaled by the
given value. If sample_weight is a tensor of size [batch_size] ,
then the total loss for each sample of the batch is rescaled by the
corresponding element in the sample_weight vector. If the shape of
sample_weight is [batch_size, d0, .. dN-1] (or can be
broadcasted to this shape), then each loss element of y_pred is
scaled by the corresponding value of sample_weight . (Note
ondN-1 : all loss functions reduce by 1 dimension, usually
axis=-1.)
|
Returns |
Weighted loss float Tensor . If reduction is NONE , this has
shape [batch_size, d0, .. dN-1] ; otherwise, it is scalar. (Note
dN-1 because all loss functions reduce by 1 dimension, usually
axis=-1.)
|
Raises |
ValueError
|
If the shape of sample_weight is invalid.
|
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2023-05-25 UTC.
[null,null,["Last updated 2023-05-25 UTC."],[],[],null,["# tfa.losses.PinballLoss\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/addons/blob/v0.20.0/tensorflow_addons/losses/quantiles.py#L72-L124) |\n\nComputes the pinball loss between `y_true` and `y_pred`. \n\n tfa.losses.PinballLoss(\n tau: ../../tfa/types/FloatTensorLike = 0.5,\n reduction: str = tf.keras.losses.Reduction.AUTO,\n name: str = 'pinball_loss'\n )\n\n`loss = maximum(tau * (y_true - y_pred), (tau - 1) * (y_true - y_pred))`\n\nIn the context of regression, this loss yields an estimator of the tau\nconditional quantile.\n\nSee: \u003chttps://en.wikipedia.org/wiki/Quantile_regression\u003e\n\n#### Usage:\n\n pinball = tfa.losses.PinballLoss(tau=.1)\n loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.])\n loss\n \u003ctf.Tensor: shape=(), dtype=float32, numpy=0.475\u003e\n\nUsage with the [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) API: \n\n model = tf.keras.Model()\n model.compile('sgd', loss=tfa.losses.PinballLoss(tau=.1))\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `tau` | (Optional) Float in \\[0, 1\\] or a tensor taking values in \\[0, 1\\] and shape = `[d0,..., dn]`. It defines the slope of the pinball loss. In the context of quantile regression, the value of tau determines the conditional quantile level. When tau = 0.5, this amounts to l1 regression, an estimator of the conditional median (0.5 quantile). |\n| `reduction` | (Optional) Type of [`tf.keras.losses.Reduction`](https://www.tensorflow.org/api_docs/python/tf/keras/losses/Reduction) to apply to loss. Default value is `AUTO`. `AUTO` indicates that the reduction option will be determined by the usage context. For almost all cases this defaults to `SUM_OVER_BATCH_SIZE`. When used with [`tf.distribute.Strategy`](https://www.tensorflow.org/api_docs/python/tf/distribute/Strategy), outside of built-in training loops such as [`tf.keras`](https://www.tensorflow.org/api_docs/python/tf/keras) `compile` and `fit`, using `AUTO` or `SUM_OVER_BATCH_SIZE` will raise an error. Please see https://www.tensorflow.org/alpha/tutorials/distribute/training_loops for more details on this. |\n| `name` | Optional name for the op. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| References ---------- ||\n|---|---|\n| \u003cbr /\u003e - \u003chttps://en.wikipedia.org/wiki/Quantile_regression\u003e - \u003chttps://projecteuclid.org/download/pdfview_1/euclid.bj/1297173840\u003e ||\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_config`\n\n @classmethod\n from_config(\n config\n )\n\nInstantiates a `Loss` from its config (output of `get_config()`).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|---------------------------|\n| `config` | Output of `get_config()`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A `Loss` instance. ||\n\n\u003cbr /\u003e\n\n### `get_config`\n\n[View source](https://github.com/tensorflow/addons/blob/v0.20.0/tensorflow_addons/utils/keras_utils.py#L63-L68) \n\n get_config()\n\nReturns the config dictionary for a `Loss` instance.\n\n### `__call__`\n\n __call__(\n y_true, y_pred, sample_weight=None\n )\n\nInvokes the `Loss` instance.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `y_true` | Ground truth values. shape = `[batch_size, d0, .. dN]`, except sparse loss functions such as sparse categorical crossentropy where shape = `[batch_size, d0, .. dN-1]` |\n| `y_pred` | The predicted values. shape = `[batch_size, d0, .. dN]` |\n| `sample_weight` | Optional `sample_weight` acts as a coefficient for the loss. If a scalar is provided, then the loss is simply scaled by the given value. If `sample_weight` is a tensor of size `[batch_size]`, then the total loss for each sample of the batch is rescaled by the corresponding element in the `sample_weight` vector. If the shape of `sample_weight` is `[batch_size, d0, .. dN-1]` (or can be broadcasted to this shape), then each loss element of `y_pred` is scaled by the corresponding value of `sample_weight`. (Note on`dN-1`: all loss functions reduce by 1 dimension, usually axis=-1.) |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| Weighted loss float `Tensor`. If `reduction` is `NONE`, this has shape `[batch_size, d0, .. dN-1]`; otherwise, it is scalar. (Note `dN-1` because all loss functions reduce by 1 dimension, usually axis=-1.) ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|---------------------------------------------|\n| `ValueError` | If the shape of `sample_weight` is invalid. |\n\n\u003cbr /\u003e"]]