question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] can `Scann` be used inside the model during training?

See original GitHub issue

I have the following model using sequences to predict the next item:

class Model(tfrs.models.Model):
    def __init__(self):
        super().__init__()

        self.query_model = tf.keras.Sequential([
            QueryModel(),
            tf.keras.layers.Dense(64),
            L2NormalizationLayer(axis=1)

        ])
        
        self.candidate_model = tf.keras.Sequential([
            CandidateModel(),
            tf.keras.layers.Dense(64),
            L2NormalizationLayer(axis=1)
        ])
        
        scann = tfrs.layers.factorized_top_k.ScaNN(num_reordering_candidates=100)
        scann.index_from_dataset(
            candidates_ds.map(
                lambda x: (x['id'], self.candidate_model({ 'url': x['url'] }))
            )
        )
        
        self.task = tfrs.tasks.Retrieval(
            # Normal approach commented out, using the candidate model to map over a dataset of unique candidates.
             metrics=tfrs.metrics.FactorizedTopK(
                  # candidates=candidates_ds.map( lambda x: (x['id'], self.candidate_model({ 'url': x['url'] })))
                  # Instead we use the SCANN layer
                  candidates=scann
            )
            remove_accidental_hits=True
        )
        
    def call(self, features):
        candidate_embeddings = self.candidate_model({
            'url': features['url'],
        })

        query_embeddings = self.query_model({
            'advertiser_name': features['advertiser_name']        
        })
                
        return (
            query_embeddings,
            candidate_embeddings,
        )
    
    def compute_loss(self, features, training=False):
        query_embeddings, candidate_embeddings = self(features)

        return self.task(
            query_embeddings, 
            candidate_embeddings,
            candidate_ids=features['id'],
            compute_metrics=not training
        )

This runs fine and much quicker! The evaluation step sees 100x speed ups!

However this model does not improve on the metric, my first thought it that the model is not updating the embeddings each epoch as they are run only once. However, in the normal approach we also pass a dataset of already mapped candidate embeddings…

At which point in the training does the model update the embeddings it is learning to use for new evaluation runs?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
maciejkulacommented, Oct 12, 2022

@ydennisy to add to Patrick’s answer, Keras caches compiled TensorFlow functions. Remember to call compile before every evaluation as per https://github.com/tensorflow/recommenders/issues/388#issuecomment-941254103.

1reaction
patrickorlandocommented, Aug 30, 2022

This concept is discussed in https://github.com/tensorflow/recommenders/issues/388#issuecomment-941254103 and the comments following.

To make this work you must re-construct the index before each call to model.evaluate() to update the candidate embedddings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

10-601 Machine Learning, Midterm Exam
Justify your answer in one or two sentences. Solution: We expect students to explain how each of these learning techniques can be used...
Read more >
Machine Learning | linkedin-skill-assessments-quizzes
Yes, data model variance trains the unsupervised machine learning algorithm. No, data model bias and variance involve supervised learning. Q37. Which choice is ......
Read more >
AI-900 Exam - Free Questions and Answers - ITExams.com
Enhance your AI-900 Microsoft Azure AI Fundamentals skills with free questions updated every hour and answers explained by community assistance.
Read more >
SCAN 2022 SNP Model of Care (MOC) Training Frequently ...
All rights reserved. SCAN 2022 SNP Model of Care (MOC) Training. Frequently Asked Questions (FAQs) ... If there are no HRA in a...
Read more >
What are the basics of environmental scanning as ... - SHRM
Economy: What is happening in the economy that could affect future business? ... an environmental scan, a variety of methods should be used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found