# tfr.keras.losses.ApproxNDCGLoss

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L1050-L1123) |

Computes approximate NDCG loss between `y_true` and `y_pred`.

    tfr.keras.losses.ApproxNDCGLoss(
        reduction: tf.losses.Reduction = tf.losses.Reduction.AUTO,
        name: Optional[str] = None,
        lambda_weight: Optional[losses_impl._LambdaWeight] = None,
        temperature: float = 0.1,
        ragged: bool = False
    )

Implementation of ApproxNDCG loss ([Qin et al, 2008](https://dl.acm.org/doi/10.1007/s10791-009-9124-x);
[Bruch et al, 2019](https://research.google/pubs/pub48168/)). This loss is an approximation for
[`tfr.keras.metrics.NDCGMetric`](https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/metrics/NDCGMetric). It replaces the non-differentiable ranking
function in NDCG with a differentiable approximation based on the logistic
function.

For each list of scores `s` in `y_pred` and list of labels `y` in `y_true`:

    loss = sum_i (2^y_i - 1) / log_2(1 + approxrank(s_i))
    approxrank(s_i) = 1 + sum_j (1 / (1 + exp(-(s_j - s_i) / temperature)))

#### Standalone usage:

    y_true = [[1., 0.]]
    y_pred = [[0.6, 0.8]]
    loss = tfr.keras.losses.ApproxNDCGLoss()
    loss(y_true, y_pred).numpy()
    -0.655107

    # Using ragged tensors
    y_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.ApproxNDCGLoss(ragged=True)
    loss(y_true, y_pred).numpy()
    -0.80536866

Usage with the `compile()` API:

    model.compile(optimizer='sgd', loss=tfr.keras.losses.ApproxNDCGLoss())

#### Definition:

\\\[
\\mathcal{L}(\\{y\\}, \\{s\\}) = - \\frac{1}{\\text{DCG}(y, y)} \\sum_{i}
\\frac{2\^{y_i} - 1}{\\log_2(1 + \\text{rank}_i)}
\\\]

where:

\\\[
\\text{rank}_i = 1 + \\sum_{j \\neq i}
\\frac{1}{1 + \\exp\\left(\\frac{-(s_j - s_i)}{\\text{temperature} }\\right)}
\\\]

<br />

<br />

| ## References ||
|---|---|
| <br /> - [A General Approximation Framework for Direct Optimization of Information Retrieval Measures, Qin et al, 2008](https://dl.acm.org/doi/10.1007/s10791-009-9124-x) - [Revisiting Approximate Metric Optimization in the Age of Deep Neural Networks, Bruch et al, 2019](https://research.google/pubs/pub48168/) ||

<br />

<br />

<br />

<br />

| ## Args ||
|---|---|
| `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. |
| `name` | Optional name for the instance. |

<br />

## Methods

### `from_config`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L742-L752)

    @classmethod
    from_config(
        config, custom_objects=None
    )

Instantiates a `Loss` from its config (output of `get_config()`).

<br />

<br />

<br />

| Args ||
|---|---|
| `config` | Output of `get_config()`. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `Loss` instance. ||

<br />

### `get_config`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L732-L740)

    get_config() -> Dict[str, Any]

Returns the config dictionary for a `Loss` instance.

### `__call__`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/losses.py#L262-L270)

    __call__(
        y_true: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorLike,
        y_pred: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorLike,
        sample_weight: Optional[utils.TensorLike] = None
    ) -> tf.Tensor

See tf.keras.losses.Loss.