dmlc--dgl
44089c8b4d
* Merge * [Graph][CUDA] Graph on GPU and many refactoring (#1791) * change edge_ids behavior and C++ impl * fix unittests; remove utils.Index in edge_id * pass mx and th tests * pass tf test * add aten::Scatter_ * Add nonzero; impl CSRGetDataAndIndices/CSRSliceMatrix * CSRGetData and CSRGetDataAndIndices passed tests * CSRSliceMatrix basic tests * fix bug in empty slice * CUDA CSRHasDuplicate * has_node; has_edge_between * predecessors, successors * deprecate send/recv; fix send_and_recv * deprecate send/recv; fix send_and_recv * in_edges; out_edges; all_edges; apply_edges * in deg/out deg * subgraph/edge_subgraph * adj * in_subgraph/out_subgraph * sample neighbors * set/get_n/e_repr * wip: working on refactoring all idtypes * pass ndata/edata tests on gpu * fix * stash * workaround nonzero issue * stash * nx conversion * test_hetero_basics except update routines * test_update_routines * test_hetero_basics for pytorch * more fixes * WIP: flatten graph * wip: flatten * test_flatten * test_to_device * fix bug in to_homo * fix bug in CSRSliceMatrix * pass subgraph test * fix send_and_recv * fix filter * test_heterograph * passed all pytorch tests * fix mx unittest * fix pytorch test_nn * fix all unittests for PyTorch * passed all mxnet tests * lint * fix tf nn test * pass all tf tests * lint * lint * change deprecation * try fix compile * lint * update METIDS * fix utest * fix * fix utests * try debug * revert * small fix * fix utests * upd * upd * upd * fix * upd * upd * upd * upd * upd * trigger * +1s * [kernel] Use heterograph index instead of unitgraph index (#1813) * upd * upd * upd * fix * upd * upd * upd * upd * upd * trigger * +1s * [Graph] Mutation for Heterograph (#1818) * mutation add_nodes and add_edges * Add support for remove_edges, remove_nodes, add_selfloop, remove_selfloop * Fix Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> * upd * upd * upd * fix * [Transfom] Mutable transform (#1833) * add nodesy * All three * Fix * lint * Add some test case * Fix * Fix * Fix * Fix * Fix * Fix * fix * triger * Fix * fix Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> * [Graph] Migrate Batch & Readout module to heterograph (#1836) * dgl.batch * unbatch * fix to device * reduce readout; segment reduce * change batch_num_nodes|edges to function * reduce readout/ softmax * broadcast * topk * fix * fix tf and mx * fix some ci * fix batch but unbatch differently * new checkk * upd * upd * upd * idtype behavior; code reorg * idtype behavior; code reorg * wip: test_basics * pass test_basics * WIP: from nx/ to nx * missing files * upd * pass test_basics:test_nx_conversion * Fix test * Fix inplace update * WIP: fixing tests * upd * pass test_transform cpu * pass gpu test_transform * pass test_batched_graph * GPU graph auto cast to int32 * missing file * stash * WIP: rgcn-hetero * Fix two datasety * upd * weird * Fix capsuley * fuck you * fuck matthias * Fix dgmg * fix bug in block degrees; pass rgcn-hetero * rgcn * gat and diffpool fix also fix ppi and tu dataset * Tree LSTM * pointcloud * rrn; wip: sgc * resolve conflicts * upd * sgc and reddit dataset * upd * Fix deepwalk, gindt and gcn * fix datasets and sign * optimization * optimization * upd * upd * Fix GIN * fix bug in add_nodes add_edges; tagcn * adaptive sampling and gcmc * upd * upd * fix geometric * fix * metapath2vec * fix agnn * fix pickling problem of block * fix utests * miss file * linegraph * upd * upd * upd * graphsage * stgcn_wave * fix hgt * on unittests * Fix transformer * Fix HAN * passed pytorch unittests * lint * fix * Fix cluster gcn * cluster-gcn is ready * on fixing block related codes * 2nd order derivative * Revert "2nd order derivative" This reverts commit 523bf6c249bee61b51b1ad1babf42aad4167f206. * passed torch utests again * fix all mxnet unittests * delete some useless tests * pass all tf cpu tests * disable * disable distributed unittest * fix * fix * lint * fix * fix * fix script * fix tutorial * fix apply edges bug * fix 2 basics * fix tutorial Co-authored-by: yzh119 <expye@outlook.com> Co-authored-by: xiang song(charlie.song) <classicxsong@gmail.com> Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-7-42.us-west-2.compute.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-1-5.us-west-2.compute.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-68-185.ec2.internal>
236 行
7.7 KiB
Python
236 行
7.7 KiB
Python
|
|
import numpy as np
|
|
from ... import utils
|
|
from ... import backend as F
|
|
from ..._ffi.function import _init_api
|
|
from ..._ffi.object import register_object, ObjectBase
|
|
from ... import ndarray
|
|
from ...base import dgl_warning
|
|
|
|
__all__ = ['random_walk',
|
|
'random_walk_with_restart',
|
|
'bipartite_single_sided_random_walk_with_restart',
|
|
'metapath_random_walk',
|
|
]
|
|
|
|
@register_object('sampler.RandomWalkTraces')
|
|
class RandomWalkTraces(ObjectBase):
|
|
pass
|
|
|
|
def random_walk(g, seeds, num_traces, num_hops):
|
|
"""**DEPRECATED**: please use :func:`dgl.sampling.random_walk` instead.
|
|
|
|
Batch-generate random walk traces on given graph with the same length.
|
|
|
|
Parameters
|
|
----------
|
|
g : DGLGraphStale
|
|
The graph.
|
|
seeds : Tensor
|
|
The node ID tensor from which the random walk traces starts.
|
|
num_traces : int
|
|
Number of traces to generate for each seed.
|
|
num_hops : int
|
|
Number of hops for each trace.
|
|
|
|
Returns
|
|
-------
|
|
traces : Tensor
|
|
A 3-dimensional node ID tensor with shape
|
|
|
|
(num_seeds, num_traces, num_hops + 1)
|
|
|
|
traces[i, j, 0] are always starting nodes (i.e. seed[i]).
|
|
"""
|
|
dgl_warning(
|
|
"This function is deprecated; please use dgl.sampling.random_walk instead",
|
|
DeprecationWarning)
|
|
if len(seeds) == 0:
|
|
return utils.toindex([]).tousertensor()
|
|
seeds = utils.toindex(seeds).todgltensor()
|
|
traces = _CAPI_DGLRandomWalk(g._graph,
|
|
seeds, int(num_traces), int(num_hops))
|
|
return F.zerocopy_from_dlpack(traces.to_dlpack())
|
|
|
|
|
|
def _split_traces(traces):
|
|
"""Splits the flattened RandomWalkTraces structure into list of list
|
|
of tensors.
|
|
|
|
Parameters
|
|
----------
|
|
traces : RandomWalkTraces
|
|
|
|
Returns
|
|
-------
|
|
traces : list[list[Tensor]]
|
|
traces[i][j] is the j-th trace generated for i-th seed.
|
|
"""
|
|
trace_counts = traces.trace_counts.asnumpy().tolist()
|
|
trace_vertices = F.zerocopy_from_dgl_ndarray(traces.vertices)
|
|
trace_vertices = F.split(
|
|
trace_vertices, traces.trace_lengths.asnumpy().tolist(), 0)
|
|
|
|
results = []
|
|
s = 0
|
|
for c in trace_counts:
|
|
results.append(trace_vertices[s:s+c])
|
|
s += c
|
|
|
|
return results
|
|
|
|
|
|
def random_walk_with_restart(
|
|
g, seeds, restart_prob, max_nodes_per_seed,
|
|
max_visit_counts=0, max_frequent_visited_nodes=0):
|
|
"""**DEPRECATED**: please use :func:`dgl.sampling.random_walk` instead.
|
|
|
|
Batch-generate random walk traces on given graph with restart probability.
|
|
|
|
Parameters
|
|
----------
|
|
g : DGLGraphStale
|
|
The graph.
|
|
seeds : Tensor
|
|
The node ID tensor from which the random walk traces starts.
|
|
restart_prob : float
|
|
Probability to stop a random walk after each step.
|
|
max_nodes_per_seed : int
|
|
Stop generating traces for a seed if the total number of nodes
|
|
visited exceeds this number. [1]
|
|
max_visit_counts : int, optional
|
|
max_frequent_visited_nodes : int, optional
|
|
Alternatively, stop generating traces for a seed if no less than
|
|
``max_frequent_visited_nodes`` are visited no less than
|
|
``max_visit_counts`` times. [1]
|
|
|
|
Returns
|
|
-------
|
|
traces : list[list[Tensor]]
|
|
traces[i][j] is the j-th trace generated for i-th seed.
|
|
|
|
Notes
|
|
-----
|
|
The traces does **not** include the seed nodes themselves.
|
|
|
|
Reference
|
|
---------
|
|
[1] Eksombatchai et al., 2017 https://arxiv.org/abs/1711.07601
|
|
"""
|
|
dgl_warning(
|
|
"This function is deprecated; please use dgl.sampling.random_walk instead",
|
|
DeprecationWarning)
|
|
if len(seeds) == 0:
|
|
return []
|
|
seeds = utils.toindex(seeds).todgltensor()
|
|
traces = _CAPI_DGLRandomWalkWithRestart(
|
|
g._graph, seeds, restart_prob, int(max_nodes_per_seed),
|
|
int(max_visit_counts), int(max_frequent_visited_nodes))
|
|
return _split_traces(traces)
|
|
|
|
|
|
def bipartite_single_sided_random_walk_with_restart(
|
|
g, seeds, restart_prob, max_nodes_per_seed,
|
|
max_visit_counts=0, max_frequent_visited_nodes=0):
|
|
"""**DEPRECATED**: please use :func:`dgl.sampling.random_walk` instead.
|
|
|
|
Batch-generate random walk traces on given graph with restart probability.
|
|
|
|
The graph must be a bipartite graph.
|
|
|
|
A single random walk step involves two normal steps, so that the "visited"
|
|
nodes always stay on the same side. [1]
|
|
|
|
Parameters
|
|
----------
|
|
g : DGLGraphStale
|
|
The graph.
|
|
seeds : Tensor
|
|
The node ID tensor from which the random walk traces starts.
|
|
restart_prob : float
|
|
Probability to stop a random walk after each step.
|
|
max_nodes_per_seed : int
|
|
Stop generating traces for a seed if the total number of nodes
|
|
visited exceeds this number. [1]
|
|
max_visit_counts : int, optional
|
|
max_frequent_visited_nodes : int, optional
|
|
Alternatively, stop generating traces for a seed if no less than
|
|
``max_frequent_visited_nodes`` are visited no less than
|
|
``max_visit_counts`` times. [1]
|
|
|
|
Returns
|
|
-------
|
|
traces : list[list[Tensor]]
|
|
traces[i][j] is the j-th trace generated for i-th seed.
|
|
|
|
Notes
|
|
-----
|
|
The current implementation does not ensure that the graph is a bipartite
|
|
graph.
|
|
|
|
The traces does **not** include the seed nodes themselves.
|
|
|
|
Reference
|
|
---------
|
|
[1] Eksombatchai et al., 2017 https://arxiv.org/abs/1711.07601
|
|
"""
|
|
dgl_warning(
|
|
"This function is deprecated; please use dgl.sampling.random_walk instead",
|
|
DeprecationWarning)
|
|
if len(seeds) == 0:
|
|
return []
|
|
seeds = utils.toindex(seeds).todgltensor()
|
|
traces = _CAPI_DGLBipartiteSingleSidedRandomWalkWithRestart(
|
|
g._graph, seeds, restart_prob, int(max_nodes_per_seed),
|
|
int(max_visit_counts), int(max_frequent_visited_nodes))
|
|
return _split_traces(traces)
|
|
|
|
|
|
def metapath_random_walk(hg, etypes, seeds, num_traces):
|
|
"""**DEPRECATED**: please use :func:`dgl.sampling.random_walk` instead.
|
|
|
|
For a single seed node, ``num_traces`` traces would be generated. A trace would
|
|
|
|
1. Start from the given seed and set ``t`` to 0.
|
|
2. Pick and traverse along edge type ``etypes[t % len(etypes)]`` from the current node.
|
|
3. If no edge can be found, halt. Otherwise, increment ``t`` and go to step 2.
|
|
|
|
Parameters
|
|
----------
|
|
hg : DGLHeteroGraph
|
|
The heterogeneous graph.
|
|
etypes : list[str or tuple of str]
|
|
Metapath, specified as a list of edge types.
|
|
The beginning and ending node type must be the same.
|
|
seeds : Tensor
|
|
The seed nodes. Node type is the same as the beginning node type of metapath.
|
|
num_traces : int
|
|
The number of traces
|
|
|
|
Returns
|
|
-------
|
|
traces : list[list[Tensor]]
|
|
traces[i][j] is the j-th trace generated for i-th seed.
|
|
traces[i][j][k] would have node type the same as the destination node type of edge
|
|
type ``etypes[k % len(etypes)]``
|
|
|
|
Notes
|
|
-----
|
|
The traces does **not** include the seed nodes themselves.
|
|
"""
|
|
dgl_warning(
|
|
"This function is deprecated; please use dgl.sampling.random_walk instead",
|
|
DeprecationWarning)
|
|
if len(etypes) == 0:
|
|
raise ValueError('empty metapath')
|
|
if hg.to_canonical_etype(etypes[0])[0] != hg.to_canonical_etype(etypes[-1])[2]:
|
|
raise ValueError('beginning and ending node type mismatch')
|
|
if len(seeds) == 0:
|
|
return []
|
|
etype_array = ndarray.array(np.asarray([hg.get_etype_id(et) for et in etypes], dtype="int64"))
|
|
seed_array = utils.toindex(seeds, hg._idtype_str).todgltensor()
|
|
traces = _CAPI_DGLMetapathRandomWalk(hg._graph, etype_array, seed_array, num_traces)
|
|
return _split_traces(traces)
|
|
|
|
_init_api('dgl.sampler.randomwalk', __name__)
|