Skip to content

Conversation

@ooples
Copy link
Owner

@ooples ooples commented Nov 8, 2025

This commit implements three critical Graph Neural Network architectures to expand the AiDotNet library's GNN capabilities:

  1. Graph Attention Networks (GAT)

    • Multi-head attention mechanism for graph-structured data
    • Learns importance weights for neighbor nodes dynamically
    • Supports configurable attention heads and dropout
    • File: GraphAttentionLayer.cs
  2. GraphSAGE (Graph Sample and Aggregate)

    • Inductive learning framework for graph data
    • Three aggregator types: Mean, MaxPool, and Sum
    • L2 normalization support for stable training
    • Designed for generalizing to unseen nodes
    • File: GraphSAGELayer.cs
  3. Graph Isomorphism Network (GIN)

    • Provably as powerful as Weisfeiler-Lehman test
    • Uses sum aggregation with learnable epsilon parameter
    • Two-layer MLP for powerful feature transformation
    • Optimal for tasks requiring fine-grained structural understanding
    • File: GraphIsomorphismLayer.cs

Additional Changes:

  • Created IGraphConvolutionLayer interface defining common GNN contracts
  • Moved GraphConvolutionalLayer to new Graph/ subdirectory
  • Updated GraphConvolutionalLayer to implement IGraphConvolutionLayer
  • Added comprehensive unit tests for all graph layers (GraphLayerTests.cs)

Architecture:
All layers follow consistent patterns:

  • Implement IGraphConvolutionLayer and extend LayerBase
  • Support adjacency matrix input via SetAdjacencyMatrix()
  • Provide InputFeatures and OutputFeatures properties
  • Include detailed XML documentation with beginner-friendly explanations

Use Cases:

  • Social network analysis
  • Molecular property prediction
  • Knowledge graph reasoning
  • Recommendation systems
  • Citation network analysis

Resolves #401

User Story / Context

  • Reference: [US-XXX] (if applicable)
  • Base branch: merge-dev2-to-master

Summary

  • What changed and why (scoped strictly to the user story / PR intent)

Verification

  • Builds succeed (scoped to changed projects)
  • Unit tests pass locally
  • Code coverage >= 90% for touched code
  • Codecov upload succeeded (if token configured)
  • TFM verification (net46, net6.0, net8.0) passes (if packaging)
  • No unresolved Copilot comments on HEAD

Copilot Review Loop (Outcome-Based)

Record counts before/after your last push:

  • Comments on HEAD BEFORE: [N]
  • Comments on HEAD AFTER (60s): [M]
  • Final HEAD SHA: [sha]

Files Modified

  • List files changed (must align with scope)

Notes

  • Any follow-ups, caveats, or migration details

This commit implements three critical Graph Neural Network architectures
to expand the AiDotNet library's GNN capabilities:

1. **Graph Attention Networks (GAT)**
   - Multi-head attention mechanism for graph-structured data
   - Learns importance weights for neighbor nodes dynamically
   - Supports configurable attention heads and dropout
   - File: GraphAttentionLayer.cs

2. **GraphSAGE (Graph Sample and Aggregate)**
   - Inductive learning framework for graph data
   - Three aggregator types: Mean, MaxPool, and Sum
   - L2 normalization support for stable training
   - Designed for generalizing to unseen nodes
   - File: GraphSAGELayer.cs

3. **Graph Isomorphism Network (GIN)**
   - Provably as powerful as Weisfeiler-Lehman test
   - Uses sum aggregation with learnable epsilon parameter
   - Two-layer MLP for powerful feature transformation
   - Optimal for tasks requiring fine-grained structural understanding
   - File: GraphIsomorphismLayer.cs

**Additional Changes:**
- Created IGraphConvolutionLayer<T> interface defining common GNN contracts
- Moved GraphConvolutionalLayer to new Graph/ subdirectory
- Updated GraphConvolutionalLayer to implement IGraphConvolutionLayer
- Added comprehensive unit tests for all graph layers (GraphLayerTests.cs)

**Architecture:**
All layers follow consistent patterns:
- Implement IGraphConvolutionLayer<T> and extend LayerBase<T>
- Support adjacency matrix input via SetAdjacencyMatrix()
- Provide InputFeatures and OutputFeatures properties
- Include detailed XML documentation with beginner-friendly explanations

**Use Cases:**
- Social network analysis
- Molecular property prediction
- Knowledge graph reasoning
- Recommendation systems
- Citation network analysis

Resolves #401
Copilot AI review requested due to automatic review settings November 8, 2025 20:19
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 8, 2025

Warning

Rate limit exceeded

@ooples has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 10 minutes and 47 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between f99b0d2 and 705c5d6.

