-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
88 lines (68 loc) · 2.73 KB
/
models.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
import torch.nn as nn
import torch.nn.functional as F
class DilatedNet(nn.Module):
def __init__(self, filters, dilation_depth, dilation_stacks):
super(DilatedNet, self).__init__()
self.filters = filters
self.dilation_depth = dilation_depth
self.dilation_stacks = dilation_stacks
self.receptive_field = dilation_stacks*(3**dilation_depth)
self.initialconv = nn.Conv1d(1, filters, 3, dilation=1, padding=1)
for s in range(dilation_stacks):
for i in range(dilation_depth):
setattr(
self,
'dilated_conv_{}_relu_s{}'.format(3 ** i, s),
nn.Conv1d(filters, filters, 3, dilation=3 ** i, padding=3 ** i)
)
self.finalconv = nn.Conv1d(filters, filters, 3, dilation=1, padding=1)
self.output = nn.Linear(filters, 1)
def forward(self, x):
x = x.cuda().double()
x = self.initialconv(x)
skip_connections = []
for s in range(self.dilation_stacks):
for i in range(self.dilation_depth):
original_x = x
x = F.relu(getattr(self, 'dilated_conv_{}_relu_s{}'.format(3 ** i, s))(x))
skip_connections.append(x)
x = x + original_x
x = F.relu(self.finalconv(x))
x = F.max_pool1d(x, kernel_size=x.size()[2:])
x = x.view(-1, self.filters)
x = F.sigmoid(self.output(x))
return x
class ConvNet(nn.Module):
def __init__(self, filters, layers):
super(ConvNet, self).__init__()
self.filters = filters
self.layers = layers
self.receptive_field = 3**layers
self.initialconv = nn.Conv1d(1, filters, 3, dilation=1, padding=1)
self.initialbn = nn.BatchNorm1d(filters)
for i in range(layers):
setattr(
self,
'conv_{}'.format(i),
nn.Conv1d(filters, filters, 3, dilation=1, padding=1)
)
setattr(
self,
'bn_{}'.format(i),
nn.BatchNorm1d(filters)
)
self.finalconv = nn.Conv1d(filters, filters, 3, dilation=1, padding=1)
self.output = nn.Linear(filters, 1)
def forward(self, x):
x = x.cuda().double()
x = self.initialconv(x)
x = self.initialbn(x)
for i in range(self.layers):
x = F.relu(getattr(self,'conv_{}'.format(i))(x))
x = getattr(self,'bn_{}'.format(i))(x)
x = F.max_pool1d(x, kernel_size=3, stride=3)
x = F.relu(self.finalconv(x))
x = F.max_pool1d(x, kernel_size=x.size()[2:])
x = x.view(-1, self.filters)
x = F.sigmoid(self.output(x))
return x