-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.py
50 lines (42 loc) · 1.32 KB
/
net.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
import torch.nn as nn
from TreeConvolution.tcnn import BinaryTreeConv, TreeLayerNorm
from TreeConvolution.tcnn import TreeActivation, DynamicPooling
from TreeConvolution.util import prepare_trees
def left_child(x):
if len(x) != 3:
return None
return x[1]
def right_child(x):
if len(x) != 3:
return None
return x[2]
def features(x):
return x[0]
class BaoNet(nn.Module):
def __init__(self, in_channels):
super(BaoNet, self).__init__()
self.__in_channels = in_channels
self.__cuda = False
self.tree_conv = nn.Sequential(
BinaryTreeConv(self.__in_channels, 256),
TreeLayerNorm(),
TreeActivation(nn.LeakyReLU()),
BinaryTreeConv(256, 128),
TreeLayerNorm(),
TreeActivation(nn.LeakyReLU()),
BinaryTreeConv(128, 64),
TreeLayerNorm(),
DynamicPooling(),
nn.Linear(64, 32),
nn.LeakyReLU(),
nn.Linear(32, 1)
)
def in_channels(self):
return self.__in_channels
def forward(self, x):
trees = prepare_trees(x, features, left_child, right_child,
cuda=self.__cuda)
return self.tree_conv(trees)
def cuda(self):
self.__cuda = True
return super().cuda()