# tfr.keras.model.ModelBuilder

<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/model.py#L316-L395) |

Builds a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model).

Inherits From: [`ModelBuilderWithMask`](https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/ModelBuilderWithMask), [`AbstractModelBuilder`](https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/AbstractModelBuilder)

    tfr.keras.model.ModelBuilder(
        input_creator: Callable[[], Tuple[TensorDict, TensorDict]],
        preprocessor: Callable[[TensorDict, TensorDict, tf.Tensor], Tuple[TensorDict, TensorDict]],
        scorer: Callable[[TensorDict, TensorDict, tf.Tensor], Union[TensorLike, TensorDict]],
        mask_feature_name: str,
        name: Optional[str] = None
    )

This class implements the `ModelBuilderWithMask` by delegating the class
behaviors to the following implementors that can be specified by callers:

- input_creator: A callable or a class like `InputCreator` to implement `create_inputs`.
- preprocessor: A callable or a class like `Preprocessor` to implement `preprocess`.
- scorer: A callable or a class like `Scorer` to implement `score`.

Users can subclass those implementor classes and pass the objects into this
class to build a [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model).

#### Example usage:

    model_builder = ModelBuilder(
        input_creator=FeatureSpecInputCreator(
            {},
            {"example_feature_1": tf.io.FixedLenFeature(
                shape=(1,), dtype=tf.float32, default_value=0.0)}),
        preprocessor=PreprocessorWithSpec(),
        scorer=DNNScorer(hidden_layer_dims=[16]),
        mask_feature_name="list_mask",
        name="model_builder")

<br />

<br />

<br />

| ## Args ||
|---|---|
| `input_creator` | A callable or a class like `InputCreator` to implement `create_inputs`. |
| `preprocessor` | A callable or a class like `Preprocessor` to implement `preprocess`. |
| `scorer` | A callable or a class like `Scorer` to implement `score`. |
| `mask_feature_name` | name of 2D mask boolean feature. |
| `name` | (optional) name of the Model. |

<br />

## Methods

### `build`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/model.py#L285-L313)

    build() -> tf.keras.Model

Builds a Keras Model for Ranking Pipeline.

#### Example usage:

    model_builder = SimpleModelBuilder(
        {},
        {"example_feature_1": tf.io.FixedLenFeature(
            shape=(1,), dtype=tf.float32, default_value=0.0)},
        "list_mask", "model_builder")
    model = model_builder.build()

<br />

<br />

<br />

| Returns ||
|---|---|
| A [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model). ||

<br />

### `create_inputs`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/model.py#L372-L377)

    create_inputs() -> Tuple[https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict, https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict, tf.Tensor]

See `ModelBuilderWithMask`.

### `preprocess`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/model.py#L379-L386)

    preprocess(
        context_inputs: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict,
        example_inputs: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict,
        mask: tf.Tensor
    ) -> Tuple[https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict, https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict]

See `ModelBuilderWithMask`.

### `score`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/model.py#L388-L395)

    score(
        context_features: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict,
        example_features: https://www.tensorflow.org/ranking/api_docs/python/tfr/keras/model/TensorDict,
        mask: tf.Tensor
    ) -> Union[TensorLike, TensorDict]

See `ModelBuilderWithMask`.