-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·799 lines (728 loc) · 26.7 KB
/
train.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
#!/usr/bin/env python3
import argparse
from functools import partial
from pathlib import Path
from typing import Any, Final
import torch
from python_tools.generic import namespace_as_string
from python_tools.ml import metrics, neural
from python_tools.ml.data_loader import DataLoader
from python_tools.ml.default.neural_models import MLPModel
from python_tools.ml.default.transformations import (
DefaultTransformations,
revert_transform,
set_transform,
)
from python_tools.ml.evaluator import evaluator
from dataloader import IEMOCAP, MOSEI, MOSI, PANAM, SEWA, Instagram, Test
from routing import MMRouting
CCC_DIMENSION = ("valence", "arousal")
MAE_DIMENSION = (
# MOSEI
"sentiment",
"polarity",
"happiness",
# MOSI sentiment
"mosi",
# Test
"uni",
"bi",
# IEMOCAP
"Valence",
"Arousal",
)
CLASS_DIMENSION = ("constructs", "intent")
class RoutingTFN(neural.LossModule):
# each uni/bi/tri has its own TFN
# embedding from TFNs need linear projection to have same size
# routing creates new embedding
# linear layer for prediction
def __init__(
self,
*,
input_size: int = -1,
output_size: int = -1,
layer_sizes: tuple[int, ...],
input_sizes: tuple[int, ...],
final_activation: dict[str, Any],
**kwargs,
) -> None:
super().__init__(
loss_function=kwargs.get("loss_function", "MSELoss"),
attenuation=kwargs.get("attenuation", ""),
attenuation_lambda=kwargs.get("attenuation_lambda", 0.0),
sample_weight=kwargs.get("sample_weight", None),
training_validation=kwargs.get("training_validation", False),
)
input_sizes = tuple(x for x in input_sizes if x)
assert input_size == sum(input_sizes)
if len(input_sizes) == 2:
tfns = [(input_sizes[0],), (input_sizes[1],), input_sizes]
elif len(input_sizes) == 3:
tfns = [
(input_sizes[0],),
(input_sizes[1],),
(input_sizes[2],),
(input_sizes[0], input_sizes[1]),
(input_sizes[1], input_sizes[2]),
(input_sizes[0], input_sizes[2]),
input_sizes,
]
else:
raise AssertionError(input_sizes)
# {MLP+TFN} routing, linear
self.view_starts: Final = torch.cumsum(
torch.LongTensor([0] + list(input_sizes)), dim=0
)
kwargs.pop("layers")
# TFNs:
self.tfns = torch.nn.ModuleList(
[
MLP_mult(
input_size=sum(input_sizes_),
output_size=layer_sizes[-1] + 1,
layer_sizes=layer_sizes[:-2] + (min(30, layer_sizes[-2]),),
layers=-1,
input_sizes=input_sizes_,
final_activation=kwargs["activation"],
save=False,
**kwargs,
)
for input_sizes_ in tfns
]
)
# Routing
self.routing = MMRouting(
in_capsules=len(tfns),
input_size=layer_sizes[-1],
out_capsules=output_size,
output_size=5,
iterations=10 if output_size > 1 else 1,
)
# prediction
self.prediction = torch.nn.ModuleList(
[torch.nn.Linear(5, 1) for i in range(output_size)]
)
self.jit_me = False
def forward(
self,
x: torch.Tensor,
meta: dict[str, torch.Tensor],
y: torch.Tensor | None = None,
dataset: str = "",
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
# TFN + projection to capsule size
x = torch.stack(
# uni-modal
[
self.tfns[i](
x[:, start : self.view_starts[i + 1]], meta, y=y, dataset=dataset
)[0]
for i, start in enumerate(self.view_starts[:-1])
]
# bi-modal
+ (
[
self.tfns[3](
x[:, : self.view_starts[-1]], meta, y=y, dataset=dataset
)[0],
self.tfns[4](
x[:, self.view_starts[1] :], meta, y=y, dataset=dataset
)[0],
self.tfns[5](
torch.cat(
[x[:, : self.view_starts[1]], x[:, self.view_starts[-2] :]],
dim=1,
),
meta,
y=y,
dataset=dataset,
)[0],
]
if len(self.tfns) == 7
else []
)
# tri-modal
+ [self.tfns[-1](x, meta, y=y, dataset=dataset)[0]],
dim=1,
)
p_bi = torch.sigmoid(x[:, :, -1])
meta["meta_p_i"] = p_bi
# final prediction is a linear function: routing weights can be applied as
# weighted sum!
for imod in range(x.shape[1]):
x_ = x[:, imod, :-1] * p_bi[:, imod, None]
for ilabel, label_fun in enumerate(self.prediction):
meta[f"meta_m{imod}_l{ilabel}"] = torch.einsum(
"bi, io, co-> bc",
x_,
self.routing.weights[imod, :, ilabel],
label_fun.weight,
)
# capsule
r = self.routing(f_bid=x[:, :, :-1], p_bi=p_bi)[1]
meta["meta_r_ij"] = r.view(r.shape[0], -1)
meta["meta_embedding"] = meta["meta_r_ij"]
# predict
y_hat = torch.cat(
[
sum(
[
meta[f"meta_m{i}_l{l}"] * r[:, i, l, None]
for i in range(r.shape[1])
],
start=self.prediction[l].bias,
)
for l in range(r.shape[2])
],
axis=1,
)
return y_hat, meta
class MLP_mult(neural.LossModule):
# multiplicative interactions before the last layer
def __init__(
self,
*,
input_size: int = -1,
output_size: int = -1,
layer_sizes: tuple[int, ...],
input_sizes: tuple[int, ...],
final_activation: dict[str, Any],
residual: bool = False,
save: bool = True,
only_tri: bool = False,
**kwargs,
) -> None:
super().__init__(
loss_function=kwargs.get("loss_function", "MSELoss"),
attenuation=kwargs.get("attenuation", ""),
attenuation_lambda=kwargs.get("attenuation_lambda", 0.0),
sample_weight=kwargs.get("sample_weight", None),
training_validation=kwargs.get("training_validation", False),
)
self.residual: Final = residual
self.save: Final = save
self.only_tri: Final = only_tri
input_sizes = tuple(x for x in input_sizes if x)
assert input_size == sum(input_sizes)
if only_tri:
assert not residual
self.view_starts: Final = torch.cumsum(
torch.LongTensor([0] + list(input_sizes)), dim=0
)
kwargs.pop("layers")
self.before = torch.nn.ModuleList(
[
neural.MLP(
input_size=size,
output_size=layer_sizes[-1] if layer_sizes else size,
layer_sizes=layer_sizes[:-1] if layer_sizes else None,
layers=-1,
final_activation=kwargs["activation"],
**kwargs,
)
for size in input_sizes
]
)
# determine combined size and uni/bi/tri parts
size = layer_sizes[-1] if layer_sizes else input_sizes[0]
if not layer_sizes:
assert all(size == input_sizes[0] for size in input_sizes)
combined = neural.interaction_terms(
[torch.ones(1, size) * value for _, value in zip(input_sizes, [2, 3, 7])],
append_one=True,
)
self.uni = (combined[0] == 2) | (combined[0] == 3) | (combined[0] == 7)
self.bi = (combined[0] == 6) | (combined[0] == 14) | (combined[0] == 21)
self.tri = combined[0] == 42
if only_tri:
self.uni[:] = False
self.bi[:] = False
self.tri[:] = True
assert self.uni.sum() + self.bi.sum() + self.tri.sum() == combined.shape[1]
self.mult = neural.InteractionModel(
input_sizes=tuple([size] * len(input_sizes)),
append_one=True,
)
kwargs["layers"] = 0
self.after = torch.nn.ModuleList(
[
neural.MLP(
input_size=int(index.sum().item()),
output_size=output_size,
final_activation=final_activation,
**kwargs,
)
for index in (self.uni, self.bi, self.tri)
if index.any()
]
)
self.jit_me = False
def forward(
self,
x: torch.Tensor,
meta: dict[str, torch.Tensor],
y: torch.Tensor | None = None,
dataset: str = "",
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
# get uni-modal representations
x = torch.cat(
[
self.before[i](
x[:, start : self.view_starts[i + 1]],
meta,
y=y,
dataset=dataset,
)[0]
for i, start in enumerate(self.view_starts[:-1])
],
dim=1,
)
# TFN
x = self.mult(x, meta, y=y, dataset=dataset)[0]
index_uni = self.uni
index_bi = self.bi
index_tri = self.tri
if self.save:
meta["meta_tfn_embedding"] = x
# predictions from each feature set
y_hat = 0
excluded = tuple(x for x in self.exclude_parameters_prefix if "before" not in x)
for i, (model, index) in enumerate(
zip(self.after, (index_uni, index_bi, index_tri))
):
y_, meta = model(x[:, index], meta, y=y, dataset=dataset)
if self.save:
meta[f"meta_y_hat_{i}"] = y_
if self.only_tri and not index.any():
y_ = 0
if self.save:
meta[f"meta_y_hat_{i}"] = meta[f"meta_y_hat_{i}"] * 0
y_hat = y_hat + y_
# uni: ("after.1", "after.2") or ("after.1",)
if i == 0 and excluded in (
("after.1", "after.2"),
("after.1",),
):
break
# bi: ("after.0", "after.2") or ("after.0")
elif i == 1 and excluded in (
("after.0", "after.2"),
("after.0",),
# joint
("after.2",),
):
break
return y_hat, meta
def loss(
self,
scores: torch.Tensor,
ground_truth: torch.Tensor,
meta: dict[str, torch.Tensor],
take_mean: bool = True,
loss: torch.Tensor | None = None,
) -> torch.Tensor:
assert loss is None
loss = 0
if not self.residual or self.exclude_parameters_prefix:
return super().loss(scores, ground_truth, meta, take_mean=take_mean)
uni = super().loss(meta["meta_y_hat_0"], ground_truth, meta)
previous = meta["meta_y_hat_0"].detach()
bi = super().loss(meta["meta_y_hat_1"] + previous, ground_truth, meta)
previous = previous + meta["meta_y_hat_1"].detach()
tri = 0.0
if "meta_y_hat_2" in meta:
tri = super().loss(meta["meta_y_hat_2"] + previous, ground_truth, meta)
return loss + uni + bi + tri
class MLP_parallel(neural.LossModule):
# additive baseline
def __init__(
self,
*,
input_size: int = -1,
input_sizes: tuple[int, ...],
**kwargs,
) -> None:
super().__init__(
loss_function=kwargs.get("loss_function", "MSELoss"),
attenuation=kwargs.get("attenuation", ""),
attenuation_lambda=kwargs.get("attenuation_lambda", 0.0),
sample_weight=kwargs.get("sample_weight", None),
training_validation=kwargs.get("training_validation", False),
)
input_sizes = tuple(x for x in input_sizes if x)
assert input_size == sum(input_sizes)
self.view_starts: Final = torch.cumsum(
torch.LongTensor([0] + list(input_sizes)), dim=0
)
fun = neural.MLP
self.before = torch.nn.ModuleList(
[fun(input_size=size, **kwargs) for size in input_sizes]
)
self.jit_me = False
def forward(
self,
x: torch.Tensor,
meta: dict[str, torch.Tensor],
y: torch.Tensor | None = None,
dataset: str = "",
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
xs = []
for i, start in enumerate(self.view_starts[:-1]):
x_, meta = self.before[i](
x[:, start : self.view_starts[i + 1]], meta, y=y, dataset=dataset
)
xs.append(x_)
meta[f"meta_tfn_embedding_{i}"] = meta["meta_embedding"]
x = torch.stack(xs, dim=-1)
meta["meta_tfn_embedding"] = torch.cat(
[
meta[f"meta_tfn_embedding_{i}"]
for i in range(self.view_starts.shape[0] - 1)
],
dim=1,
)
meta["meta_embedding"] = x.view(x.shape[0], -1)
meta["meta_parallel_y"] = meta["meta_embedding"]
return x.sum(dim=-1), meta
class MLP_detached_residual(neural.LossModule):
# uni-modalS + additive bi-modalS on residual + tri-modal on residual
def __init__(
self,
*,
input_size: int = -1,
input_sizes: tuple[int, ...],
naive_loss: bool = False,
only_tri: bool = False,
**kwargs,
) -> None:
super().__init__(
loss_function=kwargs.get("loss_function", "MSELoss"),
attenuation=kwargs.get("attenuation", ""),
attenuation_lambda=kwargs.get("attenuation_lambda", 0.0),
sample_weight=kwargs.get("sample_weight", None),
training_validation=kwargs.get("training_validation", False),
)
input_sizes = tuple(x for x in input_sizes if x)
assert input_size == sum(input_sizes)
self.naive_loss: Final = naive_loss
self.only_tri: Final = only_tri
self.view_starts: Final = torch.cumsum(
torch.LongTensor([0] + list(input_sizes)), dim=0
)
if only_tri:
assert naive_loss
self.jit_me = False
self.uni_modals = MLP_parallel(
input_size=input_size,
input_sizes=input_sizes,
**kwargs,
)
fun = neural.MLP
self.tri_modal = fun(input_size=input_size, **kwargs)
if len(input_sizes) == 3:
self.bi_a_modal = fun(input_size=input_sizes[0] + input_sizes[1], **kwargs)
self.bi_a_index = torch.zeros(input_size, dtype=bool)
self.bi_a_index[: input_sizes[0] + input_sizes[1]] = True
self.bi_b_modal = fun(input_size=input_sizes[1] + input_sizes[2], **kwargs)
self.bi_b_index = torch.zeros(input_size, dtype=bool)
self.bi_b_index[-input_sizes[1] - input_sizes[2] :] = True
self.bi_c_modal = fun(input_size=input_sizes[0] + input_sizes[2], **kwargs)
self.bi_c_index = torch.zeros(input_size, dtype=bool)
self.bi_c_index[: input_sizes[0]] = True
self.bi_c_index[-input_sizes[-1] :] = True
def forward(
self,
x: torch.Tensor,
meta: dict[str, torch.Tensor],
y: torch.Tensor | None = None,
dataset: str = "",
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
y_uni, meta = self.uni_modals(x, meta, y=y, dataset=dataset)
meta["meta_uni_y"] = y_uni
y_bis = 0
if self.view_starts.shape[0] == 4 and self.exclude_parameters_prefix != (
"bi_",
"tri_",
):
y_bi_a, meta = self.bi_a_modal(
x[:, self.bi_a_index], meta, y=y, dataset=dataset
)
meta["meta_bi_y_a"] = y_bi_a
y_bi_b, meta = self.bi_b_modal(
x[:, self.bi_b_index], meta, y=y, dataset=dataset
)
meta["meta_bi_y_b"] = y_bi_b
y_bi_c, meta = self.bi_c_modal(
x[:, self.bi_c_index], meta, y=y, dataset=dataset
)
meta["meta_bi_y_c"] = y_bi_c
y_bis = y_bi_a + y_bi_b + y_bi_c
meta["meta_bi_y"] = y_bis
y_tri = 0
if "tri_" not in self.exclude_parameters_prefix:
y_tri, meta = self.tri_modal(x, meta, y=y, dataset=dataset)
meta["meta_tri_y"] = y_tri
if self.only_tri:
y_uni = 0
y_bis = 0
meta["meta_uni_y"] = meta["meta_uni_y"] * 0
if "meta_bi_y" in meta:
meta["meta_bi_y"] = meta["meta_bi_y"] * 0
return y_uni + y_bis + y_tri, meta
def loss(
self,
scores: torch.Tensor,
ground_truth: torch.Tensor,
meta: dict[str, torch.Tensor],
take_mean: bool = True,
loss: torch.Tensor | None = None,
) -> torch.Tensor:
assert loss is None
loss = 0
if self.naive_loss or self.exclude_parameters_prefix:
return loss + super().loss(scores, ground_truth, meta, take_mean=take_mean)
uni = super().loss(meta["meta_uni_y"], ground_truth, meta)
previous = meta["meta_uni_y"].detach()
bi = 0
if "meta_bi_y" in meta:
bi = super().loss(meta["meta_bi_y"] + previous, ground_truth, meta)
previous = previous + meta["meta_bi_y"].detach()
tri = super().loss(meta["meta_tri_y"] + previous, ground_truth, meta)
return loss + uni + bi + tri
def get_modalities(xs: list[str]) -> dict[str, list[str]]:
return {
"vision": [
x
for x in xs
if x.startswith("openface")
or x == "duchenne_smile_ratio"
or x.startswith("detr")
or x.startswith("resnet")
or x.startswith("afar")
],
"acoustic": [
x
for x in xs
if x.startswith("opensmile")
or x.startswith("covarep")
or x.startswith("volume")
],
"language": [x for x in xs if x.startswith("roberta") or x.startswith("liwc")],
}
def train(
partitions: dict[int, dict[str, DataLoader]], folder: Path, args: argparse.Namespace
) -> None:
metric = "ccc"
metric_fun = metrics.interval_metrics
params = {
"interval": True,
"metric_max": True,
"y_names": partitions[0]["training"].properties["y_names"].copy(),
}
grid_search = {
"epochs": [10_000],
"early_stop": [300],
"lr": [0.001, 0.0001, 0.00001],
"dropout": [0.0],
"layers": [0],
"layer_sizes": [(5,), (10,), (20, 10), (10, 20)],
"activation": [{"name": "ReLU"}],
"attenuation": [""],
"sample_weight": [True],
"final_activation": [{"name": "linear"}],
"minmax": [False, True],
"weight_decay": [1e-4, 1e-3, 1e-2],
}
if args.dimension in ("uni", "bi", "tri"):
grid_search["epochs"] = [1_000]
grid_search["minmax"] = [False]
grid_search["weight_decay"] = [0.0]
grid_search["layer_sizes"] = [(5,), (10,), (10, 10)]
metric = "mse"
params["metric_max"] = False
elif args.dimension in MAE_DIMENSION:
grid_search["lr"].extend([0.005, 0.01, 0.05])
grid_search["layer_sizes"].extend([(100, 20, 10), (100, 100, 10)])
grid_search["weight_decay"].extend([0.0])
metric = "mae"
params["metric_max"] = False
grid_search["loss_function"] = ["L1Loss"]
elif args.dimension in CLASS_DIMENSION:
grid_search["lr"].extend([0.005, 0.01, 0.05])
if args.dimension != "intent":
grid_search["layer_sizes"].extend([(100, 20, 10), (100, 100, 10)])
grid_search["weight_decay"].extend([0.0])
grid_search["sample_weight"] = [False]
grid_search["minmax"] = [False]
params["interval"] = False
params["nominal"] = True
params["y_names"] = partitions[0]["training"].properties["y_names"].copy()
metric_fun = metrics.nominal_metrics
metric = "brier_score"
params["metric_max"] = False
if args.routing:
# need more layers
grid_search["layer_sizes"] = [
x if len(x) > 1 else x + (5,) for x in grid_search["layer_sizes"]
]
model = MLPModel(device="cuda", **params)
x_names = partitions[0]["training"].properties["x_names"]
modalities = get_modalities(x_names.tolist())
assert partitions[0]["training"].properties["x_names"].tolist() == sum(
modalities.values(), []
)
# add feature names
new_names = ["input_sizes"]
if args.routing:
# routing:
grid_search["model_class"] = [RoutingTFN]
elif not args.mult:
# early fusion
grid_search["model_class"] = [MLP_detached_residual]
if args.joint:
# naive joint loss
grid_search["naive_loss"] = [True]
new_names += ["naive_loss"]
if args.tri:
grid_search["only_tri"] = [True]
new_names += ["only_tri"]
if args.stepwise:
# freeze bi+tri, uni+tri, bi+tri
grid_search["exclude_parameters_prefixes"] = [
(("bi_", "tri_"), ("uni_", "tri_"), ("uni_", "bi_"))
]
if args.dimension == "intent" or (
args.dimension in ("arousal",) and args.fs
):
# only two modalities
grid_search["exclude_parameters_prefixes"] = [(("tri_",), ("uni_",))]
else:
# tensor fusion
grid_search["model_class"] = [MLP_mult]
if args.joint:
# Joint
if args.tri:
# Tri
grid_search["only_tri"] = [True]
new_names += ["only_tri"]
elif args.res_det:
# MRO
grid_search["residual"] = [True]
new_names += ["residual"]
if args.stepwise and not args.joint:
# sMRO
# freeze bi+tri, uni+tri, bi+tri
grid_search["exclude_parameters_prefixes"] = [
(
("after.1", "after.2"),
("after.0", "after.2"),
("after.0", "after.1"),
)
]
if args.dimension == "intent" or (
args.dimension in ("arousal",) and args.fs
):
# only two modalities
grid_search["exclude_parameters_prefixes"] = [
(("after.1",), ("after.0",))
]
grid_search["input_sizes"] = [
(
tuple(modalities["vision"]),
tuple(modalities["acoustic"]),
tuple(modalities["language"]),
)
]
model.forward_names = tuple(list(model.forward_names) + new_names)
model.parameters.update(grid_search)
models, parameters, model_transform = model.get_models()
apply_transformation = partial(
combine_transformations, model_transform=model_transform
)
transform = DefaultTransformations(**params)
transforms = tuple([{"feature_selection": args.fs} for _ in range(len(partitions))])
print(folder, len(parameters))
for key, value in model.parameters.items():
if len(value) > 1:
print(len(value), key, value)
evaluator(
models=models,
partitions=partitions,
parameters=parameters,
folder=folder,
metric_fun=partial(
metric_fun,
clustering=args.dimension in CCC_DIMENSION
or args.dimension == "constructs",
names=tuple(params["y_names"].tolist()),
),
metric=metric,
metric_max=params["metric_max"],
learn_transform=transform.define_transform,
apply_transform=apply_transformation,
revert_transform=revert_transform,
transform_parameter=transforms,
workers=args.workers,
parallel="local",
memory_limit=None,
)
def combine_transformations(data, transform, model_transform=None):
if data.properties["y_names"][0] in ("uni", "bi"):
transform["x"]["mean"][:] = 0
transform["x"]["std"][:] = 1
transform["y"]["mean"][:] = 0
transform["y"]["std"][:] = 1
data = set_transform(data, transform)
data.add_transform(model_transform, optimizable=True)
return data
if __name__ == "__main__":
# argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--dimension",
choices=list(CCC_DIMENSION) + list(MAE_DIMENSION) + list(CLASS_DIMENSION),
default="valence",
)
parser.add_argument("--routing", action="store_const", const=True, default=False)
parser.add_argument("--fs", action="store_const", const=True, default=False)
parser.add_argument("--mult", action="store_const", const=True, default=False)
parser.add_argument("--res_det", action="store_const", const=True, default=False)
parser.add_argument("--joint", action="store_const", const=True, default=False)
parser.add_argument("--stepwise", action="store_const", const=True, default=False)
parser.add_argument("--tri", action="store_const", const=True, default=False)
parser.add_argument("--workers", type=int, default=12)
args = parser.parse_args()
if args.dimension in ("uni", "bi", "tri") and not args.routing:
if args.fs:
exit(code=0)
if args.dimension in ("bi", "tri") and not args.mult:
exit(code=0)
if args.dimension == "uni" and args.mult:
exit(code=0)
# choose dataloader
folder = Path("experiments") / namespace_as_string(args, exclude=("workers",))
loader_class = SEWA
folds = 5
if args.dimension == "mosi":
loader_class = MOSI
elif args.dimension[0] != args.dimension[0].lower():
loader_class = IEMOCAP
elif args.dimension in ("uni", "bi"):
loader_class = Test
elif args.dimension in MAE_DIMENSION:
loader_class = MOSEI
elif args.dimension == "constructs":
loader_class = PANAM
elif args.dimension == "intent":
loader_class = Instagram
data = {
i: {
name: loader_class(
ifold=i, name=name, dimension=args.dimension
).get_loader()
for name in ("training", "validation", "test")
}
for i in range(folds)
}
train(data, folder, args)