# tfr.keras.losses.PrecisionLambdaWeight

<br />

|---|
| [![](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#L213-L228) |

Keras serializable class for Precision.

    tfr.keras.losses.PrecisionLambdaWeight(
        topn: Optional[int] = None,
        positive_fn: Optional[https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/utils/GainFunction] = None,
        **kwargs
    )

<br />

<br />

<br />

| ## Args ||
|---|---|
| `topn` | (int) The K in Precision@K metric. |
| `positive_fn` | (function): A function on `Tensor` that output boolean True for positive examples. The rest are negative examples. |

<br />

## Methods

### `get_config`

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

    get_config() -> Dict[str, Any]

### `individual_weights`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/losses_impl.py#L195-L207)

    individual_weights(
        labels, ranks
    )

Returns the weight `Tensor` for individual examples.

<br />

<br />

<br />

| Args ||
|---|---|
| `labels` | A dense `Tensor` of labels with shape \[batch_size, list_size\]. |
| `ranks` | A dense `Tensor` of ranks with the same shape as `labels` that are sorted by logits. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `Tensor` that can weight individual examples. ||

<br />

### `pair_weights`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/losses_impl.py#L426-L454)

    pair_weights(
        labels, ranks
    )

See `_LambdaWeight`.

The current implementation here is that for any pairs of documents i and j,
we set the weight to be 1 if

- i and j have different labels.
- i \<= topn and j \> topn or i \> topn and j \<= topn. This is exactly the same as the original LambdaRank method. The weight is the gain of swapping a pair of documents.

<br />

<br />

<br />

| Args ||
|---|---|
| `labels` | A dense `Tensor` of labels with shape \[batch_size, list_size\]. |
| `ranks` | A dense `Tensor` of ranks with the same shape as `labels` that are sorted by logits. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A `Tensor` that can weight example pairs. ||

<br />