<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/extension/premade/tfrbert_task.py#L152-L357) |

Task object for tf-ranking BERT.

Inherits From: [`RankingTask`](https://www.tensorflow.org/ranking/api_docs/python/tfr/extension/task/RankingTask)

#### View aliases


**Main aliases**

[`tfr.extension.premade.tfrbert_task.TFRBertTask`](https://www.tensorflow.org/ranking/api_docs/python/tfr/extension/premade/TFRBertTask)

<br />

    tfr.extension.premade.TFRBertTask(
        params,
        label_spec: Tuple[str, tf.io.FixedLenFeature] = None,
        logging_dir: Optional[str] = None,
        name: Optional[str] = None,
        **kwargs
    )

<br />

<br />

<br />

| ## Args ||
|---|---|
| `params` | the task configuration instance, which can be any of dataclass, ConfigDict, namedtuple, etc. |
| `logging_dir` | a string pointing to where the model, summaries etc. will be saved. You can also write additional stuff in this directory. |
| `name` | the task name. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `logging_dir` | <br /> <br /> |
| `name` | Returns the name of this module as passed or determined in the ctor. <br /> > [!NOTE] > **Note:** This is not the same as the `self.name_scope.name` which includes parent module names. |
| `name_scope` | Returns a [`tf.name_scope`](https://www.tensorflow.org/api_docs/python/tf/name_scope) instance for this class. |
| `non_trainable_variables` | Sequence of non-trainable variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `submodules` | Sequence of all sub-modules. Submodules are modules which are properties of this module, or found as properties of modules which are properties of this module (and so on). a = tf.Module() b = tf.Module() c = tf.Module() a.b = b b.c = c list(a.submodules) == [b, c] True list(b.submodules) == [c] True list(c.submodules) == [] True <br /> |
| `task_config` | <br /> <br /> |
| `trainable_variables` | Sequence of trainable variables owned by this module and its submodules. <br /> > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |
| `variables` | Sequence of variables owned by this module and its submodules. > [!NOTE] > **Note:** this method uses reflection to find variables on the current instance and submodules. For performance reasons you may wish to cache the result of calling this method if you don't expect the return value to change. |

## Methods

### `aggregate_logs`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L234-L243)

    aggregate_logs(
        state=None, step_outputs=None
    )

Aggregates over logs. This runs on CPU in eager mode.

### `build_inputs`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L202-L207)

    build_inputs(
        params, input_context=None
    )

Returns tf.data.Dataset for tf-ranking BERT task.

### `build_losses`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/task.py#L222-L229)

    build_losses(
        labels, model_outputs, aux_losses=None
    ) -> tf.Tensor

Standard interface to compute losses.

<br />

<br />

<br />

| Args ||
|---|---|
| `labels` | optional label tensors. |
| `model_outputs` | a nested structure of output tensors. |
| `aux_losses` | auxiliary loss tensors, i.e. `losses` in keras.Model. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The total loss tensor. ||

<br />

### `build_metrics`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/task.py#L231-L242)

    build_metrics(
        training=None
    )

Gets streaming metrics for training/validation.

### `build_model`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L177-L200)

    build_model()

\[Optional\] Creates model architecture.

<br />

<br />

<br />

| Returns ||
|---|---|
| A model instance. ||

<br />

### `create_optimizer`

    @classmethod
    create_optimizer(
        optimizer_config: OptimizationConfig,
        runtime_config: Optional[RuntimeConfig] = None,
        dp_config: Optional[DifferentialPrivacyConfig] = None
    )

Creates an TF optimizer from configurations.

<br />

<br />

<br />

| Args ||
|---|---|
| `optimizer_config` | the parameters of the Optimization settings. |
| `runtime_config` | the parameters of the runtime. |
| `dp_config` | the parameter of differential privacy. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A tf.optimizers.Optimizer object. ||

<br />

### `inference_step`

    inference_step(
        inputs, model: tf.keras.Model
    )

Performs the forward step.

With distribution strategies, this method runs on devices.

<br />

<br />

<br />

| Args ||
|---|---|
| `inputs` | a dictionary of input tensors. |
| `model` | the keras.Model. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| Model outputs. ||

<br />

### `initialize`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L342-L357)

    initialize(
        model
    )

Load a pretrained checkpoint (if exists) and then train from iter 0.

### `process_compiled_metrics`

    process_compiled_metrics(
        compiled_metrics, labels, model_outputs
    )

Process and update compiled_metrics.

call when using compile/fit API.

<br />

<br />

<br />

| Args ||
|---|---|
| `compiled_metrics` | the compiled metrics (model.compiled_metrics). |
| `labels` | a tensor or a nested structure of tensors. |
| `model_outputs` | a tensor or a nested structure of tensors. For example, output of the keras model built by self.build_model. |

<br />

### `process_metrics`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/task.py#L244-L246)

    process_metrics(
        metrics, labels, model_outputs
    )

Process and update metrics.

Called when using custom training loop API.

<br />

<br />

<br />

| Args ||
|---|---|
| `metrics` | a nested structure of metrics objects. The return of function self.build_metrics. |
| `labels` | a tensor or a nested structure of tensors. |
| `model_outputs` | a tensor or a nested structure of tensors. For example, output of the keras model built by self.build_model. |
| `**kwargs` | other args. |

<br />

### `reduce_aggregated_logs`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L245-L273)

    reduce_aggregated_logs(
        aggregated_logs, global_step=None
    )

Calculates aggregated metrics and writes predictions to csv.

### `train_step`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/task.py#L248-L264)

    train_step(
        inputs,
        model: tf.keras.Model,
        optimizer: tf.keras.optimizers.Optimizer,
        metrics
    )

Does forward and backward.

With distribution strategies, this method runs on devices.

<br />

<br />

<br />

| Args ||
|---|---|
| `inputs` | a dictionary of input tensors. |
| `model` | the model, forward pass definition. |
| `optimizer` | the optimizer for this training step. |
| `metrics` | a nested structure of metrics objects. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary of logs. ||

<br />

### `validation_step`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/extension/premade/tfrbert_task.py#L209-L232)

    validation_step(
        inputs, model: tf.keras.Model, metrics=None
    )

Validation step.

With distribution strategies, this method runs on devices.

<br />

<br />

<br />

| Args ||
|---|---|
| `inputs` | a dictionary of input tensors. |
| `model` | the keras.Model. |
| `metrics` | a nested structure of metrics objects. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A dictionary of logs. ||

<br />

### `with_name_scope`

    @classmethod
    with_name_scope(
        method
    )

Decorator to automatically enter the module name scope.

    class MyModule(tf.Module):
      @tf.Module.with_name_scope
      def __call__(self, x):
        if not hasattr(self, 'w'):
          self.w = tf.Variable(tf.random.normal([x.shape[1], 3]))
        return tf.matmul(x, self.w)

Using the above module would produce [`tf.Variable`](https://www.tensorflow.org/api_docs/python/tf/Variable)s and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s whose
names included the module name:

    mod = MyModule()
    mod(tf.ones([1, 2]))
    <tf.Tensor: shape=(1, 3), dtype=float32, numpy=..., dtype=float32)>
    mod.w
    <tf.Variable 'my_module/Variable:0' shape=(2, 3) dtype=float32,
    numpy=..., dtype=float32)>

<br />

<br />

<br />

| Args ||
|---|---|
| `method` | The method to wrap. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| The original method wrapped such that it enters the module's name scope. ||

<br />

<br />

<br />

<br />

| ## Class Variables ||
|---|---|
| loss | `'loss'` |

<br />