-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_external.py
More file actions
2049 lines (1682 loc) · 70.1 KB
/
train_external.py
File metadata and controls
2049 lines (1682 loc) · 70.1 KB
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
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import functools
import os
import time
from absl import app
from absl import flags
from absl import logging
import json
from flax import jax_utils
from flax import nn
from flax import optim
from flax import serialization
from flax.metrics import tensorboard
from flax.training import checkpoints
from flax.training import common_utils
import jax
from jax import lax
from jax import random
import jax.nn
import jax.numpy as jnp
#from flax_nlp.nn import recurrent
import numpy as onp
import numpy as np
import tensorflow.compat.v2 as tf
import input_pipeline
FLAGS = flags.FLAGS
flags.DEFINE_string(
'model_dir',
default='models/ttl=30d/eeg/',
help=('Directory for model data'))
flags.DEFINE_integer(
'batch_size', default=32, help=('Batch size for training.'))
flags.DEFINE_integer(
'eval_frequency',
default=125,
help=('Frequency of eval during training, e.g. every 1000 steps.'))
flags.DEFINE_integer(
'num_train_steps', default=200000, help=('Number of train steps.'))
flags.DEFINE_float('learning_rate', default=0.01, help=('Learning rate.'))
flags.DEFINE_float(
'weight_decay',
default=0.1,
help=('decay factor for AdamW style weight decay.'))
flags.DEFINE_integer(
'offset', default=0, help=('decay factor for AdamW style weight decay.'))
flags.DEFINE_integer(
'max_target_length',
default=176,
help=('maximum length of training examples.'))
flags.DEFINE_enum('classifier_type', 'transformer', ['transformer', 'lstm'],
'The classifier used.')
flags.DEFINE_string('n_avg_train', default='10_', help='avg train')
flags.DEFINE_string('n_avg_test', default='10_', help='avg test')
flags.DEFINE_string('what', default='alex', help='experiments')
flags.DEFINE_string(
'data_path',
'content/drive/My Drive/Bernd_EEG/data',
help='data path')
flags.DEFINE_integer('step_size', default=2, help=('step size for the window.'))
flags.DEFINE_integer('random_seed', default=0, help=('random seed.'))
flags.DEFINE_integer(
'window_size', default=30, help=('window size for the window.'))
SWITCH_ACC = False
@functools.partial(jax.jit, static_argnums=(1, 2, 3))
def create_model(key, input_shape, model_kwargs, cl_type):
"""Crate model."""
print('creating model', cl_type)
if cl_type == 'transformer':
model_def = Transformer.partial(train=False, **model_kwargs)
elif cl_type == 'lstm':
print('creating lstm model')
model_def = LSTM.partial(train=False, **model_kwargs)
else:
raise f'Not defined classifier type {cl_type}'
_, params = model_def.init_by_shape(key, [input_shape])
model = nn.Model(model_def, params)
return model
class LSTM(nn.Module):
"""LSTM Model for eeg data."""
def apply(self,
inputs,
output_vocab_size,
num_layers=2,
max_len=2048,
train=True,
dropout_rate=0.3):
"""Applies LSTM model on the inputs.
Args:
inputs: input data
output_vocab_size: size of the output classes
num_layers: number of layers
max_len: maximum length.
train: if it is training,
dropout_rate: dropout rate
Returns:
the logits of the clasification per eeg time step.
"""
assert inputs.ndim == 3
x = inputs.astype('float32')
x = nn.dropout(x, rate=dropout_rate, deterministic=not train)
lens = jnp.full((x.shape[0]), max_len)
x, _ = recurrent.LSTM(
x,
lens,
hidden_size=300,
num_layers=num_layers,
bidirectional=True,
dropout_rate=0,
recurrent_dropout_rate=dropout_rate,
train=train)
x = nn.dropout(x, rate=dropout_rate, deterministic=not train)
x = nn.LayerNorm(x)
logits = nn.Dense(
x,
output_vocab_size,
kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6))
return logits
def shard(xs):
local_device_count = jax.local_device_count()
return jax.tree_map(
lambda x: x.reshape((local_device_count, -1) + x.shape[1:]), xs)
def shard_prng_key(prng_key):
# PRNG keys can used at train time to drive stochastic modules
# e.g. DropOut. We would like a different PRNG key for each local
# device so that we end up with different random numbers on each one,
# hence we split our PRNG key and put the resulting keys into the batch
return jax.random.split(prng_key, num=jax.local_device_count())
def onehot(labels, num_classes):
x = (labels[..., None] == jnp.arange(num_classes)[None])
return x.astype(jnp.float32)
def pmean(tree, axis_name='batch'):
num_devices = lax.psum(1., axis_name)
return jax.tree_map(lambda x: lax.psum(x, axis_name) / num_devices, tree)
def psum(tree, axis_name='batch'):
return jax.tree_map(lambda x: lax.psum(x, axis_name), tree)
def stack_forest(forest):
stack_args = lambda *args: onp.stack(args)
return jax.tree_multimap(stack_args, *forest)
def get_metrics(device_metrics):
device_metrics = jax.tree_map(lambda x: x[0], device_metrics)
metrics_np = jax.device_get(device_metrics)
return stack_forest(metrics_np)
"""Transformer-based langauge models."""
#from flax import nn
#import jax.numpy as jnp
#import numpy as np
class Embed(nn.Module):
"""Embedding Module.
A parameterized function from integers [0, n) to d-dimensional vectors.
"""
def apply(self,
inputs,
num_embeddings,
features,
mode='input',
emb_init=nn.initializers.normal(stddev=1.0)):
"""Applies Embed module.
Args:
inputs: input data
num_embeddings: number of embedding
features: size of the embedding dimension
mode: either 'input' or 'output' -> to share input/output embedding
emb_init: embedding initializer
Returns:
output which is embedded input data
"""
embedding = self.param('embedding', (num_embeddings, features), emb_init)
if mode == 'input':
if inputs.dtype not in [jnp.int32, jnp.int64, jnp.uint32, jnp.uint64]:
raise ValueError('Input type must be an integer or unsigned integer.')
return jnp.take(embedding, inputs, axis=0)
if mode == 'output':
return jnp.einsum('bld,vd->blv', inputs, embedding)
def sinusoidal_init(max_len=2048):
"""1D Sinusoidal Position Embedding Initializer.
Args:
max_len: maximum possible length for the input
Returns:
output: init function returning `(1, max_len, d_feature)`
"""
def init(key, shape, dtype=np.float32):
"""Sinusoidal init."""
del key, dtype
d_feature = shape[-1]
print(f'd_feature / shape[-1] is {d_feature}')
pe = np.zeros((max_len, d_feature), dtype=np.float32)
position = np.arange(0, max_len)[:, np.newaxis]
div_term = np.exp(
np.arange(0, d_feature, 2) * -(np.log(10000.0) / d_feature))
pe[:, 0::2] = np.sin(position * div_term)
pe[:, 1::2] = np.cos(position * div_term)
pe = pe[np.newaxis, :, :] # [1, max_len, d_feature]
return jnp.array(pe)
return init
class AddPositionEmbs(nn.Module):
"""Adds learned positional embeddings to the inputs."""
def apply(self,
inputs,
max_len=2048,
posemb_init=nn.initializers.normal(stddev=1.0)):
"""Applies AddPositionEmbs module.
Args:
inputs: input data
max_len: maximum possible length for the input
posemb_init: positional embedding initializer
Returns:
output: `(bs, timesteps, in_dim)`
"""
assert inputs.ndim == 3, ('Number of dimention should be 3, but it is: %d' %
inputs.ndim)
length = inputs.shape[1]
pos_emb_shape = (1, max_len, inputs.shape[-1])
pos_embedding = self.param('pos_embedding', pos_emb_shape, posemb_init)
return inputs + pos_embedding[:, :length, :]
class MlpBlock(nn.Module):
"""Transformer MLP block."""
def apply(self,
inputs,
mlp_dim,
out_dim=None,
dropout_rate=0.3,
deterministic=False,
kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6)):
"""Applies Transformer MlpBlock module."""
actual_out_dim = inputs.shape[-1] if out_dim is None else out_dim
x = nn.Dense(inputs, mlp_dim, kernel_init=kernel_init, bias_init=bias_init)
x = nn.gelu(x)
x = nn.dropout(x, rate=dropout_rate, deterministic=deterministic)
output = nn.Dense(
x, actual_out_dim, kernel_init=kernel_init, bias_init=bias_init)
output = nn.dropout(output, rate=dropout_rate, deterministic=deterministic)
return output
class Transformer1DBlock(nn.Module):
"""Transformer layer (https://openreview.net/forum?id=H1e5GJBtDr)."""
def apply(self,
inputs,
qkv_dim,
mlp_dim,
num_heads,
causal_mask=False,
padding_mask=None,
dropout_rate=0.3,
attention_dropout_rate=0.3,
deterministic=False,
attention=True):
"""Applies Transformer1DBlock module.
Args:
inputs: input data
qkv_dim: dimension of the query/key/value
mlp_dim: dimension of the mlp on top of attention block
num_heads: number of heads
causal_mask: bool, mask future or not
padding_mask: bool, mask padding tokens
dropout_rate: dropout rate
attention_dropout_rate: dropout rate for attention weights
deterministic: bool, deterministic or not (to apply dropout)
Returns:
output after transformer block.
"""
# Attention block.
assert inputs.ndim == 3
x = inputs
if attention:
x = nn.LayerNorm(x)
x = nn.SelfAttention(
x,
num_heads=num_heads,
qkv_features=qkv_dim,
attention_axis=(1,),
causal_mask=causal_mask,
padding_mask=padding_mask,
kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6),
bias=False,
broadcast_dropout=False,
dropout_rate=attention_dropout_rate,
deterministic=deterministic)
x = nn.dropout(x, rate=dropout_rate, deterministic=deterministic)
x = x + inputs
# MLP block.
y = nn.LayerNorm(x)
y = MlpBlock(
y,
mlp_dim=mlp_dim,
dropout_rate=dropout_rate,
deterministic=deterministic)
return x + y
class Transformer(nn.Module):
"""Transformer Model for sequence tagging."""
def apply(self,
inputs,
vocab_size,
output_vocab_size,
emb_dim=512,
num_heads=8,
num_layers=6,
qkv_dim=512,
mlp_dim=2048,
max_len=2048,
train=True,
dropout_rate=0.1,
attention_dropout_rate=0.1,
ablation=''):
"""Applies Transformer model on the inputs.
Args:
inputs: input data
vocab_size: size of the input vocabulary
output_vocab_size: size of the output classes
emb_dim: dimension of embedding
num_heads: number of heads
num_layers: number of layers
qkv_dim: dimension of the query/key/value
mlp_dim: dimension of the mlp on top of attention block
max_len: maximum length.
train: if it is training,
dropout_rate: dropout rate
attention_dropout_rate: dropout rate for attention weights
Returns:
output of a transformer decoder.
"""
print('inputs.shape', inputs.shape)
x = inputs
x = x.astype('float32')
x = nn.dropout(x, rate=dropout_rate, deterministic=not train)
x = AddPositionEmbs(
x, max_len=max_len, posemb_init=sinusoidal_init(max_len=max_len))
if 'dense' in ablation:
x = nn.Dense(x, mlp_dim, kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6))
x = nn.dropout(x, rate=dropout_rate, deterministic=not train)
else:
attention = True
if 'noatt' in ablation:
attention = False
for _ in range(num_layers):
x = Transformer1DBlock(
x,
qkv_dim=qkv_dim,
mlp_dim=mlp_dim,
num_heads=num_heads,
causal_mask=False,
padding_mask=None,
dropout_rate=dropout_rate,
attention_dropout_rate=attention_dropout_rate,
deterministic=not train,
attention=attention)
x = nn.LayerNorm(x)
logits = nn.Dense(
x,
output_vocab_size,
kernel_init=nn.initializers.xavier_uniform(),
bias_init=nn.initializers.normal(stddev=1e-6))
time_dim = 1
num_time_steps = logits.shape[time_dim]
logits = jnp.sum(logits, axis=time_dim)
logits = logits / num_time_steps
return logits
def pad_examples(x, desired_batch_size):
"""Expand batch to desired size by repeating last slice."""
print('pad_examples shape', x.shape)
batch_pad = desired_batch_size - x.shape[0]
return np.concatenate([x, np.tile(x[-1], (batch_pad, 1, 1))], axis=0)
def pad_target(x, desired_batch_size):
"""Expand batch to desired size by repeating last slice."""
print('target shape', x.shape)
batch_pad = desired_batch_size - x.shape[0]
return np.concatenate([x, np.tile(x[-1], (batch_pad))], axis=0)
"""Sequence Tagging example.
This script trains a Transformer on the Universal dependency dataset.
"""
# check if this is still used.
N_WINDOW = 176 # 201 # 151 # 40
N_EMBEDDING = 64
N_AVG_TRAIN = '3_'
N_AVG_TEST = '3_'
N_CLASS = 6
SWITCH_ACC = False
best_acc = 0.0
FLAGS = flags.FLAGS
def create_model(key, input_shape, model_kwargs):
module = Transformer.partial(train=False, **model_kwargs)
@jax.jit
def init(key):
_, initial_params = module.init_by_shape(key, [(input_shape, jnp.float32)])
model = nn.Model(module, initial_params)
return model
return init(key)
def create_optimizer(model, learning_rate):
optimizer_def = optim.Adam(
learning_rate, beta1=0.9, beta2=0.98, eps=1e-9, weight_decay=0)
optimizer = optimizer_def.create(model)
optimizer = optimizer.replicate()
return optimizer
def create_learning_rate_scheduler(
factors='constant * linear_warmup * rsqrt_decay',
base_learning_rate=0.5,
warmup_steps=50000, # 25000
decay_factor=0.5,
steps_per_decay=25000,
steps_per_cycle=100000):
"""creates learning rate schedule.
Interprets factors in the factors string which can consist of:
* constant: interpreted as the constant value,
* linear_warmup: interpreted as linear warmup until warmup_steps,
* rsqrt_decay: divide by square root of max(step, warmup_steps)
* decay_every: Every k steps decay the learning rate by decay_factor.
* cosine_decay: Cyclic cosine decay, uses steps_per_cycle parameter.
Args:
factors: a string with factors separated by '*' that defines the schedule.
base_learning_rate: float, the starting constant for the lr schedule.
warmup_steps: how many steps to warm up for in the warmup schedule.
decay_factor: The amount to decay the learning rate by.
steps_per_decay: How often to decay the learning rate.
steps_per_cycle: Steps per cycle when using cosine decay.
Returns:
a function learning_rate(step): float -> {'learning_rate': float}, the
step-dependent lr.
"""
factors = [n.strip() for n in factors.split('*')]
def step_fn(step):
"""Step to learning rate function."""
ret = 1.0
for name in factors:
if name == 'constant':
ret *= base_learning_rate
elif name == 'linear_warmup':
ret *= jnp.minimum(1.0, step / warmup_steps)
elif name == 'rsqrt_decay':
ret /= jnp.sqrt(jnp.maximum(step, warmup_steps))
elif name == 'rsqrt_normalized_decay':
ret *= jnp.sqrt(warmup_steps)
ret /= jnp.sqrt(jnp.maximum(step, warmup_steps))
elif name == 'decay_every':
ret *= (decay_factor**(step // steps_per_decay))
elif name == 'cosine_decay':
progress = jnp.maximum(0.0,
(step - warmup_steps) / float(steps_per_cycle))
ret *= jnp.maximum(0.0,
0.5 * (1.0 + jnp.cos(jnp.pi * (progress % 1.0))))
else:
raise ValueError('Unknown factor %s.' % name)
return jnp.asarray(ret, dtype=jnp.float32)
return step_fn
def compute_weighted_cross_entropy(logits, targets, weights=None):
"""Compute weighted cross entropy and entropy for log probs and targets.
Args:
logits: [batch, length, num_classes] float array.
targets: categorical targets [batch, length] int array.
weights: None or array of shape [batch x length]
Returns:
Tuple of scalar loss and batch normalizing factor.
"""
if logits.ndim == 3:
num_classes = logits.shape[-1]
onehot_targets = onehot(targets, num_classes)
# Now, onehot_targets and logits should be completely matching now in shape
# Both should be 3-dimensional of the form [batch, length, num_classes]
log_softmax_targets = nn.log_softmax(logits)
onehot_softmax = onehot_targets * log_softmax_targets
loss = -jnp.sum(onehot_softmax, axis=-1)
normalizing_factor = onehot_targets.sum()
if logits.ndim == 2:
num_classes = logits.shape[-1]
#targets = targets[:,0:1]
onehot_targets = onehot(targets, num_classes)
log_softmax_targets = nn.log_softmax(logits)
onehot_softmax = onehot_targets * log_softmax_targets
loss = -jnp.sum(onehot_softmax, axis=-1)
normalizing_factor = onehot_targets.sum()
if weights is not None:
loss = loss * weights
normalizing_factor = weights.sum()
return loss.sum(), normalizing_factor
def compute_weighted_accuracy(logits, targets, weights=None):
"""Compute weighted accuracy for log probs and targets.
Args:
logits: [batch, length, num_classes] float array.
targets: categorical targets [batch, length] int array.
weights: None or array of shape [batch x length]
Returns:
Tuple of scalar accuracy and batch normalizing factor.
"""
total = jnp.argmax(logits, axis=-1)
loss = jnp.equal(total, targets)
normalizing_factor = np.prod(logits.shape[:-1])
return loss.sum(), normalizing_factor
def compute_metrics(logits, labels, weights):
"""Compute summary metrics."""
loss, weight_sum = compute_weighted_cross_entropy(logits, labels, weights)
acc, normalizing_factor = compute_weighted_accuracy(logits, labels, weights)
metrics = {
'loss': loss,
'accuracy': acc,
'denominator': weight_sum,
'normalizing_factor': normalizing_factor,
}
metrics = psum(metrics)
return metrics
def train_step(optimizer, batch, learning_rate_fn, dropout_rng=None):
"""Perform a single training step."""
inputs, targets = batch
print(f'inputs shape is {inputs.shape}')
n_batch, n_time, n_embed = inputs.shape
print(f'targets (2) shape is {targets.shape}')
# warning set all to 1 for eeg data.
weights = jnp.ones_like(targets).astype(jnp.float32)
dropout_rng, new_dropout_rng = random.split(dropout_rng)
def loss_fn(model):
"""Loss function used for training."""
with nn.stochastic(dropout_rng):
logits = model(inputs, train=True)
loss, weight_sum = compute_weighted_cross_entropy(logits, targets, weights)
mean_loss = loss / weight_sum
return mean_loss, logits
step = optimizer.state.step
lr = learning_rate_fn(step)
new_optimizer, _, logits = optimizer.optimize(loss_fn, learning_rate=lr)
metrics = compute_metrics(logits, targets, weights)
metrics['learning_rate'] = lr
return new_optimizer, metrics, new_dropout_rng
def eval_step(model, batch):
"""Calculate evaluation metrics on a batch."""
inputs, targets = batch
n_batch, n_time, n_embed = inputs.shape
weights = jnp.ones_like(targets).astype(jnp.float32)
logits = model(inputs, train=False)
argmax = jnp.argmax(logits, axis=-1)
return compute_metrics(logits, targets, weights), argmax
# evaluate
def evaluate(eval_ds,
num_eval_steps,
best_acc,
batch_size,
p_eval_step,
optimizer,
step,
train_accuracy, # remove never used !
summary_writer,
eval_type='eval',
eval_output=None,
eval_output_force=False):
eval_metrics = []
eval_iter = iter(eval_ds)
acc = 0
total, xt = 0, 0
argmax_batch = []
targets_batch = []
correct_wrong = []
for eval_batch in eval_iter:
cur_pred_batch_size = eval_batch[0].shape[0]
if cur_pred_batch_size != batch_size and jax.device_count() > 1:
desired_batch_size = jax.device_count()
source = eval_batch[0].numpy()
target = eval_batch[1].numpy()
source_pad = pad_examples(source, jax.device_count())
target_pad = pad_target(target, jax.device_count())
padded = (source_pad, target_pad)
eval_batch = padded
eval_batch = shard(eval_batch)
metrics, argmax = p_eval_step(optimizer.target, eval_batch)
eval_metrics.append(metrics)
else:
eval_batch = shard(jax.tree_map(lambda x: x._numpy(), eval_batch))
metrics, argmax = p_eval_step(optimizer.target, eval_batch)
eval_metrics.append(metrics)
argmax_batch.append(argmax)
targets_batch.append(eval_batch[1])
cnt = 0
correct = 0
class_dist = {}
pred_dist = {}
for batch, trg in zip(argmax_batch, targets_batch):
for a, t in zip(batch, trg):
for ax, tx in zip(a, t):
single_pred = {}
if isinstance(ax, np.int32): # using a sum
single_pred[ax] = single_pred.get(ax, 0) + 1
else: # using voting
for x in ax:
single_pred[x] = single_pred.get(x, 0) + 1
best = max(single_pred, key=single_pred.get)
class_dist[tx] = class_dist.get(tx, 0) + 1
pred_dist[best] = pred_dist.get(best, 0) + 1
cnt += 1
if best == tx:
correct_wrong.append((1, best, tx))
correct += 1
else:
correct_wrong.append((0, best, tx))
if cnt != 0:
accuracy = (correct / cnt)
else:
accuracy = 0
print('cnt, correct', cnt, correct, accuracy)
print('class_dist', class_dist)
print('pred_dist', pred_dist)
eval_metrics = get_metrics(eval_metrics)
eval_metrics_sums = jax.tree_map(jnp.sum, eval_metrics)
eval_denominator = eval_metrics_sums.pop('denominator')
normalizing_factor = eval_metrics_sums.pop('normalizing_factor')
logging.info('normalizing_factor %d, eval_denominator %d',
normalizing_factor, eval_denominator)
eval_summary = jax.tree_map(lambda x: x / eval_denominator, eval_metrics_sums)
print('eval_summary', eval_summary)
del eval_summary['loss']
eval_summary['accuracy'] = accuracy
print(f'step:{step}, {eval_type} accuracy: {accuracy:.2f}')
# best best_acc is past
if accuracy > best_acc:
best_acc = accuracy
print('wirte evals to ', eval_output)
with tf.io.gfile.GFile(eval_output, mode='w') as f:
for (c, b, t) in correct_wrong:
out_str = str(c) + '\t' + str(b) + '\t' + str(t) + '\n'
f.write(out_str)
if eval_output_force:
print('write evals to ', eval_output)
with tf.io.gfile.GFile(eval_output, mode='w') as f:
for (c, b, t) in correct_wrong:
out_str = str(c) + '\t' + str(b) + '\t' + str(t) + '\n'
f.write(out_str)
logging.info('eval in step: %d accuracy: %.4f', step,
eval_summary['accuracy'])
for key, val in eval_summary.items():
summary_writer.scalar(eval_type + '/' + key, val, step)
summary_writer.flush()
return best_acc
def train(offset=0,
window_size=1,
name='v1',
what=None,
random_seed=0,
num_train_steps=400000,
ablation=''):
tf.enable_v2_behavior()
print('random seed', random_seed)
batch_size = 8 # not used anymore? see eval_batch_size !!
learning_rate = 0.04
num_eval_steps = -1
eval_freq = 2000
max_target_length = 176 #226 # 200 #176 #176
eval_batch_size = 8 # multiplier of jax.device_count()
N_EMBEDDING = 64
N_CLASS = 6
logging.info('eval_batch_size', eval_batch_size)
logging.info('start offset', offset)
logging.info('window_size', window_size)
name += '_seed'+str(random_seed)
logging.info('name', name)
logging.info('random_seed', random_seed)
logging.info('data what', what)
#DATA_PATH = 'data/eeg/bigram/10class/avg3/'
#model_dir = 'models/ttl=720d/eeg_ablation/' + name + '/'
if len(ablation) > 1:
model_dir = 'models/ttl=720d/eeg_ablation/' + name + '/'
else:
model_dir = 'models/ttl=720d/eeg_server_windows_v2/' + name + '/'
logging.info('model_dir', model_dir)
tf.io.gfile.makedirs(model_dir)
# test sample without bootstrapping.
filename_pattern_test_wr = None
if what == 'c10_a10_rev_v2':
max_target_length = 226
N_CLASS = 10
DATA_PATH = 'data/eeg/bigram/10class_revision_v2/avg10/'
filename_pattern_test = f'bigram_10class_avg10_test_file*.tfrecords'
filename_pattern_test_wr = f'bigram_10class_avg10_test_wr_file*.tfrecords'
filename_pattern_train = f'bigram_10class_avg10_train_file*.tfrecords'
filename_pattern_dev = f'bigram_10class_avg10_dev_file*.tfrecords'
# For Alex to use
elif what == "alex":
max_target_length = 176
N_CLASS = 6
DATA_PATH = '/content/drive/My Drive/Bernd_EEG/data/unigram_6class/verify_data/'
filename_pattern_test = f'unigram_6class_avg10_test_small_file*.tfrecords'
filename_pattern_test_wr = f'unigram_6class_avg10_test_wr_small_file*.tfrecords'
filename_pattern_train = f'unigram_6class_avg10_train_small_file*.tfrecords'
filename_pattern_dev = f'unigram_6class_avg10_dev_small_file*.tfrecords'
elif what == 'c10_a3_rev_v2':
max_target_length = 226
N_CLASS = 10
DATA_PATH = 'data/eeg/bigram/10class_revision_v2/avg3/'
filename_pattern_test = f'bigram_10class_avg3_test_file*.tfrecords'
filename_pattern_test_wr = f'bigram_10class_avg3_test_wr_file*.tfrecords'
filename_pattern_train = f'bigram_10class_avg3_train_file*.tfrecords'
filename_pattern_dev = f'bigram_10class_avg3_dev_file*.tfrecords'
elif what == 'c10_a1_rev_v2':
max_target_length = 226
N_CLASS = 10
DATA_PATH = 'data/eeg/bigram/10class_revision_v2/avg1/'
filename_pattern_test = f'bigram_10class_avg1_test_file*.tfrecords'
filename_pattern_test_wr = f'bigram_10class_avg1_test_file*.tfrecords' # Same as test files as no bootstrapping was used.
filename_pattern_train = f'bigram_10class_avg1_train_file*.tfrecords'
filename_pattern_dev = f'bigram_10class_avg1_dev_file*.tfrecords'
elif what == 'matched_6class_avg10':
max_target_length = 176
N_CLASS = 6
DATA_PATH = 'data/eeg/matched_6class_avg10/'
filename_pattern_test = f'lexgram_prev_matched_6class_avg10_test_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_prev_matched_6class_avg10_test_wr_file*.tfrecords'
filename_pattern_train = f'lexgram_prev_matched_6class_avg10_train_file*.tfrecords'
filename_pattern_dev = f'lexgram_prev_matched_6class_avg10_dev_file*.tfrecords'
elif what == 'matched_6class_avg3':
max_target_length = 176
N_CLASS = 6
DATA_PATH = 'data/eeg/matched_6class_avg3/'
filename_pattern_test = f'lexgram_prev_matched_6class_avg3_test_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_prev_matched_6class_avg3_test_wr_file*.tfrecords'
filename_pattern_train = f'lexgram_prev_matched_6class_avg3_train_file*.tfrecords'
filename_pattern_dev = f'lexgram_prev_matched_6class_avg3_dev_file*.tfrecords'
elif what == 'single_trial':
max_target_length = 226
DATA_PATH = 'data/eeg/single_trial/'
filename_pattern_test = f'bigram_10class_avg1_test_file*.tfrecords'
filename_pattern_train = f'bigram_10class_avg1_train_file*.tfrecords'
filename_pattern_dev = f'bigram_10class_avg1_dev_file*.tfrecords'
elif what == 'matched_6class_avg1':
max_target_length = 176
N_CLASS = 6
DATA_PATH = 'data/eeg/matched_6class_avg1/'
filename_pattern_test = f'lexgram_prev_matched_6class_avg1_test_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_prev_matched_6class_avg1_test_file*.tfrecords'
filename_pattern_train = f'lexgram_prev_matched_6class_avg1_train_file*.tfrecords'
filename_pattern_dev = f'lexgram_prev_matched_6class_avg1_dev_file*.tfrecords'
elif what == 'avg10_c10_revision':
max_target_length = 226
DATA_PATH= 'data/eeg/bigram/10class_revision/avg10/'
filename_pattern_test = f'bigram_10class_avg10_test_file*.tfrecords'
filename_pattern_train = f'bigram_10class_avg10_train_file*.tfrecords'
filename_pattern_dev = f'bigram_10class_avg10_dev_file*.tfrecords'
elif what == 'unmatched_6class_avg1':
DATA_PATH= 'data/eeg/lexgram_unmatched_6class_avg1/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = 'lexgram_unmatched_6class_single_trials_test_file*.tfrecords'
filename_pattern_test_wr = 'lexgram_unmatched_6class_single_trials_test_file*.tfrecords'
filename_pattern_train = 'lexgram_unmatched_6class_single_trials_train_file*.tfrecords'
filename_pattern_dev = 'lexgram_unmatched_6class_single_trials_dev_file*.tfrecords'
elif what == 'unmatched_6class_avg3':
DATA_PATH= 'data/eeg/lexgram_unmatched_6class_avg3/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg3_test_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg3_test_wr_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_avg3_train_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg3_dev_file*.tfrecords'
elif what == 'unmatched_6class_avg10':
DATA_PATH= 'data/eeg/lexgram_unmatched_6class_avg10/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg10_test_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg10_test_wr_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_avg10_train_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg10_dev_file*.tfrecords'
elif what == 'bigger_unmatched_6class_avg1':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg1/'
max_target_length = 176
N_CLASS = 6 # 6class_single_trials_train_bigger
filename_pattern_test = f'lexgram_unmatched_6class_single_trials_test_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_single_trials_test_bigger_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_single_trials_train_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_single_trials_dev_bigger_file*.tfrecords'
elif what == 'bigger_unmatched_6class_avg3':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg3/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg3_test_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg3_test_wr_bigger_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_avg3_train_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg3_dev_bigger_file*.tfrecords'
elif what == 'bigger_unmatched_6class_avg10':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg10/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg10_test_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg10_test_wr_bigger_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_avg10_train_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg10_dev_bigger_file*.tfrecords'
# wr unmatched
elif what == 'wr_unmatched_6class_avg1':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg1/'
max_target_length = 176
N_CLASS = 6 # 6class_single_trials_train_bigger
filename_pattern_test = f'lexgram_unmatched_6class_single_trials_test_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_single_trials_test_bigger_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_single_trials_train_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_single_trials_dev_bigger_file*.tfrecords'
elif what == 'wr_unmatched_6class_avg3':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg3/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg3_test_wr_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg3_test_wr_bigger_file*.tfrecords'
# lexgram_unmatched_6class_avg3_train_wr_bigger_file0.tfrecords
filename_pattern_train = f'lexgram_unmatched_6class_avg3_train_wr_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg3_dev_wr_bigger_file*.tfrecords'
elif what == 'wr_unmatched_6class_avg10':
DATA_PATH= 'data/eeg/bigger_lexgram_unmatched_6class_avg10/'
max_target_length = 176
N_CLASS = 6
filename_pattern_test = f'lexgram_unmatched_6class_avg10_test_wr_bigger_file*.tfrecords'
filename_pattern_test_wr = f'lexgram_unmatched_6class_avg10_test_wr_bigger_file*.tfrecords'
filename_pattern_train = f'lexgram_unmatched_6class_avg10_train_wr_bigger_file*.tfrecords'
filename_pattern_dev = f'lexgram_unmatched_6class_avg10_dev_wr_bigger_file*.tfrecords'