negative_sampling
¶
Functions:
| Name | Description |
|---|---|
negative_sampling |
Samples random negative edges, i.e. pairs of nodes |
structured_negative_sampling |
Samples one negative destination for every edge of |
batched_negative_sampling |
Samples random negative edges within each graph of a batch of graphs (see |
negative_sampling
¶
negative_sampling(
edge_index: array,
num_nodes: Optional[Union[int, tuple[int, int]]] = None,
num_neg_samples: Optional[Union[int, float]] = None,
force_undirected: bool = False,
) -> array
Samples random negative edges, i.e. pairs of nodes (src, dst) with
src != dst that are not connected by an edge of edge_index.
The negative edges are drawn uniformly without replacement among all the
non-edges of the graph, so they are all different. The sampling is exact
(no rejection loop): exactly num_neg_samples edges are returned, or
all the non-edges of the graph if there are fewer than that (e.g. on
dense graphs).
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[Union[int, tuple[int, int]]]
|
Number of nodes in the graph. Inferred from |
None
|
num_neg_samples
|
Optional[Union[int, float]]
|
Number of negative edges to sample. A float is
interpreted as a ratio of the number of edges in |
None
|
force_undirected
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array
|
A |
array
|
|
Example:
edge_index = mx.array([[0, 0, 1, 2], [0, 1, 2, 3]])
negative_sampling(edge_index)
# e.g. mx.array([[3, 1, 2, 0], [1, 0, 0, 3]])
negative_sampling(edge_index, num_neg_samples=2, force_undirected=True)
# e.g. mx.array([[0, 2], [2, 0]])
Source code in mlx_graphs/utils/negative_sampling.py
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 | |
batched_negative_sampling
¶
batched_negative_sampling(
edge_index: array,
batch_indices: Union[array, tuple[array, array]],
num_neg_samples: Optional[Union[int, float]] = None,
force_undirected: bool = False,
) -> array
Samples random negative edges within each graph of a batch of graphs (see
negative_sampling()): no negative edge connects two different
graphs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge of the batch |
required |
batch_indices
|
Union[array, tuple[array, array]]
|
The graph of each node, of shape |
required |
num_neg_samples
|
Optional[Union[int, float]]
|
Number of negative edges to sample per graph. A float
is interpreted as a ratio of the number of edges of each graph.
Default: |
None
|
force_undirected
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
array
|
A |
array
|
dtype of |
Example:
edge_index = mx.array([[0, 1, 2, 3, 4, 5], [1, 2, 0, 4, 5, 3]])
batch_indices = mx.array([0, 0, 0, 1, 1, 1])
batched_negative_sampling(edge_index, batch_indices)
# e.g. mx.array([[1, 2, 0, 5, 3, 4], [0, 1, 2, 4, 5, 3]])
Source code in mlx_graphs/utils/negative_sampling.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | |
from mlx_graphs.utils import negative_sampling · mlx_graphs/utils/negative_sampling.py