The world is not a monolith. HeteroGNNs allow us to model networks where entities and relationships have distinct identities and semantics, capturing the true complexity of e-commerce, social media, and knowledge graphs.
1The Semantic Schema and RGCN
Most introductory GNNs assume a Homogeneous graph where every node is the same 'Type'. However, an e-commerce graph has Users, Products, Categories, and Brands. Each of these node types has a completely different feature set (a User has an age; a Product has a price). A Heterogeneous Graph defines a schema mapping these types and their allowed interactions (e.g., User-[Purchases]->Product).
To handle this, we use the Relational GCN (RGCN) architecture. Instead of a single weight matrix for all edges, RGCN uses a different neural network weight matrix for *every edge type*. The message passed along a 'Purchases' edge is transformed differently than a message passed along a 'Reviews' edge. The model aggregates all incoming messages, grouped by edge type, to form the node's updated representation. This prevents semantic collapse.
// RGCN: Relation-Specific Message Passing
function rgcnLayer(node_i, neighbors, weights) {
let aggregated_message = zeros(hidden_dim);
// Group neighbors by relation type (r)
for (const relation_type of Object.keys(neighbors)) {
const W_r = weights[relation_type];
const type_neighbors = neighbors[relation_type];
// Transform using relation-specific weights
const r_msg = type_neighbors.map(j => W_r @ j.feats);
aggregated_message += sum(r_msg) / r_msg.length;
}
// Add self-loop and apply activation
return relu(weights.self @ node_i.feats
+ aggregated_message);
}2The Logic of Meta-paths
When traversing heterogeneous graphs, the sequence of node types you follow carries deep meaning. A Meta-path is a predefined sequence of edge types that captures a specific semantic relationship. For example, in an academic graph, the meta-path Author -> Paper -> Author identifies 'Co-authors'. The meta-path Author -> Paper -> Venue <- Paper <- Author identifies 'Authors who publish at the same conferences'.
Models like HAN (Heterogeneous Attention Network) utilize these meta-paths explicitly. Instead of passing messages indiscriminately, HAN projects the graph into multiple homogeneous 'meta-path graphs' (e.g., a graph where edges only exist between co-authors). It then runs attention over these different meta-path graphs to learn which semantic view is most important for a given task. This allows the model to inject human domain knowledge directly into the learning process.
// HAN: Meta-path Attention
// We have node embeddings from two meta-paths:
// Z1: (User-Movie-User), Z2: (User-Director-User)
function semanticAttention(Z1_node, Z2_node) {
// Learn importance of each meta-path
const w1 = computeAttentionWeight(Z1_node);
const w2 = computeAttentionWeight(Z2_node);
// Softmax normalize
const [alpha1, alpha2] = softmax([w1, w2]);
// Final fused embedding
return alpha1 * Z1_node + alpha2 * Z2_node;
}