-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
226 lines (190 loc) · 6.63 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import torch.nn as nn
import torch
from utils import compute_similarity
from sklearn.cluster import AgglomerativeClustering
import gc
class TAE_encoder(nn.Module):
"""
Class for temporal autoencoder encoder.
filter_1 : filter size of the first convolution layer
filter_lstm : hidden size of the lstm.
pooling : pooling number for maxpooling.
"""
def __init__(self, filter_1, filter_lstm, pooling):
super().__init__()
self.hidden_lstm_1 = filter_lstm[0]
self.hidden_lstm_2 = filter_lstm[1]
self.pooling = pooling
self.n_hidden = None
## CNN PART
### output shape (batch_size, 50 , n_hidden = 64)
self.conv_layer = nn.Sequential(
nn.Conv1d(
in_channels=1,
out_channels=filter_1,
kernel_size=10,
stride=1,
padding=5,
),
nn.LeakyReLU(),
nn.MaxPool1d(self.pooling),
)
## LSTM PART
### output shape (batch_size , n_hidden = 64 , 50)
self.lstm_1 = nn.LSTM(
input_size=50,
hidden_size=self.hidden_lstm_1,
batch_first=True,
bidirectional=True,
)
### output shape (batch_size , n_hidden = 64 , 1)
self.lstm_2 = nn.LSTM(
input_size=50,
hidden_size=self.hidden_lstm_2,
batch_first=True,
bidirectional=True,
)
def forward(self, x):
## encoder
out_cnn = self.conv_layer(x)
out_cnn = out_cnn.permute((0, 2, 1))
out_lstm1, _ = self.lstm_1(out_cnn)
out_lstm1 = torch.sum(
out_lstm1.view(
out_lstm1.shape[0], out_lstm1.shape[1], 2, self.hidden_lstm_1
),
dim=2,
)
features, _ = self.lstm_2(out_lstm1)
features = torch.sum(
features.view(
features.shape[0], features.shape[1], 2, self.hidden_lstm_2
),
dim=2,
) ## (batch_size , n_hidden ,1)
if self.n_hidden == None:
self.n_hidden = features.shape[1]
return features
class TAE_decoder(nn.Module):
"""
Class for temporal autoencoder decoder.
filter_1 : filter size of the first convolution layer
filter_lstm : hidden size of the lstm.
"""
def __init__(self, n_hidden=64, pooling=8):
super().__init__()
self.pooling = pooling
self.n_hidden = n_hidden
# upsample
self.up_layer = nn.Upsample(size=pooling)
self.deconv_layer = nn.ConvTranspose1d(
in_channels=self.n_hidden,
out_channels=self.n_hidden,
kernel_size=10,
stride=1,
padding=self.pooling // 2,
)
def forward(self, features):
upsampled = self.up_layer(
features
) ##(batch_size , n_hidden , pooling)
out_deconv = self.deconv_layer(upsampled)[
:, :, : self.pooling
].contiguous()
out_deconv = out_deconv.view(out_deconv.shape[0], -1)
return out_deconv
class TAE(nn.Module):
"""
Class for temporal autoencoder.
filter_1 : filter size of the first convolution layer
filter_lstm : hidden size of the lstm.
"""
def __init__(self, args, filter_1=50, filter_lstm=[50, 1]):
super().__init__()
self.pooling = int(args.pool)
self.filter_1 = filter_1
self.filter_lstm = filter_lstm
self.tae_encoder = TAE_encoder(
filter_1=self.filter_1,
filter_lstm=self.filter_lstm,
pooling=self.pooling,
)
n_hidden = self.get_hidden(args.serie_size, args.device)
args.n_hidden = n_hidden
self.tae_decoder = TAE_decoder(
n_hidden=args.n_hidden, pooling=self.pooling
)
def get_hidden(self, serie_size, device):
a = torch.randn((1, 1, serie_size)).to(device)
test_model = TAE_encoder(
filter_1=self.filter_1,
filter_lstm=self.filter_lstm,
pooling=self.pooling,
).to(device)
with torch.no_grad():
_ = test_model(a)
n_hid = test_model.n_hidden
del test_model, a
gc.collect()
torch.cuda.empty_cache()
return n_hid
def forward(self, x):
features = self.tae_encoder(x)
out_deconv = self.tae_decoder(features)
return features.squeeze(2), out_deconv
class ClusterNet(nn.Module):
"""
class for the defintion of the DTC model
path_ae : path to load autoencoder
centr_size : size of the centroids = size of the hidden features
alpha : parameter alpha for the t-student distribution.
"""
def __init__(self, args):
super().__init__()
## init with the pretrained autoencoder model
self.tae = TAE(args)
self.tae.load_state_dict(
torch.load(args.path_weights_ae, map_location=args.device)
)
## clustering model
self.alpha_ = args.alpha
self.centr_size = args.n_hidden
self.n_clusters = args.n_clusters
self.device = args.device
self.similarity = args.similarity
def init_centroids(self, x):
"""
This function initializes centroids with agglomerative clustering
+ complete linkage.
"""
z, _ = self.tae(x.squeeze().unsqueeze(1).detach())
z_np = z.detach().cpu()
assignements = AgglomerativeClustering(
n_clusters=2, linkage="complete", affinity="precomputed"
).fit_predict(
compute_similarity(z_np, z_np, similarity=self.similarity)
)
centroids_ = torch.zeros(
(self.n_clusters, self.centr_size), device=self.device
)
for cluster_ in range(self.n_clusters):
index_cluster = [
k for k, index in enumerate(assignements) if index == cluster_
]
centroids_[cluster_] = torch.mean(z.detach()[index_cluster], dim=0)
self.centroids = nn.Parameter(centroids_)
def forward(self, x):
z, x_reconstr = self.tae(x)
z_np = z.detach().cpu()
similarity = compute_similarity(
z, self.centroids, similarity=self.similarity
)
## Q (batch_size , n_clusters)
Q = torch.pow((1 + (similarity / self.alpha_)), -(self.alpha_ + 1) / 2)
sum_columns_Q = torch.sum(Q, dim=1).view(-1, 1)
Q = Q / sum_columns_Q
## P : ground truth distribution
P = torch.pow(Q, 2) / torch.sum(Q, dim=0).view(1, -1)
sum_columns_P = torch.sum(P, dim=1).view(-1, 1)
P = P / sum_columns_P
return z, x_reconstr, Q, P