-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathexample_metapath2vec.py
More file actions
126 lines (102 loc) · 4.01 KB
/
Copy pathexample_metapath2vec.py
File metadata and controls
126 lines (102 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
"""MetaPath2Vec embedding example for heterogeneous multilayer networks.
Demonstrates how to compute MetaPath2Vec embeddings on a simple author-paper
network with two layers: 'author' and 'paper'.
Usage::
python examples/advanced/example_metapath2vec.py
# FAST
"""
from __future__ import annotations
import sys
from pathlib import Path
# Add parent directory to path if running as script
if __name__ == "__main__":
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
import numpy as np
# ---------------------------------------------------------------------------
# Build a tiny author-paper network directly with networkx (no file needed)
# ---------------------------------------------------------------------------
try:
import networkx as nx
from py3plex.core.multinet import multi_layer_network
net = multi_layer_network(directed=False)
net.add_nodes([
{"source": "Alice", "type": "author"},
{"source": "Bob", "type": "author"},
{"source": "Carol", "type": "author"},
{"source": "p1", "type": "paper"},
{"source": "p2", "type": "paper"},
])
net.add_edges([
{"source": "Alice", "target": "p1", "source_type": "author", "target_type": "paper"},
{"source": "Bob", "target": "p1", "source_type": "author", "target_type": "paper"},
{"source": "Bob", "target": "p2", "source_type": "author", "target_type": "paper"},
{"source": "Carol", "target": "p2", "source_type": "author", "target_type": "paper"},
])
USE_MULTINET = True
except Exception:
USE_MULTINET = False
# ---------------------------------------------------------------------------
# Build a minimal network-like object when full py3plex can't load
# ---------------------------------------------------------------------------
class _TinyNetwork:
"""Minimal multilayer network for the example."""
def __init__(self) -> None:
import networkx as nx
G = nx.MultiGraph()
for a in ["Alice", "Bob", "Carol"]:
G.add_node((a, "author"))
for p in ["p1", "p2"]:
G.add_node((p, "paper"))
G.add_edge(("Alice", "author"), ("p1", "paper"))
G.add_edge(("Bob", "author"), ("p1", "paper"))
G.add_edge(("Bob", "author"), ("p2", "paper"))
G.add_edge(("Carol", "author"), ("p2", "paper"))
self.core_network = G
def get_nodes(self):
return list(self.core_network.nodes())
# ---------------------------------------------------------------------------
# Run MetaPath2Vec
# ---------------------------------------------------------------------------
from py3plex.embeddings.metapath2vec import MetaPath2VecEmbedder
if USE_MULTINET:
network = net
else:
network = _TinyNetwork()
nodes = list(network.get_nodes()) # materialize once; generators are exhausted after first use
embedder = MetaPath2VecEmbedder(
metapaths=[["author", "paper", "author"]],
dim=16,
walk_length=20,
num_walks=5,
window_size=3,
epochs=5,
negative_samples=3,
seed=42,
normalize=True,
)
result = embedder.fit_transform(network, item_ids=nodes)
print(f"Embedding method : {result.method}")
print(f"Vocabulary size : {result.matrix.shape[0]} nodes embedded")
print(f"Embedding dim : {result.matrix.shape[1]}")
print(f"Seed used : {result.meta.get('seed')}")
print(f"Total training ms : {result.meta.get('total_ms', '?'):.1f}")
print()
print("Sample embeddings (first 3 nodes):")
for item_id, vec in zip(result.item_ids[:3], result.matrix[:3]):
print(f" {item_id!s:30s} norm={np.linalg.norm(vec):.4f}")
# Verify determinism
result2 = MetaPath2VecEmbedder(
metapaths=[["author", "paper", "author"]],
dim=16,
walk_length=20,
num_walks=5,
window_size=3,
epochs=5,
negative_samples=3,
seed=42,
normalize=True,
).fit_transform(network, item_ids=nodes)
if np.allclose(result.matrix, result2.matrix):
print("\n[OK] Same seed → identical vectors (deterministic)")
else:
print("\n[WARN] Vectors differ across runs with the same seed")