Hetero API¶
Generated reference for heterogeneous graphs — multiple node types and relations (typed edges). Start with the overview (academic author / paper / institution walkthrough) and examples.
Model zoo acronyms (R-GCN, GraphSAGE, HAN, HGT, CompGCN) are spelled out in the model zoo table.
anytensor.hetero
¶
Heterogeneous graph types and tree batch/unbatch hooks.
Not part of the jraph-mirroring API. Import from here::
from anytensor.hetero import HeteroGraphsTuple, multi_update_all
from anytensor.hetero import relational_graph_convolution
HeteroGraphsTuple
¶
Bases: NamedTuple
Heterogeneous graph(s) with per-type node pools and per-relation incidence.
Node ids in senders / receivers for etype (src, rel, dst) are
local to nodes[src] and nodes[dst] (not a global node pool).
n_node[ntype] and n_edge[etype] are integer vectors of length
n_graphs (jraph-style batching within one object).
:func:anytensor.tree.batch requires every input to share the same keys.
Source code in anytensor/hetero/graph.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
update
¶
update(
nodes=None,
edges=None,
senders=None,
receivers=None,
n_node=None,
n_edge=None,
globals=_UNSET,
)
Return a new graph with shallow-merged mapping fields.
Only keys present in the update dicts are replaced; other keys are
kept. Pass globals=... to replace globals (including with None).
Source code in anytensor/hetero/graph.py
iter_relations
¶
Yield aliasing :class:SendRecvTuple views for each relation.
Source code in anytensor/hetero/graph.py
relation_view
¶
Aliasing send→recv view for one canonical etype.
Source code in anytensor/hetero/graph.py
SendRecvTuple
¶
Bases: NamedTuple
One-way send→receive incidence (homo or bipartite).
Views alias parent storage: nodes_send / nodes_recv may be the
same object (homo) or two ntype pools (hetero relation).
Source code in anytensor/hetero/graph.py
RelationSpec
¶
Bases: NamedTuple
Per-relation update for :func:multi_update_all.
Attributes:
| Name | Type | Description |
|---|---|---|
message_fn |
MessageFn
|
|
reduce |
ReduceName
|
Segment reduce after optional attention ( |
attention_logit_fn |
Optional[AttentionLogitFn]
|
Optional |
attention_reduce_fn |
Optional[AttentionReduceFn]
|
Optional |
Source code in anytensor/hetero/message.py
graphs_tuple_as_send_recv
¶
View a jraph :class:~anytensor.jraph.GraphsTuple as send→recv (aliased pools).
Source code in anytensor/hetero/graph.py
key_schema
¶
Sorted (ntypes, etypes) from present keys (empties still count).
schemas_equal
¶
attention_weight_messages
¶
Default attention reduce: element-wise messages * weights.
Same pattern as Graph Attention Networks (GAT): after
:func:~anytensor.segment.segment_softmax, multiply messages by the
per-edge weights.
Source code in anytensor/hetero/message.py
copy_u_message
¶
multi_update_all
¶
Multi-relation update aligned with DGL multi_update_all.
Per etype: message + optional attention + segment-reduce onto destination
nodes. Then fuse mailboxes that share a destination ntype with
cross_reducer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
HeteroGraphsTuple
|
Heterogeneous graph(s). |
required |
etype_dict
|
Optional[Mapping[CanonicalEtype, Union[MessageFn, RelationSpec, tuple]]]
|
Optional map |
None
|
cross_reducer
|
CrossReduceName
|
Fuse per-relation mailboxes for the same destination
ntype. |
'sum'
|
reduce
|
ReduceName
|
Default per-relation segment reduce when not set in
|
'sum'
|
etypes
|
Optional[Sequence[CanonicalEtype]]
|
Subset of relations when |
None
|
Apply is @cache (same pattern as GraphNetwork).
Returns:
| Type | Description |
|---|---|
HeteroGraphsTuple
|
A new :class: |
HeteroGraphsTuple
|
replaced by the cross-reduced mailboxes (same as DGL writing the |
HeteroGraphsTuple
|
reduced feature). Source-only ntypes are unchanged. |
Source code in anytensor/hetero/message.py
relation_mailbox
¶
relation_mailbox(
graph,
etype,
*,
message_fn=copy_u_message,
reduce="sum",
attention_logit_fn=None,
attention_reduce_fn=None,
)
Per-relation messages reduced onto destination nodes (DGL type-wise step).
With :func:copy_u_message, reduce matches DGL fn.copy_u +
fn.sum/fn.mean/fn.max/fn.min. Empty destinations are 0
(including max/min via segment_*_or_constant).
When attention_logit_fn is set, logits are softmax-normalized per
destination (receivers) and attention_reduce_fn weights messages
before the segment reduce — same flow as
:func:anytensor.jraph.GraphNetwork attention. Omit
attention_reduce_fn to default to :func:attention_weight_messages.
With attention, prefer reduce="sum". Apply is @cache (same
pattern as GraphNetwork).
Source code in anytensor/hetero/message.py
comp_gcn
¶
comp_gcn(
graph,
relation_apply,
self_apply,
*,
composition="mult",
activation=_relu,
reducer="sum",
)
CompGCN (Composition-based Multi-Relational GCN) layer.
Vashishth et al., ICLR 2020. Requires edge features on each used etype.
mult—relation_apply[r](h_src * e)sum—relation_apply[r](h_src + e)
Then segment reducer, cross sum, and
activation(self_apply[n](h) + mailbox).
Source code in anytensor/hetero/models.py
gat_attention_logit
¶
GAT-style edge score: LeakyReLU(attn_vec_apply(concat(src, dst))).
Graph Attention Network (GAT; Veličković et al., ICLR 2018) neighborhood
scoring. attn_vec_apply maps concatenated features to shape (E, 1)
or (E,). Typical use inside a logit callable::
lambda s, d, e: gat_attention_logit(s, d, my_linear)
Source code in anytensor/hetero/models.py
han
¶
han(
graph,
meta_path_etypes,
node_message,
node_attention_logit,
semantic_project,
semantic_query,
*,
node_activation=_relu,
semantic_activation=_tanh,
)
HAN (Heterogeneous Graph Attention Network) layer.
Wang et al., WWW 2019: node-level + semantic attention. Each
meta_path_etypes entry is a meta-path hop already stored as a
canonical etype (precompute longer paths as their own etypes).
- Node-level attention —
node_message[e](h_src), logits fromnode_attention_logit[e](src, dst, edges), softmax over neighbors (GAT-style). - Mailboxes stacked; semantic attention mixes path embeddings
with
semantic_queryaftersemantic_project. Apply is@cache(same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
hetero_sage
¶
Heterogeneous GraphSAGE mean layer (Hamilton et al., NeurIPS 2017).
GraphSAGE (SAmple and aggreGatE): per-relation map on sources, mean
aggregate, cross sum, then
activation(combine_apply[n](concat[h_self, mailbox])).
Apply is @cache (same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
hgt
¶
HGT (Heterogeneous Graph Transformer) style layer.
Hu et al., WWW 2020 — typed attention + target projection. Full HGT uses typed query/key/value and edge-type matrices (often multi-head). Fold those into the callables you pass:
message_apply[etype](h_src)— value / message projection.attention_logit[etype](src, dst, edges)— edge logits (include1/sqrt(d)here, or setscale).- Softmax over neighbors, weighted sum, cross
sumacross etypes. target_apply[ntype]— target-type output projection. Apply is@cache(same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
relational_graph_convolution
¶
relational_graph_convolution(
graph,
relation_apply,
self_apply,
*,
activation=_relu,
reducer="mean",
)
R-GCN (Relational Graph Convolutional Network) layer.
Schlichtkrull et al., ESWC 2018. For each relation r, messages are
relation_apply[r](h_src), neighborhood-aggregated with reducer
(mean ≈ 1/|N_r(i)|), then cross-summed. Destinations update as
activation(self_apply[n](h) + mailbox).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
HeteroGraphsTuple
|
Input heterograph. |
required |
relation_apply
|
Mapping[CanonicalEtype, LinearFn]
|
Per-etype |
required |
self_apply
|
Mapping[Ntype, LinearFn]
|
Per-ntype self / root term ( |
required |
activation
|
ActivationFn
|
Pointwise nonlinearity (default ReLU). |
_relu
|
reducer
|
str
|
Per-relation segment reduce ( |
'mean'
|
Source code in anytensor/hetero/models.py
anytensor.hetero.models
¶
Heterogeneous GNN layers as plain functions on :class:HeteroGraphsTuple.
Each function takes a graph plus callables / arrays for the learnable pieces.
Weight ownership stays in your framework (Flax, Haiku, torch.nn, NumPy
prototypes) — pass lambda x: x @ W or a module __call__ as needed.
Citations (acronym → full name)¶
- R-GCN (Relational Graph Convolutional Network) — Schlichtkrull et al., “Modeling Relational Data with Graph Convolutional Networks,” ESWC 2018. https://arxiv.org/abs/1703.06103
- GraphSAGE (SAmple and aggreGatE; hetero wrap) — Hamilton et al., “Inductive Representation Learning on Large Graphs,” NeurIPS 2017. https://arxiv.org/abs/1706.02216
- HAN (Heterogeneous Graph Attention Network) — Wang et al., WWW 2019. https://arxiv.org/abs/1903.07293
- HGT (Heterogeneous Graph Transformer) — Hu et al., WWW 2020. https://arxiv.org/abs/2003.01332
- CompGCN (Composition-based Multi-Relational GCN) — Vashishth et al., ICLR 2020. https://arxiv.org/abs/1911.03082
- GAT (Graph Attention Network) edge scores — Veličković et al., ICLR 2018.
https://arxiv.org/abs/1710.10903 (used by :func:
gat_attention_logit/ HAN)
relational_graph_convolution
¶
relational_graph_convolution(
graph,
relation_apply,
self_apply,
*,
activation=_relu,
reducer="mean",
)
R-GCN (Relational Graph Convolutional Network) layer.
Schlichtkrull et al., ESWC 2018. For each relation r, messages are
relation_apply[r](h_src), neighborhood-aggregated with reducer
(mean ≈ 1/|N_r(i)|), then cross-summed. Destinations update as
activation(self_apply[n](h) + mailbox).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
HeteroGraphsTuple
|
Input heterograph. |
required |
relation_apply
|
Mapping[CanonicalEtype, LinearFn]
|
Per-etype |
required |
self_apply
|
Mapping[Ntype, LinearFn]
|
Per-ntype self / root term ( |
required |
activation
|
ActivationFn
|
Pointwise nonlinearity (default ReLU). |
_relu
|
reducer
|
str
|
Per-relation segment reduce ( |
'mean'
|
Source code in anytensor/hetero/models.py
hetero_sage
¶
Heterogeneous GraphSAGE mean layer (Hamilton et al., NeurIPS 2017).
GraphSAGE (SAmple and aggreGatE): per-relation map on sources, mean
aggregate, cross sum, then
activation(combine_apply[n](concat[h_self, mailbox])).
Apply is @cache (same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
comp_gcn
¶
comp_gcn(
graph,
relation_apply,
self_apply,
*,
composition="mult",
activation=_relu,
reducer="sum",
)
CompGCN (Composition-based Multi-Relational GCN) layer.
Vashishth et al., ICLR 2020. Requires edge features on each used etype.
mult—relation_apply[r](h_src * e)sum—relation_apply[r](h_src + e)
Then segment reducer, cross sum, and
activation(self_apply[n](h) + mailbox).
Source code in anytensor/hetero/models.py
han
¶
han(
graph,
meta_path_etypes,
node_message,
node_attention_logit,
semantic_project,
semantic_query,
*,
node_activation=_relu,
semantic_activation=_tanh,
)
HAN (Heterogeneous Graph Attention Network) layer.
Wang et al., WWW 2019: node-level + semantic attention. Each
meta_path_etypes entry is a meta-path hop already stored as a
canonical etype (precompute longer paths as their own etypes).
- Node-level attention —
node_message[e](h_src), logits fromnode_attention_logit[e](src, dst, edges), softmax over neighbors (GAT-style). - Mailboxes stacked; semantic attention mixes path embeddings
with
semantic_queryaftersemantic_project. Apply is@cache(same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
hgt
¶
HGT (Heterogeneous Graph Transformer) style layer.
Hu et al., WWW 2020 — typed attention + target projection. Full HGT uses typed query/key/value and edge-type matrices (often multi-head). Fold those into the callables you pass:
message_apply[etype](h_src)— value / message projection.attention_logit[etype](src, dst, edges)— edge logits (include1/sqrt(d)here, or setscale).- Softmax over neighbors, weighted sum, cross
sumacross etypes. target_apply[ntype]— target-type output projection. Apply is@cache(same pattern as GraphNetwork).
Source code in anytensor/hetero/models.py
gat_attention_logit
¶
GAT-style edge score: LeakyReLU(attn_vec_apply(concat(src, dst))).
Graph Attention Network (GAT; Veličković et al., ICLR 2018) neighborhood
scoring. attn_vec_apply maps concatenated features to shape (E, 1)
or (E,). Typical use inside a logit callable::
lambda s, d, e: gat_attention_logit(s, d, my_linear)