test_nn.py 5.48 KB
Newer Older
1
2
3
4
5
import mxnet as mx
import networkx as nx
import numpy as np
import dgl
import dgl.nn.mxnet as nn
6
from mxnet import autograd, gluon
7

8
9
def check_close(a, b):
    assert np.allclose(a.asnumpy(), b.asnumpy(), rtol=1e-4, atol=1e-4)
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

def _AXWb(A, X, W, b):
    X = mx.nd.dot(X, W.data(X.context))
    Y = mx.nd.dot(A, X.reshape(X.shape[0], -1)).reshape(X.shape)
    return Y + b.data(X.context)

def test_graph_conv():
    g = dgl.DGLGraph(nx.path_graph(3))
    adj = g.adjacency_matrix()
    ctx = mx.cpu(0)

    conv = nn.GraphConv(5, 2, norm=False, bias=True)
    conv.initialize(ctx=ctx)
    # test#1: basic
    h0 = mx.nd.ones((3, 5))
    h1 = conv(h0, g)
26
27
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
28
    check_close(h1, _AXWb(adj, h0, conv.weight, conv.bias))
29
30
31
    # test#2: more-dim
    h0 = mx.nd.ones((3, 5, 5))
    h1 = conv(h0, g)
32
33
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
34
    check_close(h1, _AXWb(adj, h0, conv.weight, conv.bias))
35
36
37
38
39
40
41

    conv = nn.GraphConv(5, 2)
    conv.initialize(ctx=ctx)

    # test#3: basic
    h0 = mx.nd.ones((3, 5))
    h1 = conv(h0, g)
42
43
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
44
45
46
    # test#4: basic
    h0 = mx.nd.ones((3, 5, 5))
    h1 = conv(h0, g)
47
48
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
49
50
51
52
53
54
55
56

    conv = nn.GraphConv(5, 2)
    conv.initialize(ctx=ctx)

    with autograd.train_mode():
        # test#3: basic
        h0 = mx.nd.ones((3, 5))
        h1 = conv(h0, g)
57
58
        assert len(g.ndata) == 0
        assert len(g.edata) == 0
59
60
61
        # test#4: basic
        h0 = mx.nd.ones((3, 5, 5))
        h1 = conv(h0, g)
62
63
        assert len(g.ndata) == 0
        assert len(g.edata) == 0
64

65
66
    # test not override features
    g.ndata["h"] = 2 * mx.nd.ones((3, 1))
67
    h1 = conv(h0, g)
68
69
70
    assert len(g.ndata) == 1
    assert len(g.edata) == 0
    assert "h" in g.ndata
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    check_close(g.ndata['h'], 2 * mx.nd.ones((3, 1)))

def test_set2set():
    g = dgl.DGLGraph(nx.path_graph(10))

    s2s = nn.Set2Set(5, 3, 3) # hidden size 5, 3 iters, 3 layers
    print(s2s)

    # test#1: basic
    h0 = mx.nd.random.randn(g.number_of_nodes(), 5)
    h1 = s2s(h0, g)
    assert h1.shape[0] == 10 and h1.ndim == 1

    # test#2: batched graph
    bg = dgl.batch([g, g, g])
    h0 = mx.nd.random.randn(bg.number_of_nodes(), 5)
    h1 = s2s(h0, bg)
    assert h1.shape[0] == 3 and h1.shape[1] == 10 and h1.ndim == 2

def test_glob_att_pool():
    g = dgl.DGLGraph(nx.path_graph(10))

    gap = nn.GlobalAttentionPooling(gluon.nn.Dense(1), gluon.nn.Dense(10))
    print(gap)
    # test#1: basic
    h0 = mx.nd.random.randn(g.number_of_nodes(), 5)
    h1 = gap(h0, g)
    assert h1.shape[0] == 10 and h1.ndim == 1

    # test#2: batched graph
    bg = dgl.batch([g, g, g, g])
    h0 = mx.nd.random.randn(bg.number_of_nodes(), 5)
    h1 = gap(h0, bg)
    assert h1.shape[0] == 4 and h1.shape[1] == 10 and h1.ndim == 2

