tensorflow / tensorflow/recommenders

Why does the Retrieval class use an identify matrix for labels?

Open
#334 13 comments 0 reactions 1 assignee View on GitHub

@maciejkula is already working on this.

Since Jul 21, 2021.

question
Dominant language
Python
Stars
2k
Forks
300
PR merge metrics
No merged PRs in 30d

Description

Hi there,

Thanks so much for releasing and maintaining this code base. It's really fantastic.

I don't really have a bug to report, but I do have a question regarding the Retrieval class and how the loss function is being calculated. I've been working through the tutorials, focusing on the basic retrieval example and I understand that by default, the loss function uses a categorical cross entropy loss function.

Obviously, this implies having a label and predicted probabilities which I can see in the Retrieval class.

From that class, the scores are the matrix multiplication of the query and candidate embeddings:

 scores = tf.linalg.matmul(
        query_embeddings, candidate_embeddings, transpose_b=True)

Then, the labels are derived as:

labels = tf.eye(num_queries, num_candidates)

Which is then passed to tf.keras.losses.CategoricalCrossentropy to calculate the loss:

loss = self._loss(y_true=labels, y_pred=scores, sample_weight=sample_weight)

What I don't quite understand is why is the identity matrix used as the labels? Doesn't this imply that user_i has selected candidate_i? Or am I missing something?

If I relate this back to the basic retrieval example, then would the scores matrix be of size number_unique_user_ids x number_unique_movie_ids? Likewise, the rows of the loss matrix would relate to a user_id and the columns to a candidate. Wouldn't this imply that user_1 reviewed candidate_1, etc..?

Apologies if this is a fairly basic question, but I'm quite new to Tensorflow. Would appreciate any feedback or references. I've tried looking at the issues here and also on stackoverflow, but haven't really been able to find anything. Thanks.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.