-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleave_one_sgRNA_out_testing.py
130 lines (103 loc) · 3.32 KB
/
leave_one_sgRNA_out_testing.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
import numpy as np
import torch
import random
import os
import logging
from transformers.modeling_bert import BertForSequenceClassificationFeatures,BertForSequenceClassificationFeatures2
from transformers import(
AdamW,
BertForSequenceClassification,
BertConfig,
DNATokenizer,
get_linear_schedule_with_warmup
)
from transformers import glue_processors as processors
from finetune_model import train, predict
logger = logging.getLogger(__name__)
MODEL_CLASSES = {
"dna": (BertConfig, BertForSequenceClassification, DNATokenizer),
"dnafeatures": (BertConfig, BertForSequenceClassificationFeatures, DNATokenizer),
"dnafeatures2": (BertConfig, BertForSequenceClassificationFeatures2, DNATokenizer)
}
cfg = {
"data_dir":"data/leave_one_out_testing",
"model_type":"dna",
"model_name_or_path":"pretrained_model/checkpoint-38950",
"task_name":"dnaprom",
"max_seq_length": 23 ,
"per_gpu_eval_batch_size":300 ,
"per_gpu_train_batch_size": 200,
"pred_batch_size":200,
"learning_rate": 2e-4 ,
"num_train_epochs": 70,
"logging_steps": 1 ,
"warmup_percent": 0.1 ,
"hidden_dropout_prob": 0.1 ,
"attention_probs_dropout_prob": 0.1,
"weight_decay": 0.01 ,
"n_samples_dataset": 1000,
"save_total_limits": 1 ,
"gradient_accumulation_steps": 3,
"adam_epsilon":1e-8,
"beta1":0.9,
"beta2":0.999,
"output_dir": "outputs/leave_one_out_testing",
"patience": 15
}
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",
datefmt="%m/%d/%Y %H:%M:%S",
level=logging.INFO
)
set_seed(42)
# Prepare GLUE task
cfg["task_name"] = cfg["task_name"].lower()
if cfg["task_name"] not in processors:
raise ValueError("Task not found: %s" % (cfg["task_name"]))
processor = processors[cfg["task_name"]]()
label_list = processor.get_labels()
num_labels = len(label_list)
# Load pretrained model and tokenizer
cfg["model_type"] = cfg["model_type"].lower()
config_class, model_class, tokenizer_class = MODEL_CLASSES[cfg["model_type"]]
config = config_class.from_pretrained(
cfg["model_name_or_path"],
num_labels=num_labels,
finetuning_task=cfg["task_name"],
cache_dir=None,
)
config.hidden_dropout_prob = cfg["hidden_dropout_prob"]
config.attention_probs_dropout_prob = cfg["attention_probs_dropout_prob"]
sgRNA_list = os.listdir(cfg["data_dir"])
input_path = cfg["data_dir"]
output_path = cfg["output_dir"]
aucpr_results = []
for sgRNA in sgRNA_list:
print(sgRNA)
tokenizer = tokenizer_class.from_pretrained(
"dna7",
do_lower_case=False,
cache_dir=None,
)
model = model_class.from_pretrained(
cfg["model_name_or_path"],
from_tf=bool(".ckpt" in cfg["model_name_or_path"]),
config=config,
cache_dir=None,
)
model.to(device)
# change datapath to specific sgRNA
cfg["data_dir"] = os.path.join(input_path,sgRNA)
cfg["output_dir"] = os.path.join(output_path,sgRNA)
train(cfg,model,tokenizer)
result = predict(cfg,model,tokenizer,pred_dir="test")
aucpr = result["auc-pr"]
print(aucpr)
aucpr_results.append(aucpr)
print(aucpr_results)
print(np.mean(aucpr_results))