Skip to content

Migrating Graph attention network (GAT) for node classification to Keras3 from Keras2#2290

Open
LakshmiKalaKadali wants to merge 3 commits intokeras-team:masterfrom
LakshmiKalaKadali:gat_br
Open

Migrating Graph attention network (GAT) for node classification to Keras3 from Keras2#2290
LakshmiKalaKadali wants to merge 3 commits intokeras-team:masterfrom
LakshmiKalaKadali:gat_br

Conversation

@LakshmiKalaKadali
Copy link
Contributor

@LakshmiKalaKadali LakshmiKalaKadali commented Feb 17, 2026

The major change made for the original tutorial is, removed custom train_step, test_step, and predict_step which relied on tf.GradientTape. Refactored the model call method to accept node indices and return sliced predictions. gist

@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 modernizes the Graph Attention Network (GAT) for node classification tutorial by migrating its implementation to Keras 3. The primary goal was to enhance compatibility and leverage Keras 3's unified API, which involved removing custom training and prediction steps in favor of the framework's built-in methods. This refactoring simplifies the model's structure and promotes backend-agnosticism, making the tutorial more robust and aligned with current Keras best practices.

Highlights

  • Keras 3 Migration: The Graph Attention Network (GAT) tutorial has been fully migrated from Keras 2 (TensorFlow backend) to Keras 3, utilizing keras.ops for backend-agnostic operations.
  • Simplified Training Loop: Custom train_step, test_step, and predict_step methods were removed from the GraphAttentionNetwork model, leveraging Keras 3's standard fit, evaluate, and predict functionalities.
  • Refactored Model Call Method: The call method of the GraphAttentionNetwork was updated to accept node indices directly, returning sliced predictions, and the graph data (node_states, edges) is now stored as internal model attributes.
  • Backend-Agnostic Operations: Core graph attention calculations within GraphAttention and MultiHeadGraphAttention layers were refactored to use keras.ops (e.g., ops.matmul, ops.take, ops.segment_sum) for 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/gat_node_classification.py
    • Added attribution for Keras 3 conversion in the file header.
    • Updated imports to use keras and keras.ops, and configured the KERAS_BACKEND environment variable.
    • Replaced np.random.seed with keras.utils.set_random_seed for consistent random state management.
    • Adjusted the data_dir path construction for dataset loading.
    • Migrated tensor creation from tf.convert_to_tensor to ops.convert_to_tensor, explicitly defining data types.
    • Completely refactored the GraphAttention.call method to utilize keras.ops for all tensor manipulations, including attention score calculation, normalization, and aggregation.
    • Modified MultiHeadGraphAttention.call to use keras.ops.concatenate and keras.ops.mean for combining outputs from multiple attention heads.
    • Removed custom train_step, predict_step, and test_step implementations from GraphAttentionNetwork.
    • Updated GraphAttentionNetwork.call to accept node indices as input and return targeted predictions, storing node_states and edges as model attributes.
    • Adjusted model compilation parameters, including loss function, optimizer, and metric names, to align with Keras 3 standards.
    • Updated the prediction logic to apply ops.softmax and ops.convert_to_numpy after obtaining raw logits from the model.
Activity
  • No specific activity (comments, reviews, or progress updates) was provided in the context for this pull request.
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 Graph Attention Network (GAT) example to Keras 3. The changes are well-executed, including updating to keras.ops for backend-agnostic operations and refactoring the model to use the standard Keras fit/evaluate/predict workflow by removing custom training steps. This significantly improves the code's clarity and alignment with modern Keras practices. I have one minor suggestion to further improve code readability.

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.

Thanks, looks good, just one small comment, you can address and generate .md and .ipynb files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants