Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Examples of Retrieval and Ranking Models in a Single Sequential Model? #707

Open
rlcauvin opened this issue Jan 31, 2024 · 5 comments

Comments

@rlcauvin
Copy link

Using TensorFlow Recommenders, we often employ a standard process:

  1. Retrieve candidates using a retrieval model.
  2. "Explode" the retrieval queries and retrieved candidates into query-candidate pairs to be ranked.
  3. Rank candidates using a ranking model.

The examples I've seen treat the retrieval and ranking models as separate and perform these steps in sequence, but "manually". Is it possible to combine the three steps into a single Keras sequential model with the retrieval model as the first layer, an "exploder" as the second layer, and a ranking model as the final layer?

If it is possible, are there examples of implementing it? In particular, I am not sure how we might implement the "exploder" layer.

@patrickorlando
Copy link

Yes, this should technically be possible. Usually it isn't done because the ranking model contains additional features, often features that required frequent updates.

In terms of cross joining your query and candidate tensors, check out tf.repeat and tf.tile functions.

@rlcauvin
Copy link
Author

rlcauvin commented Feb 9, 2024

Yes, this should technically be possible. Usually it isn't done because the ranking model contains additional features, often features that required frequent updates.

In terms of cross joining your query and candidate tensors, check out tf.repeat and tf.tile functions.

Thanks. I would love to see an example of using those functions to cross join queries and candidates for ranking.

@patrickorlando
Copy link

patrickorlando commented Feb 20, 2024

This is the general idea @rlcauvin

import tensorflow as tf

# Define the tensors with shape (3, 1)
tensor1 = tf.constant([['a'], ['b'], ['c']])
tensor2 = tf.constant([['x'], ['y'], ['z']])

# Tile tensor1 to repeat for each element in tensor2
tensor1_tiled = tf.tile(tensor1, [1, 3])  # Now tensor1 is tiled horizontally

# Repeat tensor2 to match the length of tensor1 and reshape to interleave with tensor1
tensor2_repeated = tf.repeat(tensor2, repeats=[3], axis=0)

# Concatenate along the second axis to get the final cross-joined tensor
cross_joined = tf.concat([tensor1_tiled, tensor2_repeated], axis=1)

which would give you

[['a', 'x'],
 ['a', 'y'],
 ['a', 'z'],
 ['b', 'x'],
 ['b', 'y'],
 ['b', 'z'],
 ['c', 'x'],
 ['c', 'y'],
 ['c', 'z']]

Do this with your query and candidate embeddings, then pass to your ranking model.

@rlcauvin
Copy link
Author

Thanks, @patrickorlando. It looks like there might be some copy-paste problems in that code snippet? Perhaps the reshaping of the two tensors before concatenating them?

@rlcauvin
Copy link
Author

rlcauvin commented May 6, 2024

A small change in the code @patrickorlando provided yields the desired output.

Instead of:

cross_joined = tf.concat([tensor1_tiled, tensor2_repeated], axis=1)

reshape tensor1_tiled before concatenating:

cross_joined = tf.concat([tf.reshape(tensor1_tiled, [-1, 1]), tensor2_repeated], axis=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants