-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
adapting the script movielens_recommendations_transformers.py to be Backend-Agnostic #2039
base: master
Are you sure you want to change the base?
adapting the script movielens_recommendations_transformers.py to be Backend-Agnostic #2039
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR!
encoded_other_features = [] | ||
|
||
# Helper function to create embeddings | ||
def embedding_helper(input): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this to be a method on the class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved.
|
||
# This function merely groups similar logic for include_movie_features=True, | ||
# or given as include_movie_features=False | ||
def movie_sequence_helper(encoded_sequence_movies): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved
# or given as include_movie_features=False | ||
def movie_sequence_helper(encoded_sequence_movies): | ||
# Create positional embedding. | ||
positions = keras.ops.arange(start=0, stop=sequence_length - 1, step=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using keras.ops.*
everywhere, just do from keras import ops
at the start -- it will shorten many lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved
output_dim=movie_embedding_dims, | ||
name="position_embedding", | ||
) | ||
positions = tf.range(start=0, limit=sequence_length - 1, delta=1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did the old really need extensive refactoring? Best I can tell only this line needed to change (to become ops.arange()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing only the tf.range
to ops.arange
does not work and I believe it is because StringLookup
is part of the model, not of tf.data.Dataset
in the original script, and only Tensorflow can handle strings.
So, my approach was to split the function encode_input_features
into two parts StringLookups
and Embeddings
. I placed the StringLookups
functionality into tf.data.Dataset
processing step, and created a class to handle each input embeddings
. And also the issue that this script must run whether one chooses to include user_features
or not also adds some refactoring.
However, if there is a less-refactoring approach/method, I'm wiling to learn and implement it. I must confess also that the refactoring felt a bit extensive, but required by my approach.
PR addressing code changes, and comments replies are also provided. |
Indeed, there was extensive refactoring. I've implemented the following new approach:
This PR implements the changes |
This PR adapts the script
movielens_recommendations_transformers.py
to be Backend-Agnostic