HeterogeneousGraphLayer<T>
Layers · AiDotNet.NeuralNetworks.Layers
Implements Heterogeneous Graph Neural Network layer for graphs with multiple node and edge types.
Inheritance: LayerBase<T> → HeterogeneousGraphLayer<T>
Implements: IGraphConvolutionLayer<T>, IShapeContract
For Beginners
This layer handles graphs where not all nodes and edges are the same.
Real-world examples:
Knowledge Graph:
- Node types: Person, Place, Event
- Edge types: BornIn, HappenedAt, AttendedBy
- Each type needs different processing
E-commerce:
- Node types: User, Product, Brand, Category
- Edge types: Purchased, Manufactured, BelongsTo, Viewed
- Different relationships have different meanings
Academic Network:
- Node types: Author, Paper, Venue, Topic
- Edge types: Wrote, PublishedIn, About, Cites
- Mixed types of entities and relationships
Why heterogeneous?
- Different semantics: A “User” has different properties than a “Product”
- Type-specific patterns: Relationships mean different things
- Better representation: Specialized processing for each type
The layer learns separate transformations for each edge type, then combines them intelligently.
How It Works
Heterogeneous Graph Neural Networks (HGNNs) handle graphs where nodes and edges have different types. Unlike homogeneous GNNs that treat all nodes and edges uniformly, HGNNs use type-specific transformations and aggregations. This layer implements the R-GCN (Relational GCN) approach with type-specific weight matrices.
The layer computes: h_i’ = σ(Σ_{r∈R} Σ_{j∈N_r(i)} (1/c_{i,r}) W_r h_j + W_0 h_i) where R is the set of relation types, N_r(i) are neighbors of type r, c_{i,r} is a normalization constant, W_r are relation-specific weights, and W_0 is the self-loop weight.
Constructors
| Constructor | Summary |
|---|---|
HeterogeneousGraphLayer(HeterogeneousGraphMetadata metadata, int outputFeatures, bool useBasis, int numBases, IActivationFunction<T> activationFunction) |
Initializes a new instance of the HeterogeneousGraphLayer class. |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
|
int InputFeatures { get; set; } |
|
int OutputFeatures { get; } |
Methods
| Method | Summary |
|---|---|
IReadOnlyList<OutputAxisContract> OutputAxesFor(int inputRank) |
|
List<Tensor<T>> GetParameterTensors() |
Gets all trainable parameters of the layer as a list of tensors. |
Tensor<T> GetAdjacencyMatrix() |
|
void ResetState() |
|
void SetAdjacencyMatrices(Dictionary<string, Tensor<T>> adjacencyMatrices) |
Sets the adjacency matrices for all edge types. |
void SetAdjacencyMatrix(Tensor<T> adjacencyMatrix) |
|
void SetNodeTypeMap(Dictionary<int, string> nodeTypeMap) |
Sets the node type mapping. |
void SetParameterTensors(List<Tensor<T>> parameters) |
Sets the trainable parameters of the layer from a list of tensors. |
void UpdateParameters(T learningRate) |
Updates the layer parameters based on computed gradients. |