dmlc--dgl
6066fee935
* random walk traces generation * remove outdated comments * oops put in the wrong place * explicit inline * moving rand_r to util * pinsage-like model on movielens * the code runs now * support cuda * using readonly graph * moving random walk to public function * per-thread seed and openmp support * pinsage-like model on movielens * the code runs now * support cuda * using readonly graph * using C random walk * removing profile decorators * param initialization * no grad * leaky relu fixes everything * train and save * WIP * WIP * WIP * seems to work * evaluation output * swapping order of val/test and train * debug * hyperparam tuning * prior/training dataset split changes * random walk reorg * random walk with restart * signed comparison fix * migrating random walk to nodeflow * Revert "migrating random walk to nodeflow" This reverts commit f2565347cced7c912a58a529b257c033d9f375b7. * add README and remove dataset * new endpoint * lint * lint x2 * oops forgot test * including bpr - better for baseline * addressing fixes * throwing random walks out from SamplerOp class * forgot to move RandomWalk; why did this even work? * removing legacy garbage * add todo * address comments * stupid bug fix * call ndarrayvector converter to handle traces
63 行
2.0 KiB
Python
63 行
2.0 KiB
Python
import dgl
|
|
from dgl import utils
|
|
import backend as F
|
|
import numpy as np
|
|
|
|
def test_random_walk():
|
|
edge_list = [(0, 1), (1, 2), (2, 3), (3, 4),
|
|
(4, 3), (3, 2), (2, 1), (1, 0)]
|
|
seeds = [0, 1]
|
|
n_traces = 3
|
|
n_hops = 4
|
|
|
|
g = dgl.DGLGraph(edge_list, readonly=True)
|
|
traces = dgl.contrib.sampling.random_walk(g, seeds, n_traces, n_hops)
|
|
traces = F.zerocopy_to_numpy(traces)
|
|
|
|
assert traces.shape == (len(seeds), n_traces, n_hops + 1)
|
|
|
|
for i, seed in enumerate(seeds):
|
|
assert (traces[i, :, 0] == seeds[i]).all()
|
|
|
|
trace_diff = np.diff(traces, axis=-1)
|
|
# only nodes with adjacent IDs are connected
|
|
assert (np.abs(trace_diff) == 1).all()
|
|
|
|
def test_random_walk_with_restart():
|
|
edge_list = [(0, 1), (1, 2), (2, 3), (3, 4),
|
|
(4, 3), (3, 2), (2, 1), (1, 0)]
|
|
seeds = [0, 1]
|
|
max_nodes = 10
|
|
|
|
g = dgl.DGLGraph(edge_list)
|
|
|
|
# test normal RWR
|
|
traces = dgl.contrib.sampling.random_walk_with_restart(g, seeds, 0.2, max_nodes)
|
|
assert len(traces) == len(seeds)
|
|
for traces_per_seed in traces:
|
|
total_nodes = 0
|
|
for t in traces_per_seed:
|
|
total_nodes += len(t)
|
|
trace_diff = np.diff(F.zerocopy_to_numpy(t), axis=-1)
|
|
assert (np.abs(trace_diff) == 1).all()
|
|
assert total_nodes >= max_nodes
|
|
|
|
# test RWR with early stopping
|
|
traces = dgl.contrib.sampling.random_walk_with_restart(
|
|
g, seeds, 1, 100, max_nodes, 1)
|
|
assert len(traces) == len(seeds)
|
|
for traces_per_seed in traces:
|
|
assert sum(len(t) for t in traces_per_seed) < 100
|
|
|
|
# test bipartite RWR
|
|
traces = dgl.contrib.sampling.bipartite_single_sided_random_walk_with_restart(
|
|
g, seeds, 0.2, max_nodes)
|
|
assert len(traces) == len(seeds)
|
|
for traces_per_seed in traces:
|
|
for t in traces_per_seed:
|
|
trace_diff = np.diff(F.zerocopy_to_numpy(t), axis=-1)
|
|
assert (trace_diff % 2 == 0).all()
|
|
|
|
if __name__ == '__main__':
|
|
test_random_walk()
|