Skip to content

Migrate node2vec_movielens keras2 code to keras3#2278

Merged
sachinprasadhs merged 8 commits intokeras-team:masterfrom
LakshmiKalaKadali:node2vec_br
Feb 11, 2026
Merged

Migrate node2vec_movielens keras2 code to keras3#2278
sachinprasadhs merged 8 commits intokeras-team:masterfrom
LakshmiKalaKadali:node2vec_br

Conversation

@LakshmiKalaKadali
Copy link
Contributor

@LakshmiKalaKadali LakshmiKalaKadali commented Feb 5, 2026

This PR migrates the Graph representation learning with node2vec tutorial from Keras 2 to Keras 3. The implementation is now fully backend-agnostic, supporting JAX, PyTorch, and TensorFlow backends. gist

The major changes are:
Data Pipeline:
Replaced the tf.data.Dataset pipeline with keras.utils.PyDataset.
This ensures the data loading process is thread-safe and avoids "Symbolic Tensor" errors when running on JAX or PyTorch.
Custom Skip-gram Generation:
Since keras.preprocessing.sequence.skipgrams is no longer available in Keras 3, I implemented a NumPy-based manual_skipgrams utility. This maintains the tutorial's logic while remaining compatible with the new Keras 3 structure.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @LakshmiKalaKadali, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request updates the node2vec_movielens tutorial to be fully compatible with Keras 3. The core changes involve adapting the data loading pipeline, implementing a custom skip-gram generation method, and refactoring model components and utility functions to utilize Keras 3's backend-agnostic keras.ops API. This ensures the tutorial can run seamlessly across JAX, PyTorch, and TensorFlow backends, enhancing its versatility and future-proofing.

Highlights

  • Keras 3 Migration: The tutorial code has been fully migrated from Keras 2 to Keras 3, ensuring backend-agnostic compatibility with JAX, PyTorch, and TensorFlow.
  • Data Pipeline Refactor: The tf.data.Dataset pipeline has been replaced with keras.utils.PyDataset for improved thread-safety and to prevent "Symbolic Tensor" errors in JAX/PyTorch backends.
  • Custom Skip-gram Implementation: A new NumPy-based manual_skipgrams utility was introduced to replace the deprecated keras.preprocessing.sequence.skipgrams function, maintaining the tutorial's logic.
  • Backend-Agnostic Operations: Model definition and similarity calculations now leverage keras.ops for operations like L2 normalization, matrix multiplication, and top-k selection, ensuring compatibility across different Keras 3 backends.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • examples/graph/node2vec_movielens.py
    • Updated the Last modified date in the file header.
    • Replaced tensorflow imports with keras, keras.ops, and keras.layers for Keras 3 compatibility.
    • Added keras.utils.set_random_seed(42) and os.environ["KERAS_BACKEND"] = "jax" for explicit backend configuration.
    • Refactored next_step and random_walk functions by removing verbose comments and simplifying logic.
    • Introduced a new manual_skipgrams function, a NumPy-based replacement for the removed keras.preprocessing.sequence.skipgrams.
    • Modified generate_examples to use the new manual_skipgrams function and explicitly cast NumPy array dtypes.
    • Replaced the tf.data.Dataset-based data pipeline with a custom MovieLensDataset class inheriting from keras.utils.PyDataset.
    • Updated the create_model function to use Input layers with shape=(1,) and adjusted the Dot layer's axes to 2, followed by a Flatten layer for Keras 3 compatibility.
    • Migrated similarity calculation logic from tf.linalg and tf.math to keras.ops for backend-agnostic execution, including L2 normalization and top_k operations.
    • Ensured trained embeddings are converted to NumPy using ops.convert_to_numpy before writing to TSV files, and adjusted indexing for correct metadata generation.
  • ml-latest-small/README.txt
    • Added the README.txt file, providing detailed information about the MovieLens dataset (ml-latest-small), its usage license, citation, and file structures.
Activity
  • No specific activity (comments, reviews, approvals) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request successfully migrates the node2vec example from Keras 2 to Keras 3, making it backend-agnostic. The changes are well-structured, replacing tf.data.Dataset with keras.utils.PyDataset and implementing a manual skip-gram generation function. I've identified a critical issue regarding input shapes in the model, a high-severity issue in the negative sampling logic, and a couple of medium-severity suggestions for performance and documentation. Overall, this is a great update.

Copy link
Collaborator

@sachinprasadhs sachinprasadhs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added my review comment, please address it.

@sachinprasadhs
Copy link
Collaborator

Address Gemini comments and also delete ml-latest-small dir and all it's files and also remove .zip file which is committed.

Copy link
Collaborator

@sachinprasadhs sachinprasadhs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 2 small comments.

@sachinprasadhs
Copy link
Collaborator

node2vec_movielens_48_1.png file is missing.. which is inclued in the script.. did you forget to include it?

Copy link
Collaborator

@sachinprasadhs sachinprasadhs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks

@sachinprasadhs sachinprasadhs merged commit bfde978 into keras-team:master Feb 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants