-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.py
98 lines (80 loc) · 3.96 KB
/
model.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
97
98
import torch.nn as nn
from torch.nn.utils import weight_norm, spectral_norm
class Chomp1d(nn.Module):
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 TemporalBlock(nn.Module):
"""Creates a temporal block.
Args:
n_inputs (int): number of inputs.
n_outputs (int): size of fully connected layers.
kernel_size (int): kernel size along temporal axis of convolution layers within the temporal block.
dilation (int): dilation of convolution layers along temporal axis within the temporal block.
padding (int): padding
dropout (float): dropout rate
Returns:
tuple of output layers
"""
def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2):
super(TemporalBlock, self).__init__()
self.conv1 = spectral_norm(nn.Conv1d(n_inputs, n_outputs, kernel_size, stride=stride, padding=padding, dilation=dilation))
self.chomp1 = Chomp1d(padding)
self.relu1 = nn.PReLU()
self.dropout1 = nn.Dropout(dropout)
self.conv2 = spectral_norm(nn.Conv1d(n_outputs, n_outputs, kernel_size, stride=stride, padding=padding, dilation=dilation))
self.chomp2 = Chomp1d(padding)
self.relu2 = nn.PReLU()
self.dropout2 = nn.Dropout(dropout)
if padding == 0:
self.net = nn.Sequential(self.conv1, self.relu1, self.dropout1, self.conv2, self.relu2, self.dropout2)
else:
self.net = nn.Sequential(self.conv1, self.chomp1, self.relu1, self.dropout1, self.conv2, self.chomp2, self.relu2, self.dropout2)
self.downsample = nn.Conv1d(n_inputs, n_outputs, 1) if n_inputs != n_outputs else None
self.relu = nn.PReLU()
self.init_weights()
def init_weights(self):
self.conv1.weight.data.normal_(0, 0.5)
self.conv2.weight.data.normal_(0, 0.5)
if self.downsample is not None:
self.downsample.weight.data.normal_(0, 0.5)
def forward(self, x):
out = self.net(x)
res = x if self.downsample is None else self.downsample(x)
return out, self.relu(out + res)
class Generator(nn.Module):
"""Generator: 3 to 1 Causal temporal convolutional network with skip connections.
This network uses 1D convolutions in order to model multiple timeseries co-dependency.
"""
def __init__(self):
super(Generator, self).__init__()
self.tcn = nn.ModuleList([TemporalBlock(3, 80, kernel_size=1, stride=1, dilation=1, padding=0),
*[TemporalBlock(80, 80, kernel_size=2, stride=1, dilation=i, padding=i) for i in [1, 2, 4, 8, 16, 32]]])
self.last = nn.Conv1d(80, 1, kernel_size=1, stride=1, dilation=1)
def forward(self, x):
skip_layers = []
for layer in self.tcn:
skip, x = layer(x)
skip_layers.append(skip)
x = self.last(x + sum(skip_layers))
return x
class Discriminator(nn.Module):
"""Discrimnator: 1 to 1 Causal temporal convolutional network with skip connections.
This network uses 1D convolutions in order to model multiple timeseries co-dependency.
"""
def __init__(self, seq_len, conv_dropout=0.05):
super(Discriminator, self).__init__()
self.tcn = nn.ModuleList([TemporalBlock(1, 80, kernel_size=1, stride=1, dilation=1, padding=0),
*[TemporalBlock(80, 80, kernel_size=2, stride=1, dilation=i, padding=i) for i in [1, 2, 4, 8, 16, 32]]])
self.last = nn.Conv1d(80, 1, kernel_size=1, dilation=1)
self.to_prob = nn.Sequential(nn.Linear(seq_len, 1), nn.Sigmoid())
def forward(self, x):
skip_layers = []
for layer in self.tcn:
skip, x = layer(x)
skip_layers.append(skip)
x = x + sum(skip_layers)
x = self.last(x)
return self.to_prob(x).squeeze()