-
Notifications
You must be signed in to change notification settings - Fork 12
/
example.py
97 lines (81 loc) · 2.51 KB
/
example.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
86
87
88
89
90
91
92
93
94
95
96
97
# < begin copyright >
# Copyright Ryan Marcus 2019
#
# This file is part of TreeConvolution.
#
# TreeConvolution is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TreeConvolution is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TreeConvolution. If not, see <http://www.gnu.org/licenses/>.
#
# < end copyright >
import numpy as np
from torch import nn
from util import prepare_trees
import tcnn
# First tree:
# (0, 1)
# (1, 2) (-3, 0)
# (0, 1) (-1, 0) (2, 3) (1, 2)
tree1 = (
(0, 1),
((1, 2), ((0, 1),), ((-1, 0),)),
((-3, 0), ((2, 3),), ((1, 2),))
)
# Second tree:
# (16, 3)
# (0, 1) (2, 9)
# (5, 3) (2, 6)
tree2 = (
(16, 3),
((0, 1), ((5, 3),), ((2, 6),)),
((2, 9),)
)
trees = [tree1, tree2]
# function to extract the left child of a node
def left_child(x):
assert isinstance(x, tuple)
if len(x) == 1:
# leaf.
return None
return x[1]
# function to extract the right child of node
def right_child(x):
assert isinstance(x, tuple)
if len(x) == 1:
# leaf.
return None
return x[2]
# function to transform a node into a (feature) vector,
# should be a numpy array.
def transformer(x):
return np.array(x[0])
# this call to `prepare_trees` will create the correct input for
# a `tcnn.BinaryTreeConv` operator.
prepared_trees = prepare_trees(trees, transformer, left_child, right_child)
# A tree convolution neural network mapping our input trees with
# 2 channels to trees with 16 channels, then 8 channels, then 4 channels.
# Between each mapping, we apply layer norm and then a ReLU activation.
# Finally, we apply "dynamic pooling", which returns a flattened vector.
net = nn.Sequential(
tcnn.BinaryTreeConv(2, 16),
tcnn.TreeLayerNorm(),
tcnn.TreeActivation(nn.ReLU()),
tcnn.BinaryTreeConv(16, 8),
tcnn.TreeLayerNorm(),
tcnn.TreeActivation(nn.ReLU()),
tcnn.BinaryTreeConv(8, 4),
tcnn.TreeLayerNorm(),
tcnn.TreeActivation(nn.ReLU()),
tcnn.DynamicPooling()
)
# output: torch.Size([2, 4])
print(net(prepared_trees).shape)