Many recommender models are relatively complex, and do not neatly fit into
supervised or unsupervised paradigms. This base class makes it easy to
define custom training and test losses for such complex models.
This is done by asking the user to implement the following methods:
__init__ to set up your model. Variable, task, loss, and metric
initialization should go here.
compute_loss to define the training loss. The method takes as input the
raw features passed into the model, and returns a loss tensor for training.
As part of doing so, it should also update the model's metrics.
[Optional] call to define how the model computes its predictions. This
is not always necessary: for example, two-tower retrieval models have two
well-defined submodels whose call methods are normally used directly.
Note that this base class is a thin conveniece wrapper for tf.keras.Model, and
equivalent functionality can easily be achieved by overriding the train_step
and test_step methods of a plain Keras model. Doing so also makes it easy
to build even more complex training mechanisms, such as the use of
different optimizers for different variables, or manipulating gradients.
Keras has an excellent tutorial on how to
do this here.
Methods
call
call(inputs,training=None,mask=None)
Calls the model on new inputs and returns the outputs as tensors.
In this case call() just reapplies
all ops in the graph to the new inputs
(e.g. build a new computational graph from the provided inputs).
Args
inputs
Input tensor, or dict/list/tuple of input tensors.
training
Boolean or boolean scalar tensor, indicating whether to
run the Network in training mode or inference mode.
mask
A mask or list of masks. A mask can be either a boolean tensor
or None (no mask). For more details, check the guide
here.
Returns
A tensor if there is a single output, or
a list of tensors if there are more than one outputs.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tfrs.models.Model\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/recommenders/blob/v0.7.3/tensorflow_recommenders/models/base.py#L21-L100) |\n\nBase model for TFRS models.\n\n#### View aliases\n\n\n**Main aliases**\n\n[`tfrs.Model`](https://www.tensorflow.org/recommenders/api_docs/python/tfrs/models/Model)\n\n\u003cbr /\u003e\n\n tfrs.models.Model(\n *args, **kwargs\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Deep \\& Cross Network (DCN)](https://www.tensorflow.org/recommenders/examples/dcn) - [Recommending movies: ranking](https://www.tensorflow.org/recommenders/examples/basic_ranking) - [Recommending movies: retrieval](https://www.tensorflow.org/recommenders/examples/basic_retrieval) - [Taking advantage of context features](https://www.tensorflow.org/recommenders/examples/context_features) - [Building deep retrieval models](https://www.tensorflow.org/recommenders/examples/deep_recommenders) |\n\nMany recommender models are relatively complex, and do not neatly fit into\nsupervised or unsupervised paradigms. This base class makes it easy to\ndefine custom training and test losses for such complex models.\n\nThis is done by asking the user to implement the following methods:\n\n- `__init__` to set up your model. Variable, task, loss, and metric initialization should go here.\n- `compute_loss` to define the training loss. The method takes as input the raw features passed into the model, and returns a loss tensor for training. As part of doing so, it should also update the model's metrics.\n- \\[Optional\\] `call` to define how the model computes its predictions. This is not always necessary: for example, two-tower retrieval models have two well-defined submodels whose `call` methods are normally used directly.\n\nNote that this base class is a thin conveniece wrapper for tf.keras.Model, and\nequivalent functionality can easily be achieved by overriding the `train_step`\nand `test_step` methods of a plain Keras model. Doing so also makes it easy\nto build even more complex training mechanisms, such as the use of\ndifferent optimizers for different variables, or manipulating gradients.\n\nKeras has an excellent tutorial on how to\ndo this [here](https://www.tensorflow.org/guide/keras/customizing_what_happens_in_fit).\n\nMethods\n-------\n\n### `call`\n\n call(\n inputs, training=None, mask=None\n )\n\nCalls the model on new inputs and returns the outputs as tensors.\n\nIn this case `call()` just reapplies\nall ops in the graph to the new inputs\n(e.g. build a new computational graph from the provided inputs).\n| **Note:** This method should not be called directly. It is only meant to be overridden when subclassing [`tf.keras.Model`](https://www.tensorflow.org/api_docs/python/tf/keras/Model). To call a model on an input, always use the `__call__()` method, i.e. `model(inputs)`, which relies on the underlying `call()` method.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `inputs` | Input tensor, or dict/list/tuple of input tensors. |\n| `training` | Boolean or boolean scalar tensor, indicating whether to run the `Network` in training mode or inference mode. |\n| `mask` | A mask or list of masks. A mask can be either a boolean tensor or None (no mask). For more details, check the guide [here](https://www.tensorflow.org/guide/keras/masking_and_padding). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A tensor if there is a single output, or a list of tensors if there are more than one outputs. ||\n\n\u003cbr /\u003e"]]