Skip to content

Commit 432094f

Browse files
committed
fix line edge sampling probabilities
1 parent 1027d10 commit 432094f

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

ge/models/line.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,10 @@ def _gen_sampling_table(self):
131131
self.node_accept, self.node_alias = create_alias_table(norm_prob)
132132

133133
# create sampling table for edge
134-
numEdges = self.graph.number_of_edges()
135134
total_sum = sum([self.graph[edge[0]][edge[1]].get('weight', 1.0)
136135
for edge in self.graph.edges()])
137-
norm_prob = [self.graph[edge[0]][edge[1]].get('weight', 1.0) *
138-
numEdges / total_sum for edge in self.graph.edges()]
136+
norm_prob = [self.graph[edge[0]][edge[1]].get('weight', 1.0) /
137+
total_sum for edge in self.graph.edges()]
139138

140139
self.edge_accept, self.edge_alias = create_alias_table(norm_prob)
141140

tests/line_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,42 @@
55

66
pytest.importorskip("tensorflow")
77
from ge import LINE
8+
from ge.models import line as line_module
9+
from ge.utils import preprocess_nxgraph
810

911
TEST_GRAPH_PATH = Path(__file__).resolve().parent / "Wiki_edgelist.txt"
1012

1113

14+
def test_LINE_sampling_tables_use_normalized_probabilities(monkeypatch):
15+
graph = nx.DiGraph()
16+
graph.add_edge("a", "b", weight=2)
17+
graph.add_edge("a", "c", weight=6)
18+
19+
model = LINE.__new__(LINE)
20+
model.graph = graph
21+
model.idx2node, model.node2idx = preprocess_nxgraph(graph)
22+
model.node_size = graph.number_of_nodes()
23+
24+
sampling_tables = []
25+
26+
def record_sampling_table(area_ratio):
27+
sampling_tables.append(list(area_ratio))
28+
return [1] * len(area_ratio), [0] * len(area_ratio)
29+
30+
monkeypatch.setattr(line_module, "create_alias_table", record_sampling_table)
31+
32+
LINE._gen_sampling_table(model)
33+
34+
node_probs, edge_probs = sampling_tables
35+
expected_edge_probs = [
36+
graph[edge[0]][edge[1]]["weight"] / 8 for edge in graph.edges()
37+
]
38+
39+
assert sum(node_probs) == pytest.approx(1)
40+
assert sum(edge_probs) == pytest.approx(1)
41+
assert edge_probs == pytest.approx(expected_edge_probs)
42+
43+
1244
def test_LINE():
1345
graph = nx.read_edgelist(
1446
str(TEST_GRAPH_PATH),

0 commit comments

Comments
 (0)