tensorflow / tensorflow/recommenders

RemoveAccidentalHits Not Work Correctly

Open
#706 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

First i will explain code
I took the code here and analyzed it. class RemoveAccidentalHits(tf.keras.layers.Layer)

import tensorflow as tf

# MIN_FLOAT 
MIN_FLOAT = 1e-8

# Örnek veri oluştur
batch_size = 3
num_candidates = 4

# Örnek labels (one-hot encoded)
labels = tf.constant([
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0]
], dtype=tf.float32)

# Örnek logits
logits = tf.random.normal(shape=(batch_size, num_candidates))

# Örnek candidate_ids
candidate_ids = tf.constant(["101", "102", "101", "104"], dtype=tf.string)

# Create an instance of the RemoveAccidentalHits layer
remove_accidental_hits_layer = RemoveAccidentalHits()

# Modify logits using the custom layer
modified_logits = remove_accidental_hits_layer(labels, logits, candidate_ids)

# Print the original and modified logits
print("Original Logits:")
print(logits.numpy())
print("\nModified Logits:")
print(modified_logits.numpy())

Output layer. Nothing change here but i expected to logits[0, 2] and logits[2,0] set to min_float

Original Logits:
[[ 0.82528496 -1.3708178   0.21487834 -0.06206743]
 [-0.79802114  1.9325752   1.1109723   0.963176  ]
 [ 1.3992568  -0.7718229   2.2503998   0.77576673]]

Modified Logits:
[[ 0.82528496 -1.3708178   0.21487835 -0.06206743]
 [-0.79802114  1.9325752   1.1109723   0.963176  ]
 [ 1.3992568  -0.7718229   2.2503998   0.77576673]]

The problem here is that the information set as duplicate is intended to be assigned a very small value, but there is a small error here.
logits + duplicate * MIN_FLOAT
When we assign in this way, the values will remain very similar. It is necessary to update the code here as I did.

As far as I understand, duplicate variable works like a mask layer.
This works correctly.

duplicate = duplicate - labels
duplicate
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[0., 0., 1., 0.],
       [0., 0., 0., 0.],
       [1., 0., 0., 0.]], dtype=float32)>

My sugessiton here. First we need take inverse duplicate matrix.

inverse_duplicate = tf.where(tf.equal(duplicate, 1), 0., 1.)
inverse_duplicate
<tf.Tensor: shape=(3, 4), dtype=float32, numpy=
array([[1., 1., 0., 1.],
       [1., 1., 1., 1.],
       [0., 1., 1., 1.]], dtype=float32)>

Then we need to convert last line like this

logits * inverse_duplicate + MIN_FLOAT
array([[ 8.2528496e-01, -1.3708178e+00,  9.9999999e-09, -6.2067419e-02],
       [-7.9802114e-01,  1.9325752e+00,  1.1109723e+00,  9.6317601e-01],
       [ 9.9999999e-09, -7.7182293e-01,  2.2503998e+00,  7.7576673e-01]],
      dtype=float32)

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.

Research direction

Start with the linked RemoveAccidentalHits class in tensorflow_recommenders/layers/loss.py and reproduce the reported example using the shown labels, logits, and candidate_ids. Check that accidental-hit positions are changed to MIN_FLOAT while other logits remain unchanged; the issue does not mention a specific test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.