-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathalgorithms.py
276 lines (202 loc) · 9.73 KB
/
algorithms.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import torch
import torch.nn as nn
import numpy as np
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from models.loss import NTXentLoss
import torch.nn.functional as F
from models.TC import TC
from models.helpers import proj_head
def get_algorithm_class(algorithm_name):
"""Return the algorithm class with the given name."""
if algorithm_name not in globals():
raise NotImplementedError("Algorithm not found: {}".format(algorithm_name))
return globals()[algorithm_name]
class Algorithm(torch.nn.Module):
"""
A subclass of Algorithm implements a domain adaptation algorithm.
Subclasses should implement the update() method.
"""
def __init__(self, configs):
super(Algorithm, self).__init__()
self.configs = configs
self.cross_entropy = nn.CrossEntropyLoss()
def update(self, *args, **kwargs):
raise NotImplementedError
class simclr(Algorithm):
def __init__(self, backbone_fe, backbone_temporal, classifier, configs, hparams, device):
super(simclr, self).__init__(configs)
self.feature_extractor = backbone_fe(configs)
self.temporal_encoder = backbone_temporal(hparams)
self.proj_head = proj_head(configs, hparams)
self.network = nn.Sequential(self.feature_extractor, self.proj_head)
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=hparams["learning_rate"],
weight_decay=hparams["weight_decay"],
betas=(0.9, 0.99)
)
self.hparams = hparams
self.contrastive_loss = NTXentLoss(device, hparams["batch_size"], 0.2, True)
def update(self, samples):
# ====== Data =====================
aug1 = samples["transformed_samples"][0]
aug2 = samples["transformed_samples"][1]
self.optimizer.zero_grad()
features1 = self.feature_extractor(aug1)
z1 = self.proj_head(features1)
features2 = self.feature_extractor(aug2)
z2 = self.proj_head(features2)
# normalize projection feature vectors
z1 = F.normalize(z1, dim=1)
z2 = F.normalize(z2, dim=1)
# Cross-Entropy loss
loss = self.contrastive_loss(z1, z2)
loss.backward()
self.optimizer.step()
return {'Total_loss': loss.item()}, \
[self.feature_extractor, self.temporal_encoder, self.proj_head]
class cpc(Algorithm):
def __init__(self, backbone_fe, backbone_temporal, classifier, configs, hparams, device):
super(cpc, self).__init__(configs)
self.feature_extractor = backbone_fe(configs)
self.temporal_encoder = backbone_temporal(hparams)
self.classifier = classifier(configs, hparams)
self.network = nn.Sequential(self.feature_extractor, self.temporal_encoder, self.classifier)
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=hparams["learning_rate"],
weight_decay=hparams["weight_decay"],
betas=(0.9, 0.99)
)
self.hparams = hparams
self.num_channels = hparams["num_channels"]
self.hid_dim = hparams["hid_dim"]
self.timestep = hparams["timesteps"]
self.Wk = nn.ModuleList([nn.Linear(self.hid_dim, self.num_channels) for _ in range(self.timestep)])
self.lsoftmax = nn.LogSoftmax()
self.device = device
self.lstm = nn.LSTM(self.num_channels, self.hid_dim, bidirectional=False, batch_first=True)
def update(self, samples):
# ====== Data =====================
data = samples['sample_ori'].float()
self.optimizer.zero_grad()
# Src original features
features = self.feature_extractor(data)
seq_len = features.shape[2]
features = features.transpose(1, 2)
batch = self.hparams["batch_size"]
t_samples = torch.randint(seq_len - self.timestep, size=(1,)).long().to(self.device) # randomly pick timesteps
loss = 0 # average over timestep and batch
encode_samples = torch.empty((self.timestep, batch, self.num_channels)).float().to(self.device)
for i in np.arange(1, self.timestep + 1):
encode_samples[i - 1] = features[:, t_samples + i, :].view(batch, self.num_channels)
forward_seq = features[:, :t_samples + 1, :]
output1, _ = self.lstm(forward_seq)
c_t = output1[:, t_samples, :].view(batch, self.hid_dim)
pred = torch.empty((self.timestep, batch, self.num_channels)).float().to(self.device)
for i in np.arange(0, self.timestep):
linear = self.Wk[i]
pred[i] = linear(c_t)
for i in np.arange(0, self.timestep):
total = torch.mm(encode_samples[i], torch.transpose(pred[i], 0, 1))
loss += torch.sum(torch.diag(self.lsoftmax(total)))
loss /= -1. * batch * self.timestep
loss.backward()
self.optimizer.step()
return {'Total_loss': loss.item()}, \
[self.feature_extractor, self.temporal_encoder, self.classifier]
class ts_tcc(Algorithm):
def __init__(self, backbone_fe, backbone_temporal, classifier, configs, hparams, device):
super(ts_tcc, self).__init__(configs)
self.feature_extractor = backbone_fe(configs)
self.temporal_encoder = backbone_temporal(hparams)
self.classifier = classifier(configs, hparams)
self.temporal_contr_model = TC(hparams, device)
self.network = nn.Sequential(self.feature_extractor, self.temporal_encoder,
self.classifier, self.temporal_contr_model)
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=hparams["learning_rate"],
weight_decay=hparams["weight_decay"],
betas=(0.9, 0.99)
)
self.hparams = hparams
self.contrastive_loss = NTXentLoss(device, hparams["batch_size"], 0.2, True)
def update(self, samples):
# ====== Data =====================
aug1 = samples["transformed_samples"][0]
aug2 = samples["transformed_samples"][1]
self.optimizer.zero_grad()
features1 = self.feature_extractor(aug1)
features2 = self.feature_extractor(aug2)
# normalize projection feature vectors
features1 = F.normalize(features1, dim=1)
features2 = F.normalize(features2, dim=1)
temp_cont_loss1, temp_cont_lstm_feat1 = self.temporal_contr_model(features1, features2)
temp_cont_loss2, temp_cont_lstm_feat2 = self.temporal_contr_model(features2, features1)
# Cross-Entropy loss
loss = temp_cont_loss1 + temp_cont_loss2 + \
0.7 * self.contrastive_loss(temp_cont_lstm_feat1, temp_cont_lstm_feat2)
loss.backward()
self.optimizer.step()
return {'Total_loss': loss.item()}, \
[self.feature_extractor, self.temporal_encoder, self.classifier]
class clsTran(Algorithm):
def __init__(self, backbone_fe, backbone_temporal, classifier, configs, hparams, device):
super(clsTran, self).__init__(configs)
self.feature_extractor = backbone_fe(configs)
self.temporal_encoder = backbone_temporal(hparams)
self.classifier = nn.Linear(hparams["clf"], configs.num_clsTran_tasks)
self.network = nn.Sequential(self.feature_extractor, self.temporal_encoder, self.classifier)
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=hparams["learning_rate"],
weight_decay=hparams["weight_decay"],
betas=(0.9, 0.99)
)
self.hparams = hparams
def update(self, samples):
# ====== Data =====================
data = samples["transformed_samples"].float()
labels = samples["aux_labels"].long()
self.optimizer.zero_grad()
features = self.feature_extractor(data)
features = features.flatten(1, 2)
logits = self.classifier(features)
# Cross-Entropy loss
loss = self.cross_entropy(logits, labels)
loss.backward()
self.optimizer.step()
return {'Total_loss': loss.item()}, \
[self.feature_extractor, self.temporal_encoder, self.classifier]
class supervised(Algorithm):
def __init__(self, backbone_fe, backbone_temporal, classifier, configs, hparams):
super(supervised, self).__init__(configs)
self.feature_extractor = backbone_fe
self.temporal_encoder = backbone_temporal
self.classifier = classifier(configs, hparams)
self.network = nn.Sequential(self.feature_extractor, self.temporal_encoder, self.classifier)
self.optimizer = torch.optim.Adam(
self.network.parameters(),
lr=hparams["learning_rate"],
weight_decay=hparams["weight_decay"],
betas=(0.9, 0.99)
)
self.hparams = hparams
def update(self, samples):
# ====== Data =====================
data = samples['sample_ori'].float()
labels = samples['class_labels'].long()
# ====== Source =====================
self.optimizer.zero_grad()
# Src original features
features = self.feature_extractor(data)
features = self.temporal_encoder(features)
logits = self.classifier(features)
# Cross-Entropy loss
x_ent_loss = self.cross_entropy(logits, labels)
x_ent_loss.backward()
self.optimizer.step()
return {'Total_loss': x_ent_loss.item()}, \
[self.feature_extractor, self.temporal_encoder, self.classifier]