-
Notifications
You must be signed in to change notification settings - Fork 104
/
depthwise.py
96 lines (77 loc) · 3.95 KB
/
depthwise.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
import torch
import torch.nn as nn
from torch.nn.utils import weight_norm
from torch.autograd import Variable
class Chomp1d(nn.Module):
"""PyTorch does not offer native support for causal convolutions, so it is implemented (with some inefficiency) by simply using a standard convolution with zero padding on both sides, and chopping off the end of the sequence."""
def __init__(self, chomp_size):
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size].contiguous()
class FirstBlock(nn.Module):
def __init__(self, target, n_inputs, n_outputs, kernel_size, stride, dilation, padding):
super(FirstBlock, self).__init__()
self.target = target
self.conv1 = nn.Conv1d(n_inputs, n_outputs, kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=n_outputs)
self.chomp1 = Chomp1d(padding)
self.net = nn.Sequential(self.conv1, self.chomp1)
self.relu = nn.PReLU(n_inputs)
self.init_weights()
def init_weights(self):
"""Initialize weights"""
self.conv1.weight.data.normal_(0, 0.1)
def forward(self, x):
out = self.net(x)
return self.relu(out)
class TemporalBlock(nn.Module):
def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding):
super(TemporalBlock, self).__init__()
self.conv1 = nn.Conv1d(n_inputs, n_outputs, kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=n_outputs)
self.chomp1 = Chomp1d(padding)
self.net = nn.Sequential(self.conv1, self.chomp1)
self.relu = nn.PReLU(n_inputs)
self.init_weights()
def init_weights(self):
"""Initialize weights"""
self.conv1.weight.data.normal_(0, 0.1)
def forward(self, x):
out = self.net(x)
return self.relu(out+x) #residual connection
class LastBlock(nn.Module):
def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding):
super(LastBlock, self).__init__()
self.conv1 = nn.Conv1d(n_inputs, n_outputs, kernel_size,
stride=stride, padding=padding, dilation=dilation, groups=n_outputs)
self.chomp1 = Chomp1d(padding)
self.net = nn.Sequential(self.conv1, self.chomp1)
self.linear = nn.Linear(n_inputs, n_inputs)
self.init_weights()
def init_weights(self):
"""Initialize weights"""
self.linear.weight.data.normal_(0, 0.01)
def forward(self, x):
out = self.net(x)
return self.linear(out.transpose(1,2)+x.transpose(1,2)).transpose(1,2) #residual connection
class DepthwiseNet(nn.Module):
def __init__(self, target, num_inputs, num_levels, kernel_size=2, dilation_c=2):
super(DepthwiseNet, self).__init__()
layers = []
in_channels = num_inputs
out_channels = num_inputs
for l in range(num_levels):
dilation_size = dilation_c ** l
if l==0:
layers += [FirstBlock(target, in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
padding=(kernel_size-1) * dilation_size)]
elif l==num_levels-1:
layers+=[LastBlock(in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
padding=(kernel_size-1) * dilation_size)]
else:
layers += [TemporalBlock(in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
padding=(kernel_size-1) * dilation_size)]
self.network = nn.Sequential(*layers)
def forward(self, x):
return self.network(x)