-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaa_simtrain2.py
1197 lines (938 loc) · 45.3 KB
/
aa_simtrain2.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
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
from typing import Union, List
from transformer_infrastructure.hf_embed import parse_fasta_for_embed
from transformers import BertTokenizerFast, AdamW, BertModel, get_linear_schedule_with_warmup, PreTrainedModel, PretrainedConfig, BertConfig, BertPreTrainedModel, TrainingArguments, EarlyStoppingCallback, EvalPrediction
from Bio import SeqIO
from Bio.Align import MultipleSeqAlignment
from Bio import AlignIO
from Bio.Seq import Seq
from torch.utils.data import Dataset, TensorDataset, DataLoader, RandomSampler, SequentialSampler
from sentence_transformers import LoggingHandler, SentenceTransformer, models, evaluation
import argparse
import numpy as np
import torch
from torch import nn
import re
from transformer_infrastructure.hf_utils import AA
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
import random
import os
import pandas as pd
import copy
import json
from pytorch_lightning import LightningModule
from torchmetrics import Accuracy
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
def get_aasim_args():
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--model", dest = "model_path", type = str, required = True,
help="Model directory path or name on huggingface. Ex. /path/to/model_dir Rostlab/prot_bert_bfd")
parser.add_argument("-trfasta", "--trainfastas", dest = "train_fastas", type = str, required = True,
help="Path to file containing one fasta file per line. Each fasta corresponds to one gold standard alignment")
parser.add_argument("-traln", "--trainalns", dest = "train_alns", type = str, required = True,
help="Path to files containine one gold standard alignment file in fasta format per line, with dashes for gaps")
parser.add_argument("-dvfasta", "--devfastas", dest = "dev_fastas", type = str, required = True,
help="Path to file containing one fasta file per line. Each fasta corresponds to one gold standard alignment")
parser.add_argument("-dvaln", "--devalns", dest = "dev_alns", type = str, required = True,
help="Path to files containine one gold standard alignment file in fasta format per line, with dashes for gaps")
parser.add_argument("-l", "--layers", dest = "layers", nargs="+", type=int, default = [-4,-3,-2,-1],
help="Which layers") #layers = [-4,-3,-2,-1]
parser.add_argument("-o", "--outdir", dest = "outdir", type = str, required = True,
help="Output directory to save final model")
# Training arguments
parser.add_argument("-maxl", "--maxseqlength", dest = "max_length", type = int, required = False, default = 1024,
help="Truncate all sequences to this length (default 1024). Reduce if memory errors")
parser.add_argument("-n", "--expname", dest = "expname", type = str, required = False, default = "transformer_run",
help="Experiment name, used for logging, default = transformer_run")
parser.add_argument("-e", "--epochs", dest = "epochs", type = int, required = False, default = 10,
help="Number of epochs. Increasing can help if memory error")
parser.add_argument("-trbsize", "--train_batchsize", dest = "train_batchsize", type = int, required = False, default = 5,
help="Per device train batchsize. Reduce along with val batch size if memory error")
parser.add_argument("-dvbsize", "--dev_batchsize", dest = "dev_batchsize", type = int, required = False, default = 5,
help="Per device validation batchsize. Reduce if memory error")
parser.add_argument("-fs", "--fasta_suffix", dest = "fasta_suffix", type = str, required = False, default = ".fasta",
help="File ending for recovering prot names .fasta for prot.fasta")
parser.add_argument("-as", "--aln_suffix", dest = "aln_suffix", type = str, required = False, default = ".aln",
help="File ending for recovering prot names .aln for prot.aln")
args = parser.parse_args()
return(args)
def load_dataset_alnpairs(fasta_list, aln_list, max_length, aln_suffix, fasta_suffix, max_records= 4):
# Match fasta to reference alignment with dictionaries
print(aln_suffix, fasta_suffix)
alndict = {}
aln_seqnames = []
with open(aln_list, "r") as f:
for idx, aln_file in enumerate(f):
protgroup = aln_file.split("/")[-1].split(".")[0].replace(aln_suffix, "")
aln_record_dict = {}
with open(aln_file.replace("\n", ""), "r") as input_handle:
alignment= AlignIO.read(input_handle, format = "fasta")
for i in range(len(alignment)):
aln_record_dict[alignment[i].id] = str(alignment[i].seq)
aln_seqnames.append(alignment[i].id)
alndict[protgroup] = aln_record_dict
#print(alndict)
seqdict = {}
with open(fasta_list, "r") as f:
for idx, fasta_file in enumerate(f):
print(idx)
record_dict = {}
print(fasta_file)
protgroup = fasta_file.split("/")[-1].split(".")[0]
protgroup = protgroup.replace(fasta_suffix, "")
print(protgroup)
if protgroup in alndict.keys():
#print(protgroup)
seq_names, seqs, seqs_spaced = parse_fasta_for_embed(fasta_file.replace("\n", ""), extra_padding = False)
# Only first three sequences are in the gold standard
#print(seq_names)
for i in range(max_records ):
#print(i)
try:
if seq_names[i] in aln_seqnames:
#print(len(seq_names), i)
record_dict[seq_names[i]] = seqs_spaced[i]
except Exception as E:
print(E, i)
seqdict[protgroup] = record_dict
allprots = alndict.keys()
#trainset = []
protnames = []
seqs1 = []
seqs2 =[]
pos1 =[]
pos2 =[]
seqnames1 =[]
seqnames2 =[]
aas1 = [] # Not used for training, but used in other processes
aas2 = []
labels = []
for protgroup in allprots:
prot_seqs =seqdict[protgroup]
prot_alns =alndict[protgroup]
allseqnames = list(prot_seqs.keys())
complete = []
for seqname_i in range(len(allseqnames)):
seqname1 = allseqnames[seqname_i]
complete.append(seqname1)
for seqname_j in range(len(allseqnames)):
seqname2 = allseqnames[seqname_j]
if seqname2 in complete:
continue
seq1 = prot_seqs[seqname1]
seq2 = prot_seqs[seqname2]
aln1 = prot_alns[seqname1]
aln2 = prot_alns[seqname2]
#print(seqname1, seqname2)
#print(seq1, seq2)
#print(aln1, aln2)
# Both alignments are the same length
seqpos1 = 0
seqpos2 = 0
equi_positions = []
for i in range(len(aln1)):
aa1 = AA()
aa2 = AA()
char1 = aln1[i]
char2 = aln2[i]
#print(char1, char2)
if char1 != "-" and char2 != "-":
#print(char1, char2, seqpos1, seqpos2)
# Don't take positions beyond the max length
if seqpos1 <= max_length-2:
if seqpos2 <= max_length - 2:
equi_positions.append([seqpos1, seqpos2])
if char1 != "-":
seqpos1 = seqpos1 + 1
if char2 != "-":
seqpos2 = seqpos2 + 1
seq1_fixed = "".join(seq1.split())
seq1_fixed = re.sub(r"[UZOB]", "X", seq1_fixed)
seq1_fixed = list(seq1_fixed)[:max_length-2]
seq2_fixed = "".join(seq2.split())
seq2_fixed = re.sub(r"[UZOB]", "X", seq2_fixed)
seq2_fixed = list(seq2_fixed)[:max_length-2]
#print(seqname1, seqname2)
for equi in equi_positions:
#print(equi)
protnames.append(protgroup)
seqs1.append(seq1_fixed)
seqs2.append(seq2_fixed)
pos1.append(equi[0])
pos2.append(equi[1])
seqnames1.append(seqname1)
seqnames2.append(seqname2)
labels.append(1)
aa1 = AA()
aa1.seqpos = equi[0]
aa1.seqnames = seqname1
aa1.seqaa = seq1_fixed[equi[0]]
aa1.seqnum = seqname_i
aa2 = AA()
aa2.seqpos = equi[1]
aa2.seqnames = seqname2
aa2.seqaa = seq2_fixed[equi[1]]
aa2.seqnum= seqname_j
aas1.append(aa1)
aas2.append(aa2)
# Make some random pair negatives
for i in range(len(equi_positions)):
non_equi = [random.choice(range(len(seq1_fixed))), random.choice(range(len(seq2_fixed)))]
if non_equi in equi_positions:
continue
protnames.append(protgroup)
seqs1.append(seq1_fixed)
seqs2.append(seq2_fixed)
pos1.append(non_equi[0])
pos2.append(non_equi[1])
seqnames1.append(seqname1)
seqnames2.append(seqname2)
labels.append(0)
aa1 = AA()
aa1.seqpos = non_equi[0]
aa1.seqnames = seqname1
aa1.seqaa = seq1_fixed[non_equi[0]]
aa1.seqnum = seqname_i
aa2 = AA()
aa2.seqpos = non_equi[1]
aa2.seqnames = seqname2
aa2.seqaa = seq2_fixed[non_equi[1]]
aa2.seqnum= seqname_j
aas1.append(aa1)
aas2.append(aa2)
# Make close negatives, one after correct second position
for i in range(len(equi_positions)):
equi = equi_positions[i]
if (equi[1] + 1) < len(seq2_fixed): # Change post training
protnames.append(protgroup)
seqs1.append(seq1_fixed)
seqs2.append(seq2_fixed)
pos1.append(equi[0])
pos2.append(equi[1] + 1)
seqnames1.append(seqname1)
seqnames2.append(seqname2)
labels.append(0)
aa1 = AA()
aa1.seqpos = equi[0]
aa1.seqnames = seqname1
aa1.seqaa = seq1_fixed[equi[0]]
aa2 = AA()
aa2.seqpos = equi[1] + 1
aa2.seqnames = seqname2
aa2.seqaa = seq2_fixed[equi[1] + 1]
aa1.seqnum = seqname_i
aa2.seqnum = seqname_j
aas1.append(aa1)
aas2.append(aa2)
# Make close negatives, one before correct second position
for i in range(len(equi_positions)):
equi = equi_positions[i]
if equi[1] - 1 >= 0:
protnames.append(protgroup)
seqs1.append(seq1_fixed)
seqs2.append(seq2_fixed)
pos1.append(equi[0])
pos2.append(equi[1] - 1)
seqnames1.append(seqname1)
seqnames2.append(seqname2)
labels.append(0)
aa1 = AA()
aa1.seqpos = equi[0]
aa1.seqnames = seqname1
aa1.seqaa = seq1_fixed[equi[0]]
aa2 = AA()
aa2.seqpos = equi[1] -1
aa2.seqnames = seqname2
aa2.seqaa = seq2_fixed[equi[1] -1 ]
aa1.seqnum = seqname_i
aa2.seqnum = seqname_j
aas1.append(aa1)
aas2.append(aa2)
#trainset.append([seq1, seq2, equi[0], equi[1], seqname1, seqname2])
return(protnames, seqs1, seqs2, pos1, pos2, aas1, aas2, labels, seqnames1, seqnames2 )
def encode_tags(labels, labels1, encodings1, labels2, encodings2, max_length):
input_ids = []
token_type_ids = []
attention_masks = []
enc_labels1 = []
enc_labels2 = []
word_indices = []
for label1, input_id1, label2, input_id2, in zip(labels1, encodings1.input_ids, labels2, encodings2.input_ids): #encodings.offset_mapping):
input_id = np.concatenate((input_id1, input_id2), axis = 0)
# The length of the first sequence
ones1 = np.ones(len(input_id1),dtype=int)
zeroes1 = ones1 * 0
# The length of the second sequence
ones2 = np.ones(len(input_id2), dtype = int)
zeroes2 = ones2 * 0
# The remaining pad to max_length
ones_tail = np.ones(max_length - len(ones1) - len(ones2), dtype = int)
zeroes_tail = ones_tail * 0
# label1 = 0
# [0, 1, 0, 0]
enc_label1 = zeroes1.copy()
enc_label1[label1 + 1] = 1
# label1 = 1
# [0, 0, 1, 0]
enc_label2 = zeroes2.copy()
enc_label2[label2 + 1] = 1
# tokens1
# [1, 4, 3, 5]
# tokens2
# [1, 3, 7, 5]
# Token_ids
# [1, 4, 3, 5, 1, 3, 7, 5, 0, 0]
token_id = np.concatenate((input_id, zeroes_tail), axis = 0)
# enc_labels_1
# [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
enc_label1 = np.concatenate((enc_label1, zeroes2, zeroes_tail), axis = 0)
# enc_labels2
# [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
enc_label2 = np.concatenate((zeroes1, enc_label2, zeroes_tail), axis = 0)
# Mark positions of diff sequences
# [0, 0, 0, 1, 1, 1, 1, 1, 1, 1]
token_type_id = np.concatenate((zeroes1, ones2, ones_tail), axis = 0)
# Mark positions that are sequence vs. padding
# [1, 1, 1, 1, 1, 1, 1, 1, 0, 0]
attention_mask = np.concatenate((ones1, ones2, zeroes_tail), axis = 0)
input_ids.append(token_id)
enc_labels1.append([enc_label1])
enc_labels2.append([enc_label2])
token_type_ids.append(token_type_id)
attention_masks.append(attention_mask)
word_index1 = label1 + 1
word_index2 = len(input_id1) + label2 + 1
word_indices.append([word_index1, word_index2])
indexes = range(0, len(input_ids))
assert (len(indexes) == len(input_ids) == len(token_type_ids) == len(attention_masks) == len(labels) == len(enc_labels1) == len(enc_labels2) == len(word_indices)), "Unequal lengths during tag encoding!"
data_dict = {"index" : indexes, "input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask":attention_masks, "labels": labels, "word_indices" : word_indices}
return(data_dict )
# Copied from cs github
# Function to calculate the accuracy of our predictions vs labels
def flat_accuracy(preds, labels, return_predict_correctness = False):
pred_flat = np.argmax(preds, axis=1).flatten()
labels_flat = labels.flatten()
if return_predict_correctness:
return np.sum(pred_flat == labels_flat) / len(labels_flat), pred_flat == labels_flat
else:
return np.sum(pred_flat == labels_flat) / len(labels_flat)
def flat_predictions(preds):
pred_flat = np.argmax(preds, axis=1).flatten()
return pred_flat == 1
def collate_fn(batch):
data_list, label_list = [], []
for _data, _label in batch:
data_list.append(_data)
label_list.append(_label)
return torch.Tensor(data_list), torch.LongTensor(label_list)
class AlignDataset(Dataset):
def __init__(self, encodings):
self.encodings = encodings
#self.labels = labels
def __getitem__(self, idx):
# This creates a torch.tensor for everything in the encodings dict
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
#item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
#print(len(self.encodings['labels']))
return len(self.encodings['labels'])
def load_datasets(train_fastas, train_alns, dev_fastas, dev_alns, train_batchsize, dev_batchsize, aln_suffix, fasta_suffix):
print(aln_suffix, fasta_suffix)
train_protnames, train_seqs1, train_seqs2, train_pos1, train_pos2, train_aas1, train_aas2, train_labels, train_seqnames1, train_seqnames2 = load_dataset_alnpairs(train_fasta_list, train_aln_list, int(max_length/2), aln_suffix, fasta_suffix)
seq_tokenizer = BertTokenizerFast.from_pretrained(model_name, do_lower_case=False)
# Going to be concatenating sequence pair
# With one mask showing sequence vs. padding: attention_mask
# With one mask showing which sequence is which: token_type_ids
train_seqs1_encodings = seq_tokenizer(train_seqs1, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
train_seqs2_encodings = seq_tokenizer(train_seqs2, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
#train_seqs_encodings, train_pos1_encodings, train_pos2_encodings =
train_data_dict = encode_tags(train_labels, train_pos1, train_seqs1_encodings, train_pos2, train_seqs2_encodings, max_length)
outtrain = pd.DataFrame()
outtrain['protname'] = train_protnames
outtrain['seqnames1'] = train_seqnames1
outtrain['seqnames2'] = train_seqnames2
outtrain['pos1'] = train_pos1
outtrain['pos2'] = train_pos2
outtrain['label'] = train_labels
outtrain['aas1'] = train_aas1
outtrain['aas2'] = train_aas2
outtrain.to_csv(os.path.join(outdir, "traindata.csv"), index=False)
np.set_printoptions(threshold=np.inf)
print("input ids1")
print(train_seqs1_encodings.input_ids[0])
print("input ids2")
print(train_seqs2_encodings.input_ids[0])
print("encoded input")
print(train_data_dict['input_ids'][0])
print("equivalent positions")
print(train_pos1[0], train_pos2[0])
print("sequence groups")
print(train_data_dict['token_type_ids'][0])
print("sequence positions")
print(train_data_dict['attention_mask'][0])
#print("position1 label")
#print(train_data_dict['enc_labels1'][0])
#print("position2 label")
#print(train_data_dict['enc_labels2'][0])
print("label")
print(train_data_dict['labels'][0])
#print("index")
#print(train_data_dict['index'][0])
print("word_indices")
print(train_data_dict['word_indices'][0])
#https://github.com/llightts/CSI5138_Project/blob/master/RoBERTa_WiC_baseline.ipynb
# print(enc_labels1)
# enc_labels1 = enc_labels1.type(torch.LongTensor)
# print(enc_labels1)
# enc_labels2 = enc_labels2.type(torch.LongTensor)
print("lengths")
print(len(train_data_dict["input_ids"]))
print(len(train_data_dict["token_type_ids"]))
print(len(train_data_dict["attention_mask"]))
print(len(train_data_dict["labels"]))
#print(len(train_data_dict["enc_labels1"]))
#print(len(train_data_dict["enc_labels2"]))
print(len(train_data_dict["word_indices"]))
#print(len(train_data_dict["index"]))
#train_data = AlignDataset(train_data_dict)
#print(train_dataloader)
train_data = TensorDataset(
torch.tensor(train_data_dict["input_ids"]),
torch.tensor(train_data_dict["token_type_ids"]),
torch.tensor(train_data_dict["attention_mask"]),
torch.tensor(train_data_dict["labels"]).type(torch.FloatTensor),
torch.tensor(train_data_dict["word_indices"]),
torch.tensor(train_data_dict["index"])
)
#
#
#
# # Create a sampler and loader
# train_sampler = RandomSampler(train_data)
#train_dataloader= train_data
# data_loader = DataLoader(dataset=ListDataset(seqs),
# batch_size=batch_size,
# shuffle=False,
# collate_fn=collate,
# pin_memory=False)
train_dataloader = DataLoader(dataset = train_data,
shuffle = True,
#collate_fn = collate_fn,
batch_size=train_batchsize)
# Dev set
dev_protnames, dev_seqs1, dev_seqs2, dev_pos1, dev_pos2, dev_aas1, dev_aas2, dev_labels, dev_seqnames1, dev_seqnames2 = load_dataset_alnpairs(dev_fasta_list, dev_aln_list, int(max_length/2), aln_suffix, fasta_suffix)
dev_seqs1_encodings = seq_tokenizer(dev_seqs1, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
dev_seqs2_encodings = seq_tokenizer(dev_seqs2, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
dev_data_dict = encode_tags(dev_labels, dev_pos1, dev_seqs1_encodings, dev_pos2, dev_seqs2_encodings, max_length)
#outdev = pd.DataFrame.from_dict(dev_data_dict)
outdev = pd.DataFrame()
outdev['protname'] = dev_protnames
outdev['seqnames1'] = dev_seqnames1
outdev['seqnames2'] = dev_seqnames2
outdev['pos1'] = dev_pos1
outdev['pos2'] = dev_pos2
outdev['aas1'] = dev_aas1
outdev['aas2'] = dev_aas2
outdev['label'] = dev_labels
outdev.to_csv(os.path.join(outdir, "devdata.csv"), index=False)
dev_data = TensorDataset(
torch.tensor(dev_data_dict["input_ids"]),
torch.tensor(dev_data_dict["token_type_ids"]),
torch.tensor(dev_data_dict["attention_mask"]),
torch.tensor(dev_data_dict["labels"]).type(torch.FloatTensor),
torch.tensor(dev_data_dict["word_indices"]),
torch.tensor(dev_data_dict["index"])
)
# Create a sampler and loader
#dev_sampler = SequentialSampler(dev_data)
dev_dataloader = DataLoader(dev_data, shuffle = True, batch_size=dev_batchsize)
#dev_dataloader = AlignDataset(dev_data_dict)
# Test, unlabelled
#test_seqs1, test_seqs2, test_pos1, test_pos2, test_labels, test_seqnames1, test_seqnames2 = load_dataset_alnpairs(test_fasta_list, test_aln_list, int(max_length/2))
#test_seqs1_encodings = seq_tokenizer(test_seqs1, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
#test_seqs2_encodings = seq_tokenizer(test_seqs2, is_split_into_words=True, return_offsets_mapping=False, truncation=True, padding=False, max_length = int(max_length/2))
#test_data_dict = encode_tags(test_labels, test_pos1, test_seqs1_encodings, test_pos2, test_seqs2_encodings, max_length)
#outtest = pd.DataFrame.from_dict(test_data_dict)
#outtest['seqs1'] = test_seqs1
#outtest['seqs2'] = test_seqs2
#outtest['pos1'] = test_pos1
#outtest['pos2'] = test_pos2
#outtest.to_csv(os.path.join(outdir, "testdata.csv"), index=False)
#test_data = TensorDataset(
# torch.tensor(test_data_dict["input_ids"]),
# torch.tensor(test_data_dict["token_type_ids"]),
# torch.tensor(test_data_dict["attention_mask"]),
## #torch.tensor(test_data_dict["enc_labels1"]).type(torch.LongTensor),
## #torch.tensor(test_data_dict["enc_labels2"]).type(torch.LongTensor),
# torch.tensor(test_data_dict["word_indices"]),
# torch.tensor(test_data_dict["index"])
#)
# Create a sampler and loader
#test_sampler = RandomSampler(test_data)
#test_dataloader = DataLoader(test_data, sampler=test_sampler, batch_size=dev_batchsize)
#test_dataloader = AlignDataset(test_data_dict)
return(train_dataloader, dev_dataloader, seq_tokenizer)
class WiC_Head(torch.nn.Module):
def __init__(self, model, embedding_size = 1024):
"""
Keeps a reference to the provided RoBERTa model.
It then adds a linear layer that takes the distance between two
"""
super(WiC_Head, self).__init__()
self.bert = BertModel(config)
self.embedding_size = embedding_size
self.embedder = model
self.linear_diff = torch.nn.Linear(embedding_size, 250, bias = True)
self.linear_separator = torch.nn.Linear(250, 2, bias = True)
self.loss = torch.nn.CrossEntropyLoss()
self.activation = torch.nn.ReLU()
self.softmax = torch.nn.Softmax()
def forward(self, input_ids=None, attention_mask=None, labels=None,
enc_labels1 = None, enc_labels2 = None):
"""
Takes in the same argument as RoBERTa forward plus two tensors for the location of the 2 words to compare
"""
if enc_labels1 is None or enc_labels2 is None:
raise ValueError("The tensors (enc_labels1, enc_labels2) containing the location of the words to compare in the input vector must be provided.")
elif input_ids is None:
raise ValueError("The input_ids tensor must be provided.")
elif enc_labels1.shape[0] != input_ids.shape[0] or enc_labels2.shape[0] != input_ids.shape[0]:
raise ValueError("All provided vectors should have the same batch size.")
batch_size = enc_labels1.shape[0]
# Get the embeddings (?)
#print(batch_size)
outputs = self.embedder(input_ids=input_ids, attention_mask=attention_mask)
# Get the words
print(outputs)
# Is a BaseModelOutputWithPoolingAndCrossAttentions
embs = outputs[0:4] # Get last hidden state
print(embs)
#embs, _= self.embedder(input_ids=input_ids, attention_mask=attention_mask).embeddings()
word1s = torch.matmul(enc_labels1, embs).view(batch_size, self.embedding_size*4)
#.view(batch_size, self.embedding_size)
word2s = torch.matmul(enc_labels2, embs).view(batch_size, self.embedding_size*4)
diff = word1s - word2s
# Calculate outputs using activation
layer1_results = self.activation(self.linear_diff(diff))
logits = self.softmax(self.linear_separator(layer1_results))
outputs = logits
# Calculate the loss
if labels is not None:
# We want seperation like a SVM so use Hinge loss
loss = self.loss(logits.view(-1, 2), labels.view(-1))
outputs = (loss, logits)
return outputs
def _get_mask(indices, embedding_size):
mask = (indices != 0)
mask.unsqueeze_(-1)
mask = mask.expand(mask.shape[0], mask.shape[1], embedding_size)
LARGE_VALUE = 2 ** 32
return torch.where(mask == True, 0, LARGE_VALUE)
#def __init__(self, config: PretrainedConfig, *inputs, **kwargs):
# super().__init__(config, *inputs, **kwargs)
# LightningModule
class GeneralBertClassifier(LightningModule):
#config_class = PretrainedConfig
def __init__(self, model_path):#, layers = [-4, -3, -2, -1]):
super(GeneralBertClassifier, self).__init__()
#def __init__(self, model_path, config: PretrainedConfig, *inputs, **kwargs):
# super(GeneralBertClassifier, self).__init__(config, *inputs, **kwargs)
#print("CONFIG", config)
self.model = BertModel.from_pretrained(model_path, output_hidden_states = True)#, config = config)
self.embedding_dim = self.model.get_input_embeddings().embedding_dim
self.loss = nn.BCELoss()
self.save_hyperparameters()
self.valid_accuracy = Accuracy()
self.test_accuracy = Accuracy()
self.predictions_proba = torch.Tensor()
def _get_embedding2(self, input_ids, attention_mask, token_type_ids, word_indices):
return 0
def _get_embeddings(self, input_ids, attention_mask, token_type_ids, word_indices, add_cls):
#sentence_outputs = self.model(input_ids, attention_mask, token_type_ids).last_hidden_state
hidden_states = self.model(input_ids, attention_mask, token_type_ids).hidden_states
#print(self.layers)
embeddings = torch.cat(tuple([hidden_states[i] for i in self.layers]), dim=-1)
#print(embeddings.shape)
tokens_embeddings = get_tokens_embeddings(embeddings, word_indices)
word_embedding = torch.max(tokens_embeddings, 1)[0]
#print(tokens_embeddings.shape)
#print(word_embedding.shape)
if not add_cls:
return word_embedding
cls_embedding = sentence_outputs[:, 0, :]
return word_embedding, cls_embedding
def forward(self, input_ids, attention_mask, token_type_ids, word_indices):
raise RuntimeError("Override me")
def training_step(self, batch, _):
input_ids, token_type_ids, attention_mask, labels, word_indices, index = batch
outputs = self(input_ids, attention_mask, token_type_ids, word_indices)
return self.loss(outputs, labels)
def _get_logits(self, outputs):
raise RuntimeError("Override me")
def validation_step(self, batch, _):
input_ids, token_type_ids, attention_mask, labels, word_indices, index = batch
outputs = self(input_ids, attention_mask,token_type_ids, word_indices)
logits = self._get_logits(outputs)
#print("logits")
self.valid_accuracy.update(logits, labels.int())
self.log("val_acc", self.valid_accuracy)
#print("outputs", outputs)
#print("labels", labels)
loss = self.loss(outputs, labels)
self.log("val_loss", loss, prog_bar=True)
def validation_epoch_end(self, _):
self.log("val_acc_epoch", self.valid_accuracy.compute(), prog_bar=True)
def on_test_epoch_start(self):
self.predictions_proba = torch.Tensor()
def test_step(self, batch, _):
input_ids, token_type_ids, attention_mask, word_indices, index = batch
outputs = self(input_ids, attention_mask, token_type_ids, word_indices)
self.predictions_proba = torch.cat((self.predictions_proba, outputs.detach().cpu()))
def configure_optimizers(self):
optimizer = AdamW(self.parameters(), lr=1e-5)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=20)
return [optimizer], [scheduler]
def get_backbone(self):
return self.model
def get_tokens_embeddings(batch, indices):
return _batched_index_select(batch, 1, indices) - _get_mask(indices, batch.shape[2])
def _batched_index_select(t, dim, inds):
dummy = inds.unsqueeze(2).expand(inds.size(0), inds.size(1), t.size(2))
out = t.gather(dim, dummy) # b x e x f
return out
class CosineSimilarityClassifier(GeneralBertClassifier):
#def __init__(self, model_path, activation, threshold, config:PretrainedConfig):
# super(CosineSimilarityClassifier, self).__init__(model_path, config)
#config_class = PretrainedConfig
def __init__(self, model_path, activation, layers):
super(CosineSimilarityClassifier, self).__init__(model_path)
#def __init__(self, config: PretrainedConfig, *inputs, **kwargs):
# super(GeneralBertClassifier, self).__init__(config, *inputs, **kwargs)
if activation == "relu":
self.activation = nn.ReLU()
elif activation == "sigmoid":
self.activation = nn.Sigmoid()
else:
raise RuntimeError("Only relu or sigmoid can be use as an activation")
self.layers = layers
self.cos = nn.CosineSimilarity(dim=1)
#classifier_dropout = (
# config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
#)
#self.dropout = nn.Dropout(classifier_dropout)
#self.classifier = nn.Linear(config.hidden_size, config.num_labels)
#self.init_weights()
def _get_logits(self, outputs):
#print("Is this happening")
#print(outputs > self.threshold)
#return (outputs > self.threshold).float()
return (outputs > 0.5).float()
#def forward(self, input_ids, attention_mask, word_indices):
# first_word_embedding = self._get_embeddings(input_ids[0], attention_mask[0], word_indices[0], add_cls=False)
# second_word_embedding = self._get_embeddings(input_ids[1], attention_mask[1], word_indices[1], add_cls=False)
#
# outputs = self.cos(first_word_embedding, second_word_embedding)
# outputs = self.activation(outputs)
# return outputs
#def forward(self, input_ids, attention_mask, token_type_ids, word_indices):
def forward(self, input_ids, attention_mask=None, token_type_ids=None,
word_indices=None, labels=None):
#print("word_indices", word_indices)
#print(word_indices.shape)
word_index1 = word_indices[:, [0]]
#print(word_index1)
word_index2 = word_indices[:, [1]]
#print(word_index2)
first_word_embedding = self._get_embeddings(input_ids, attention_mask, token_type_ids, word_index1, add_cls = False)
second_word_embedding = self._get_embeddings(input_ids, attention_mask, token_type_ids, word_index2, add_cls = False)
#print("both words", both_word_embeddings)
#print("both words shape", both_word_embeddings.shape)
#first_word_embedding = both_word_embeddings[0]
#second_word_embedding = both_word_embeddings[1]
#print("first", first_word_embedding)
#print("second", second_word_embedding)
outputs = self.cos(first_word_embedding, second_word_embedding)
#print("pree act", outputs)
outputs = self.activation(outputs)
#print("post act", outputs)
#logits = _get_logits(outputs)
#pooled_output = outputs[1]
#print("pooled_output", pooled_output)
#pooled_output = self.dropout(pooled_output)
#logits = self.classifier(pooled_output)
#return SequenceClassifierOutput(
# loss=loss,
# logits=logits#,
# #hidden_states=outputs.hidden_states,
# #attentions=outputs.attentions,
#)
return outputs
def compute_metrics(p: EvalPrediction):
print(dir(p))
print(p)
pred, labels = p.predictions, p.label_ids
print("pred", pred)
print("labels", labels)
print(pred.shape)
print(labels.shape)
#pred = np.argmax(pred, axis=1)
accuracy = accuracy_score(y_true=labels, y_pred=pred)
recall = recall_score(y_true=labels, y_pred=pred)
precision = precision_score(y_true=labels, y_pred=pred)
f1 = f1_score(y_true=labels, y_pred=pred)
return {"accuracy": accuracy, "precision": precision, "recall": recall, "f1": f1}
if __name__ == "__main__":
args = get_aasim_args()
model_name = args.model_path
max_length = args.max_length
train_fasta_list = args.train_fastas
train_aln_list = args.train_alns
dev_fasta_list = args.dev_fastas
dev_aln_list = args.dev_alns
#test_fasta_list = args.test_fastas
#test_aln_list = args.test_alns
outdir = args.outdir
layers = args.layers
train_batchsize = args.train_batchsize
dev_batchsize = args.dev_batchsize
fasta_suffix = args.fasta_suffix
aln_suffix = args.aln_suffix
epochs = args.epochs
print(layers)
print(outdir)
print(train_fasta_list)
print(train_aln_list)
print(dev_fasta_list)
print(dev_aln_list)
if not os.path.exists(outdir):
os.makedirs(outdir)
print(fasta_suffix, aln_suffix)
#train_batchsize = 10
#epochs = 20
patience = 10
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
torch.cuda.get_device_name(0)
train_dataloader, dev_dataloader, seq_tokenizer = load_datasets(train_fasta_list, train_aln_list, dev_fasta_list, dev_aln_list, train_batchsize, dev_batchsize, aln_suffix, fasta_suffix)
#model = AutoModel.from_pretrained(model_name)
#model = CosineSimilarityClassifier(model_name, "relu", BASELINE_THRESHOLD, config = PretrainedConfig)
config = PretrainedConfig.from_pretrained(model_name)
print(config)
print(model_name)
print(config.__class__)
model = CosineSimilarityClassifier(model_name, activation = "relu", layers = layers)#, config)
model.cuda()
print(model)
if n_gpu > 0:
on_gpu = True
else:
on_gpu = False
early_stop_callback = EarlyStopping(
monitor="val_loss",
min_delta=0.001,
patience=5,
verbose=True,
mode="min"
)
model_dir = os.path.join(outdir, f"checkpoints")
if not os.path.exists(model_dir):
os.makedirs(model_dir)
model_checkpoint = ModelCheckpoint(
monitor="val_loss",
dirpath=model_dir,
filename="{epoch}-{val_loss:.3f}",
)
trainer = Trainer(
gpus=1 if on_gpu else None,
enable_checkpointing=True,
accumulate_grad_batches=10,
max_epochs=epochs,
callbacks=[early_stop_callback, model_checkpoint],
val_check_interval=0.5)