-
Notifications
You must be signed in to change notification settings - Fork 12
/
tcnn.py
66 lines (55 loc) · 2.27 KB
/
tcnn.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
# < 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 torch
import torch.nn as nn
class BinaryTreeConv(nn.Module):
def __init__(self, in_channels, out_channels):
super(BinaryTreeConv, self).__init__()
self.__in_channels = in_channels
self.__out_channels = out_channels
# we can think of the tree conv as a single dense layer
# that we "drag" across the tree.
self.weights = nn.Conv1d(in_channels, out_channels, stride=3, kernel_size=3)
def forward(self, flat_data):
trees, idxes = flat_data
orig_idxes = idxes
idxes = idxes.expand(-1, -1, self.__in_channels).transpose(1, 2)
expanded = torch.gather(trees, 2, idxes)
results = self.weights(expanded)
# add a zero vector back on
zero_vec = torch.zeros((trees.shape[0], self.__out_channels)).unsqueeze(2)
results = torch.cat((zero_vec, results), dim=2)
return (results, orig_idxes)
class TreeActivation(nn.Module):
def __init__(self, activation):
super(TreeActivation, self).__init__()
self.activation = activation
def forward(self, x):
return (self.activation(x[0]), x[1])
class TreeLayerNorm(nn.Module):
def forward(self, x):
data, idxes = x
mean = torch.mean(data, dim=(1, 2)).unsqueeze(1).unsqueeze(1)
std = torch.std(data, dim=(1, 2)).unsqueeze(1).unsqueeze(1)
normd = (data - mean) / (std + 0.00001)
return (normd, idxes)
class DynamicPooling(nn.Module):
def forward(self, x):
return torch.max(x[0], dim=2).values