RSVP for your your local TensorFlow Everywhere event today!

tfrs.layers.factorized_top_k.TopK

Interface for top K layers.

Implementers must provide the following two methods:

  1. index: takes a tensor of candidate embeddings and creates the retrieval index.
  2. call: takes a tensor of queries and returns top K candidates for those queries.

Methods

call

View source

Query the index.

Args
queries Query features. If query_model was provided in the constructor, these can be raw query features that will be processed by the query model before performing retrieval. If query_model was not provided, these should be pre-computed query embeddings.
k The number of candidates to retrieve. If not supplied, defaults to the k value supplied in the constructor.

Returns
Tuple of (top candidate scores, top candidate identifiers).

Raises
ValueError if index has not been called.

index

View source

Builds the retrieval index.

When called multiple times the existing index will be dropped and a new one created.

Args
candidates Matrix (or dataset) of candidate embeddings.
identifiers Optional tensor (or dataset) of candidate identifiers. If given, these will be used to as identifiers of top candidates returned when performing searches. If not given, indices into the candidates tensor will be given instead.

Returns
Self.

query_with_exclusions

View source

Query the index.

Args
queries Query features. If query_model was provided in the constructor, these can be raw query features that will be processed by the query model before performing retrieval. If query_model was not provided, these should be pre-computed query embeddings.
exclusions [query_batch_size, num_to_exclude] tensor of identifiers to be excluded from the top-k calculation. This is most commonly used to exclude previously seen candidates from retrieval. For example, if a user has already seen items with ids "42" and "43", you could set exclude to [["42", "43"]].
k The number of candidates to retrieve. Defaults to constructor k parameter if not supplied.

Returns
Tuple of (top candidate scores, top candidate identifiers).

Raises
ValueError if index has not been called. ValueError if queries is not a tensor (after being passed through the query model).