-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_tcn.py
95 lines (77 loc) · 3.2 KB
/
net_tcn.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
import torch
"""
TCN
"""
class Chomp1d(torch.nn.Module):
"""
Removes the last elements of a time series.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L - s`) where `s`
is the number of elements to remove.
@param chomp_size Number of elements to remove.
"""
def __init__(self, chomp_size):
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size]
class SqueezeChannels(torch.nn.Module):
"""
Squeezes, in a three-dimensional tensor, the third dimension.
"""
def __init__(self):
super(SqueezeChannels, self).__init__()
def forward(self, x):
return x.squeeze(2)
class CausalConvolutionBlock(torch.nn.Module):
"""
Causal convolution block, composed sequentially of two causal convolutions
(with leaky ReLU activation functions), and a parallel residual connection.
Takes as input a three-dimensional tensor (`B`, `C`, `L`) where `B` is the
batch size, `C` is the number of input channels, and `L` is the length of
the input. Outputs a three-dimensional tensor (`B`, `C`, `L`).
@param in_channels Number of input channels.
@param out_channels Number of save channels.
@param kernel_size Kernel size of the applied non-residual convolutions.
@param dilation Dilation parameter of non-residual convolutions.
@param final Disables, if True, the last activation function.
"""
def __init__(self, in_channels, out_channels, kernel_size, dilation, final=False):
super(CausalConvolutionBlock, self).__init__()
# Computes left padding so that the applied convolutions are causal
padding = (kernel_size - 1) * dilation
# First causal convolution
conv1 = torch.nn.utils.weight_norm(torch.nn.Conv1d(
in_channels, out_channels, kernel_size,
padding=padding, dilation=dilation
))
# The truncation makes the convolution causal
chomp1 = Chomp1d(padding)
relu1 = torch.nn.LeakyReLU()
# Second causal convolution
conv2 = torch.nn.utils.weight_norm(torch.nn.Conv1d(
out_channels, out_channels, kernel_size,
padding=padding, dilation=dilation
))
chomp2 = Chomp1d(padding)
relu2 = torch.nn.LeakyReLU()
# Causal network
self.causal = torch.nn.Sequential(
conv1, chomp1, relu1, conv2, chomp2, relu2
)
# Residual connection
self.upordownsample = torch.nn.Conv1d(
in_channels, out_channels, 1
) if in_channels != out_channels else None
# Final activation function
self.relu = torch.nn.LeakyReLU() if final else None
def forward(self, x):
out_causal = self.causal(x)
res = x if self.upordownsample is None else self.upordownsample(x)
if self.relu is None:
# return out_causal + res
return out_causal + res
else:
# return self.relu(out_causal + res)
return self.relu(out_causal + res)