GATConv
¶
GATConv(
node_features_dim: Union[int, Tuple[int, int]],
out_features_dim: int,
heads: int = 1,
concat: bool = True,
bias: bool = True,
negative_slope: float = 0.2,
dropout: float = 0.0,
edge_features_dim: Optional[int] = None,
add_self_loops: bool = True,
fill_value: SelfLoopFillValue = "mean",
**kwargs,
)
Bases: MessagePassing
Graph Attention Network convolution layer from "Graph Attention Networks" paper.
The attention coefficients are computed per edge, but the weighted sum
over neighbors runs as one sparse x dense matmul per head, so the
[num_edges, heads, out_features_dim] message array is never built.
The layer also works on bipartite graphs (e.g. author -> paper in a
heterogeneous graph): pass node_features as a (src_features,
dst_features) tuple. With an integer node_features_dim both sides
share one projection, as in PyG; with a (src_dim, dst_dim) tuple they
get separate projections lin_src_proj and lin_dst_proj. The
output has one row per destination node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_features_dim
|
Union[int, Tuple[int, int]]
|
Size of input node features, or a
|
required |
out_features_dim
|
int
|
Size of output node embeddings |
required |
heads
|
int
|
Number of attention heads |
1
|
concat
|
bool
|
Whether to use concat of heads or mean reduction |
True
|
bias
|
bool
|
Whether to use bias in the node projection |
True
|
negative_slope
|
float
|
Slope for the leaky relu |
0.2
|
dropout
|
float
|
Probability p for dropout |
0.0
|
edge_features_dim
|
Optional[int]
|
Size of edge features |
None
|
add_self_loops
|
bool
|
Whether to add a self-loop to every node, as in PyG
(existing self-loops are replaced by it). Not supported on
bipartite |
True
|
fill_value
|
SelfLoopFillValue
|
Edge features of the added self-loops, if
|
'mean'
|
Example:
import mlx.core as mx
from mlx_graphs.nn import GATConv
conv = GATConv(16, 32, heads=2, concat=True)
edge_index = mx.array([[0, 1, 2, 3, 4], [0, 0, 1, 1, 3]])
node_features = mx.random.uniform(low=0, high=1, shape=(5, 16))
h = conv(edge_index, node_features)
>>> h.shape
(5, 64)
Methods:
| Name | Description |
|---|---|
__call__ |
Computes the forward pass of GATConv. |
Source code in mlx_graphs/nn/conv/gat_conv.py
__call__
¶
__call__(
edge_index: array,
node_features: Union[array, Tuple[array, array]],
edge_features: Optional[array] = None,
) -> array
Computes the forward pass of GATConv.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
input edge index of shape |
required |
node_features
|
Union[array, Tuple[array, array]]
|
input node features, or a |
required |
edge_features
|
Optional[array]
|
edge features |
None
|
Returns:
| Type | Description |
|---|---|
array
|
computed node embeddings, one row per destination node |
Source code in mlx_graphs/nn/conv/gat_conv.py
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 | |
from mlx_graphs.nn import GATConv · mlx_graphs/nn/conv/gat_conv.py