structured_negative_sampling
¶
structured_negative_sampling(
edge_index: array,
num_nodes: Optional[int] = None,
contains_neg_self_loops: bool = True,
) -> tuple[array, array, array]
Samples one negative destination for every edge of edge_index: for
each edge (src, dst_pos), a node dst_neg such that
(src, dst_neg) is not an edge.
Each dst_neg is drawn uniformly among the nodes that are not
neighbors of src. The sampling is exact (no rejection loop), so it
also terminates on dense graphs: an edge whose source is connected to
every node has no valid negative and is left out of the result. The three
returned arrays are therefore always aligned with each other, and they
are aligned with edge_index whenever every source has at least one
non-neighbor.
Randomness comes from a NumPy generator seeded from mx.random, so
results are reproducible with mx.random.seed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge |
required |
num_nodes
|
Optional[int]
|
Number of nodes in the graph. Inferred from |
None
|
contains_neg_self_loops
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
the dtype of |
Example:
edge_index = mx.array([[0, 0, 1, 2], [0, 1, 2, 3]])
src, dst_pos, dst_neg = structured_negative_sampling(edge_index)
# src: mx.array([0, 0, 1, 2])
# dst_pos: mx.array([0, 1, 2, 3])
# dst_neg: e.g. mx.array([3, 2, 0, 1])
Source code in mlx_graphs/utils/negative_sampling.py
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
from mlx_graphs.utils import structured_negative_sampling · mlx_graphs/utils/negative_sampling.py