"docker/vscode:/vscode.git/clone" did not exist on "fe5f035f797a5fa663a98030c9d0ec2f982cd09d"
test_function.py 2.15 KB
Newer Older
1
2
3
4
5
6
import torch as th
import dgl
import dgl.function as fn

def generate_graph():
    g = dgl.DGLGraph()
Minjie Wang's avatar
Minjie Wang committed
7
    g.add_nodes(10) # 10 nodes.
Lingfan Yu's avatar
Lingfan Yu committed
8
    h = th.arange(1, 11, dtype=th.float)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    g.set_n_repr({'h': h})
    # create a graph where 0 is the source and 9 is the sink
    for i in range(1, 9):
        g.add_edge(0, i)
        g.add_edge(i, 9)
    # add a back flow from 9 to 0
    g.add_edge(9, 0)
    h = th.tensor([1., 2., 1., 3., 1., 4., 1., 5., 1., 6.,\
            1., 7., 1., 8., 1., 9., 10.])
    g.set_e_repr({'h' : h})
    return g

def generate_graph1():
    """graph with anonymous repr"""
    g = dgl.DGLGraph()
Minjie Wang's avatar
Minjie Wang committed
24
    g.add_nodes(10) # 10 nodes.
Lingfan Yu's avatar
Lingfan Yu committed
25
26
    h = th.arange(1, 11, dtype=th.float)
    h = th.arange(1, 11, dtype=th.float)
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
    g.set_n_repr(h)
    # create a graph where 0 is the source and 9 is the sink
    for i in range(1, 9):
        g.add_edge(0, i)
        g.add_edge(i, 9)
    # add a back flow from 9 to 0
    g.add_edge(9, 0)
    h = th.tensor([1., 2., 1., 3., 1., 4., 1., 5., 1., 6.,\
            1., 7., 1., 8., 1., 9., 10.])
    g.set_e_repr(h)
    return g

def reducer_both(node, msgs):
    return {'h' : th.sum(msgs['m'], 1)}

def test_copy_src():
    # copy_src with both fields
    g = generate_graph()
Minjie Wang's avatar
Minjie Wang committed
45
46
    g.register_message_func(fn.copy_src(src='h', out='m'))
    g.register_reduce_func(reducer_both)
47
48
49
50
51
52
53
    g.update_all()
    assert th.allclose(g.get_n_repr()['h'],
            th.tensor([10., 1., 1., 1., 1., 1., 1., 1., 1., 44.]))

def test_copy_edge():
    # copy_edge with both fields
    g = generate_graph()
Minjie Wang's avatar
Minjie Wang committed
54
55
    g.register_message_func(fn.copy_edge(edge='h', out='m'))
    g.register_reduce_func(reducer_both)
56
57
58
59
60
61
62
    g.update_all()
    assert th.allclose(g.get_n_repr()['h'],
            th.tensor([10., 1., 1., 1., 1., 1., 1., 1., 1., 44.]))

def test_src_mul_edge():
    # src_mul_edge with all fields
    g = generate_graph()
Minjie Wang's avatar
Minjie Wang committed
63
64
    g.register_message_func(fn.src_mul_edge(src='h', edge='h', out='m'))
    g.register_reduce_func(reducer_both)
65
66
67
68
69
70
71
72
    g.update_all()
    assert th.allclose(g.get_n_repr()['h'],
            th.tensor([100., 1., 1., 1., 1., 1., 1., 1., 1., 284.]))

if __name__ == '__main__':
    test_copy_src()
    test_copy_edge()
    test_src_mul_edge()