# Using a higher gumbel temperatureloss=tfr.keras.losses.GumbelApproxNDCGLoss(gumbel_temperature=2.0,seed=42)loss(y_true,y_pred).numpy()-0.7583889
# Using ragged tensorsy_true=tf.ragged.constant([[1.,0.],[0.,1.,0.]])y_pred=tf.ragged.constant([[0.6,0.8],[0.5,0.8,0.4]])loss=tfr.keras.losses.GumbelApproxNDCGLoss(seed=42,ragged=True)loss(y_true,y_pred).numpy()-0.6987189
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 under a tf.distribute.Strategy, except via
Model.compile() and Model.fit(), using AUTO or
SUM_OVER_BATCH_SIZE will raise an error. Please see this
custom training tutorial
for more details.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-09-29 UTC."],[],[],null,["# tfr.keras.losses.GumbelApproxNDCGLoss\n\n|----------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L1126-L1227) |\n\nComputes the Gumbel approximate NDCG loss between `y_true` and `y_pred`.\n\nInherits From: [`ApproxNDCGLoss`](../../../tfr/keras/losses/ApproxNDCGLoss) \n\n tfr.keras.losses.GumbelApproxNDCGLoss(\n reduction: tf.losses.Reduction = tf.losses.Reduction.AUTO,\n name: Optional[str] = None,\n lambda_weight: Optional[losses_impl._LambdaWeight] = None,\n temperature: float = 0.1,\n sample_size: int = 8,\n gumbel_temperature: float = 1.0,\n seed: Optional[int] = None,\n ragged: bool = False\n )\n\nImplementation of Gumbel ApproxNDCG loss ([Bruch et al, 2020](https://research.google/pubs/pub48689/)).\nThis loss is the same as [`tfr.keras.losses.ApproxNDCGLoss`](../../../tfr/keras/losses/ApproxNDCGLoss) but where logits\nare sampled from the Gumbel distribution:\n\n`y_new_pred ~ Gumbel(y_pred, 1 / temperature)`\n| **Note:** This loss is stochastic and may return different values for identical inputs.\n\n#### Standalone usage:\n\n tf.random.set_seed(42)\n y_true = [[1., 0.]]\n y_pred = [[0.6, 0.8]]\n loss = tfr.keras.losses.GumbelApproxNDCGLoss(seed=42)\n loss(y_true, y_pred).numpy()\n -0.8160851\n\n # Using a higher gumbel temperature\n loss = tfr.keras.losses.GumbelApproxNDCGLoss(gumbel_temperature=2.0,\n seed=42)\n loss(y_true, y_pred).numpy()\n -0.7583889\n\n # Using ragged tensors\n y_true = tf.ragged.constant([[1., 0.], [0., 1., 0.]])\n y_pred = tf.ragged.constant([[0.6, 0.8], [0.5, 0.8, 0.4]])\n loss = tfr.keras.losses.GumbelApproxNDCGLoss(seed=42, ragged=True)\n loss(y_true, y_pred).numpy()\n -0.6987189\n\nUsage with the `compile()` API: \n\n model.compile(optimizer='sgd', loss=tfr.keras.losses.GumbelApproxNDCGLoss())\n\n#### Definition:\n\n\\\\\\[\\\\mathcal{L}(\\\\{y\\\\}, \\\\{s\\\\}) = \\\\text{ApproxNDCGLoss}(\\\\{y\\\\}, \\\\{z\\\\})\\\\\\]\n\nwhere\n\n\\\\\\[\nz \\\\sim \\\\text{Gumbel}(s, \\\\beta)\\\\\\\\\np(z) = e\\^{-t-e\\^{-t} }\\\\\\\\\nt = \\\\beta(z - s)\\\\\\\\\n\\\\beta = \\\\frac{1}{\\\\text{temperature} }\n\\\\\\]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| References ---------- ||\n|---|---|\n| \u003cbr /\u003e - [A Stochastic Treatment of Learning to Rank Scoring Functions, Bruch et al, 2020](https://research.google/pubs/pub48689/) ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `reduction` | 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 under a [`tf.distribute.Strategy`](https://www.tensorflow.org/api_docs/python/tf/distribute/Strategy), except via [`Model.compile()`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#compile) and [`Model.fit()`](https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit), using `AUTO` or `SUM_OVER_BATCH_SIZE` will raise an error. Please see this custom training [tutorial](https://www.tensorflow.org/tutorials/distribute/custom_training) for more details. |\n| `name` | Optional name for the instance. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_config`\n\n[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L742-L752) \n\n @classmethod\n from_config(\n config, custom_objects=None\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/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L1209-L1216) \n\n get_config() -\u003e Dict[str, Any]\n\nReturns the config dictionary for a `Loss` instance.\n\n### `__call__`\n\n[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L1218-L1227) \n\n __call__(\n y_true: ../../../tfr/keras/model/TensorLike,\n y_pred: ../../../tfr/keras/model/TensorLike,\n sample_weight: Optional[utils.TensorLike] = None\n ) -\u003e tf.Tensor\n\nSee _RankingLoss."]]