📒 Files selected for processing (6)
  • src/NeuralNetworks/Layers/Graph/GraphAttentionLayer.cs (1 hunks)
  • src/NeuralNetworks/Layers/Graph/GraphConvolutionalLayer.cs (5 hunks)
  • src/NeuralNetworks/Layers/Graph/GraphIsomorphismLayer.cs (1 hunks)
  • src/NeuralNetworks/Layers/Graph/GraphSAGELayer.cs (1 hunks)
  • src/NeuralNetworks/Layers/Graph/IGraphConvolutionLayer.cs (1 hunks)
  • tests/AiDotNet.Tests/UnitTests/NeuralNetworks/Layers/GraphLayerTests.cs (1 hunks)
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch claude/fix-issue-401-011CUw2PdjV8WHcpjT2wAkpr

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds comprehensive support for Graph Neural Networks (GNNs) to the AiDotNet library by introducing a common interface and four distinct graph layer implementations for processing graph-structured data.

  • Defines IGraphConvolutionLayer<T> interface with methods for managing adjacency matrices and node features
  • Implements four graph layer types: GCN (Graph Convolutional Network), GAT (Graph Attention Network), GraphSAGE, and GIN (Graph Isomorphism Network)
  • Provides extensive test coverage for all layers including constructor tests, forward pass validation, and interface compliance checks

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
IGraphConvolutionLayer.cs Defines interface contract for graph layers with adjacency matrix management and feature dimensions
GraphConvolutionalLayer.cs Updates existing GCN layer to implement new interface and adds GetAdjacencyMatrix method
GraphAttentionLayer.cs New implementation of GAT layer with multi-head attention mechanism
GraphSAGELayer.cs New implementation of GraphSAGE with support for Mean, MaxPool, and Sum aggregators
GraphIsomorphismLayer.cs New implementation of GIN layer with learnable epsilon and MLP transformation
GraphLayerTests.cs Comprehensive unit tests covering all graph layers and their functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// - Mean: Average all neighbor features (balanced, smooth)
/// - Max: Take the maximum value from neighbors (emphasizes outliers)
/// - Sum: Add up all neighbor features (sensitive to number of neighbors)
/// - LSTM: Use a recurrent network to aggregate (most expressive but slowest)
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

The documentation mentions LSTM as an aggregation option, but the SAGEAggregatorType enum (lines 15-31) does not include an LSTM option. Either add the LSTM aggregator type to the enum or remove this line from the documentation.

Copilot uses AI. Check for mistakes.
/// <para><b>For Beginners:</b> These are different ways to combine information from neighbors.
///
/// - Mean: Average all neighbor features (balanced, smooth)
/// - Max: Take the maximum value from neighbors (emphasizes outliers)
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Corrected 'Max' to 'MaxPool' to match the enum value name.

Suggested change
/// - Max: Take the maximum value from neighbors (emphasizes outliers)
/// - MaxPool: Take the maximum value from neighbors (emphasizes outliers)

Copilot uses AI. Check for mistakes.
// Epsilon (if learnable)
if (_learnEpsilon)
{
parameters[index++] = _epsilon;
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

This assignment to index is useless, since its value is never read.

Copilot uses AI. Check for mistakes.
// Set epsilon (if learnable)
if (_learnEpsilon)
{
_epsilon = parameters[index++];
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

This assignment to index is useless, since its value is never read.

Suggested change
_epsilon = parameters[index++];
_epsilon = parameters[index];

Copilot uses AI. Check for mistakes.
/// <summary>
/// Weight matrices for each attention head. Shape: [numHeads, inputFeatures, outputFeatures].
/// </summary>
private Tensor<T> _weights;
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Field '_weights' can be 'readonly'.

Suggested change
private Tensor<T> _weights;
private readonly Tensor<T> _weights;

Copilot uses AI. Check for mistakes.
/// <summary>
/// Attention mechanism parameters for each head. Shape: [numHeads, 2 * outputFeatures].
/// </summary>
private Matrix<T> _attentionWeights;
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Field '_attentionWeights' can be 'readonly'.

Suggested change
private Matrix<T> _attentionWeights;
private readonly Matrix<T> _attentionWeights;

Copilot uses AI. Check for mistakes.
/// <summary>
/// Bias vector for the output transformation.
/// </summary>
private Vector<T> _bias;
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Field '_bias' can be 'readonly'.

Suggested change
private Vector<T> _bias;
private readonly Vector<T> _bias;

Copilot uses AI. Check for mistakes.
Comment on lines +319 to +327
if (!NumOps.Equals(_adjacencyMatrix[b, i, j], NumOps.Zero))
{
_lastAttentionCoefficients[b, h, i, j] =
NumOps.Divide(_lastAttentionCoefficients[b, h, i, j], sumExp);
}
else
{
_lastAttentionCoefficients[b, h, i, j] = NumOps.Zero;
}
Copy link

Copilot AI Nov 8, 2025

Choose a reason for hiding this comment

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

Both branches of this 'if' statement write to the same variable - consider using '?' to express intent better.

Suggested change
if (!NumOps.Equals(_adjacencyMatrix[b, i, j], NumOps.Zero))
{
_lastAttentionCoefficients[b, h, i, j] =
NumOps.Divide(_lastAttentionCoefficients[b, h, i, j], sumExp);
}
else
{
_lastAttentionCoefficients[b, h, i, j] = NumOps.Zero;
}
_lastAttentionCoefficients[b, h, i, j] =
!NumOps.Equals(_adjacencyMatrix[b, i, j], NumOps.Zero)
? NumOps.Divide(_lastAttentionCoefficients[b, h, i, j], sumExp)
: NumOps.Zero;

Copilot uses AI. Check for mistakes.
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.

[Phase 3] Expand Graph Neural Networks Beyond GCN

3 participants