def test_simple_pool():
    g = dgl.DGLGraph(nx.path_graph(15))

    sum_pool = nn.SumPooling()
    avg_pool = nn.AvgPooling()
    max_pool = nn.MaxPooling()
    sort_pool = nn.SortPooling(10) # k = 10
    print(sum_pool, avg_pool, max_pool, sort_pool)

    # test#1: basic
    h0 = mx.nd.random.randn(g.number_of_nodes(), 5)
    h1 = sum_pool(h0, g)
    check_close(h1, mx.nd.sum(h0, 0))
    h1 = avg_pool(h0, g)
    check_close(h1, mx.nd.mean(h0, 0))
    h1 = max_pool(h0, g)
    check_close(h1, mx.nd.max(h0, 0))
    h1 = sort_pool(h0, g)
    assert h1.shape[0] == 10 * 5 and h1.ndim == 1

    # test#2: batched graph
    g_ = dgl.DGLGraph(nx.path_graph(5))
    bg = dgl.batch([g, g_, g, g_, g])
    h0 = mx.nd.random.randn(bg.number_of_nodes(), 5)
    h1 = sum_pool(h0, bg)
    truth = mx.nd.stack(mx.nd.sum(h0[:15], 0),
                        mx.nd.sum(h0[15:20], 0),
                        mx.nd.sum(h0[20:35], 0),
                        mx.nd.sum(h0[35:40], 0),
                        mx.nd.sum(h0[40:55], 0), axis=0)
    check_close(h1, truth)

    h1 = avg_pool(h0, bg)
    truth = mx.nd.stack(mx.nd.mean(h0[:15], 0),
                        mx.nd.mean(h0[15:20], 0),
                        mx.nd.mean(h0[20:35], 0),
                        mx.nd.mean(h0[35:40], 0),
                        mx.nd.mean(h0[40:55], 0), axis=0)
    check_close(h1, truth)

    h1 = max_pool(h0, bg)
    truth = mx.nd.stack(mx.nd.max(h0[:15], 0),
                        mx.nd.max(h0[15:20], 0),
                        mx.nd.max(h0[20:35], 0),
                        mx.nd.max(h0[35:40], 0),
                        mx.nd.max(h0[40:55], 0), axis=0)
    check_close(h1, truth)

    h1 = sort_pool(h0, bg)
    assert h1.shape[0] == 5 and h1.shape[1] == 10 * 5 and h1.ndim == 2

157

158
159
160
161
162
163
164
165
166
167
def uniform_attention(g, shape):
    a = mx.nd.ones(shape)
    target_shape = (g.number_of_edges(),) + (1,) * (len(shape) - 1)
    return a / g.in_degrees(g.edges()[1]).reshape(target_shape).astype('float32')

def test_edge_softmax():
    # Basic
    g = dgl.DGLGraph(nx.path_graph(3))
    edata = mx.nd.ones((g.number_of_edges(), 1))
    a = nn.edge_softmax(g, edata)
168
169
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
170
171
172
173
174
175
    assert np.allclose(a.asnumpy(), uniform_attention(g, a.shape).asnumpy(),
            1e-4, 1e-4)

    # Test higher dimension case
    edata = mx.nd.ones((g.number_of_edges(), 3, 1))
    a = nn.edge_softmax(g, edata)
176
177
    assert len(g.ndata) == 0
    assert len(g.edata) == 0
178
179
180
    assert np.allclose(a.asnumpy(), uniform_attention(g, a.shape).asnumpy(),
            1e-4, 1e-4)

181
182
if __name__ == '__main__':
    test_graph_conv()
183
    test_edge_softmax()
184
185
186
    test_set2set()
    test_glob_att_pool()
    test_simple_pool()