# tfr.keras.saved_model.Signatures

<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/saved_model.py#L24-L175) |

Defines signatures to support regress and predict serving.

    tfr.keras.saved_model.Signatures(
        model: tf.keras.Model,
        context_feature_spec: Dict[str, Union[tf.io.FixedLenFeature, tf.io.RaggedFeature]],
        example_feature_spec: Dict[str, Union[tf.io.FixedLenFeature, tf.io.RaggedFeature]],
        mask_feature_name: str
    )

This wraps the trained Keras model in two serving functions that can be saved
with [`tf.saved_model.save`](https://www.tensorflow.org/api_docs/python/tf/saved_model/save) or `model.save`, and loaded with corresponding
signature names. The regress serving signature takes a batch of serialized
`tf.Example`s as input, whereas the predict serving signature takes a batch of
serialized `ExampleListWithContext` as input.

#### Example usage:

A ranking model can be saved with signatures as follows:

    tf.saved_model.save(model, path, signatures=Signatures(model, ...)())

For regress serving, scores can be generated using `REGRESS` signature as
follows:

    loaded_model = tf.saved_model.load(path)
    predictor = loaded_model.signatures[tf.saved_model.REGRESS_METHOD_NAME]
    scores = predictor(serialized_examples)[tf.saved_model.REGRESS_OUTPUTS]

For predict serving, scores can be generated using `PREDICT` signature as
follows:

    loaded_model = tf.saved_model.load(path)
    predictor = loaded_model.signatures[tf.saved_model.PREDICT_METHOD_NAME]
    scores = predictor(serialized_elwcs)[tf.saved_model.PREDICT_OUTPUTS]

<br />

<br />

<br />

| ## Args ||
|---|---|
| `model` | A Keras ranking model. |
| `context_feature_spec` | (dict) A mapping from feature keys to `FixedLenFeature` or `RaggedFeature` values for context in `tensorflow.serving.ExampleListWithContext` proto. |
| `example_feature_spec` | (dict) A mapping from feature keys to `FixedLenFeature` or `Ragged` values for examples in `tensorflow.serving.ExampleListWithContext` proto. |
| `mask_feature_name` | (str) Name of feature for example list masks. |

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `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 /> |
| `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

### `normalize_outputs`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/saved_model.py#L84-L105)

    normalize_outputs(
        default_key: str, outputs: Union[tf.Tensor, Dict[str, tf.Tensor]]
    ) -> Dict[str, tf.Tensor]

Returns a dict of Tensors for outputs.

<br />

<br />

<br />

| Args ||
|---|---|
| `default_key` | If outputs is a Tensor, use the default_key to make a dict. |
| `outputs` | outputs to be normalized. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A dict maps from str-like key(s) to Tensor(s). ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| TypeError if outputs is not a Tensor nor a dict. ||

<br />

### `predict_tf_function`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/saved_model.py#L107-L124)

    predict_tf_function() -> Callable[[tf.Tensor], Dict[str, tf.Tensor]]

Makes a tensorflow function for `predict`.

### `regress_tf_function`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/saved_model.py#L126-L145)

    regress_tf_function() -> Callable[[tf.Tensor], Dict[str, tf.Tensor]]

Makes a tensorflow function for `regress`.

### `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 />

### `__call__`

[View source](https://github.com/tensorflow/ranking/blob/v0.5.3/tensorflow_ranking/python/keras/saved_model.py#L147-L175)

    __call__(
        serving_default: str = 'regress'
    ) -> Dict[str, Callable[[tf.Tensor], Dict[str, tf.Tensor]]]

Returns a dict of signatures.

<br />

<br />

<br />

| Args ||
|---|---|
| `serving_default` | Specifies "regress" or "predict" as the serving_default signature. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A dict of signatures. ||

<br />