/
test_rsgcn_update.py
85 lines (63 loc) · 2.6 KB
/
test_rsgcn_update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from chainer import cuda
from chainer import gradient_check
import numpy
import pytest
from chainer_chemistry.config import MAX_ATOMIC_NUM
from chainer_chemistry.links.connection.embed_atom_id import EmbedAtomID
from chainer_chemistry.links.update.rsgcn_update import RSGCNUpdate
from chainer_chemistry.utils.permutation import permute_adj
from chainer_chemistry.utils.permutation import permute_node
atom_size = 5
in_channels = 3
hidden_dim = 4
batch_size = 2
num_edge_type = 7
@pytest.fixture
def update():
return RSGCNUpdate(in_channels=in_channels, out_channels=hidden_dim)
@pytest.fixture
def data():
numpy.random.seed(0)
atom_data = numpy.random.randint(
0, high=MAX_ATOMIC_NUM, size=(batch_size, atom_size)).astype('i')
adj_data = numpy.random.randint(
0, high=2, size=(batch_size, atom_size, atom_size)).astype('f')
y_grad = numpy.random.uniform(
-1, 1, (batch_size, atom_size, hidden_dim)).astype('f')
embed = EmbedAtomID(in_size=MAX_ATOMIC_NUM, out_size=in_channels)
embed_atom_data = embed(atom_data).data
return embed_atom_data, adj_data, y_grad
def check_forward(update, atom_data, adj_data):
y_actual = cuda.to_cpu(update(atom_data, adj_data).data)
assert y_actual.shape == (batch_size, atom_size, hidden_dim)
def test_forward_cpu(update, data):
atom_data, adj_data = data[:2]
check_forward(update, atom_data, adj_data)
@pytest.mark.gpu
def test_forward_gpu(update, data):
atom_data, adj_data = map(cuda.to_gpu, data[:2])
update.to_gpu()
check_forward(update, atom_data, adj_data)
def test_backward_cpu(update, data):
atom_data, adj_data, y_grad = data
gradient_check.check_backward(
update, (atom_data, adj_data), y_grad, atol=1e-3, rtol=1e-3)
@pytest.mark.gpu
def test_backward_gpu(update, data):
atom_data, adj_data, y_grad = map(cuda.to_gpu, data)
update.to_gpu()
gradient_check.check_backward(
update, (atom_data, adj_data), y_grad, atol=1e-3, rtol=1e-3)
def test_forward_cpu_graph_invariant(update, data):
atom_data, adj_data = data[:2]
y_actual = cuda.to_cpu(update(atom_data, adj_data).data)
permutation_index = numpy.random.permutation(atom_size)
permute_atom_data = permute_node(atom_data, permutation_index, axis=1)
permute_adj_data = permute_adj(adj_data, permutation_index)
permute_y_actual = cuda.to_cpu(
update(permute_atom_data, permute_adj_data).data)
numpy.testing.assert_allclose(
permute_node(y_actual, permutation_index, axis=1), permute_y_actual,
rtol=1e-5, atol=1e-5)
if __name__ == '__main__':
pytest.main([__file__, '-v', '-s'])