-
Notifications
You must be signed in to change notification settings - Fork 13
/
baseline_bert_layer_prune.py
151 lines (128 loc) · 5.35 KB
/
baseline_bert_layer_prune.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
from datasets import load_dataset, load_metric
from datasets import Dataset, Features, ClassLabel, Value
from dataset import load, data_augmentation
from transformers import AutoTokenizer, Adafactor, AutoConfig, BertConfig
from transformers import AutoModelForTokenClassification, TrainingArguments, Trainer
from sklearn.metrics import accuracy_score, precision_recall_fscore_support, classification_report
import numpy as np
import numpy.ma as ma
from transformers import DataCollatorForTokenClassification
from tqdm import tqdm
import datetime
task = 1
number_of_layers = 12
#model_checkpoint = "german-nlp-group/electra-base-german-uncased"
model_checkpoint = "bert-base-uncased"#"distilbert-base-german-cased"
run_name= f"{model_checkpoint}-{task}-{number_of_layers}layer-10-data"
run_name = run_name.replace("/","-") + " " + str(datetime.datetime.now())[:-7]
batch_size = 8
label_all_tokens = True
data_factor = 0.1 # train and test on x percent of the data
sequenz_length = 512
## load data
val_data = load("data/sepp_nlg_2021_train_dev_data_v5.zip","dev","en",subtask=task)
train_data = load("data/sepp_nlg_2021_train_dev_data_v5.zip","train","en",subtask=task)
## tokenize data
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
def tokenize_and_align_data(data,stride=0):
tokenizer_settings = {'is_split_into_words':True,'return_offsets_mapping':True,
'padding':False, 'truncation':True, 'stride':stride,
'max_length':sequenz_length, 'return_overflowing_tokens':True}
tokenized_inputs = tokenizer(data[0], **tokenizer_settings)
labels = []
for i,document in enumerate(tokenized_inputs.encodings):
doc_encoded_labels = []
last_word_id = None
for word_id in document.word_ids:
if word_id == None: #or last_word_id == word_id:
doc_encoded_labels.append(-100)
else:
#document_id = tokenized_inputs.overflow_to_sample_mapping[i]
#label = examples[task][document_id][word_id]
label = data[1][word_id]
doc_encoded_labels.append(int(label))
last_word_id = word_id
labels.append(doc_encoded_labels)
tokenized_inputs["labels"] = labels
return tokenized_inputs
def to_dataset(data,stride=0):
labels, token_type_ids, input_ids, attention_masks = [],[],[],[]
for item in tqdm(data):
result = tokenize_and_align_data(item,stride=stride)
labels += result['labels']
token_type_ids += result['token_type_ids']
input_ids += result['input_ids']
attention_masks += result['attention_mask']
return Dataset.from_dict({'labels': labels, 'token_type_ids':token_type_ids, 'input_ids':input_ids, 'attention_mask':attention_masks})
train_data = train_data[:int(len(train_data)*data_factor)] # limit data to x%
#train_data += data_augmentation(train_data,.5)
print("tokenize training data")
tokenized_dataset_train = to_dataset(train_data,stride=400)
del train_data
print("tokenize validation data")
val_data = val_data[:int(len(val_data)*data_factor)] # limit data to x%
tokenized_dataset_val = to_dataset(val_data)
del val_data
## metrics
def compute_metrics_sklearn(pred):
mask = np.less(pred.label_ids,0) # mask out -100 values
labels = ma.masked_array(pred.label_ids,mask).compressed()
preds = ma.masked_array(pred.predictions.argmax(-1),mask).compressed()
precision, recall, f1, _ = precision_recall_fscore_support(labels, preds, average='binary')
acc = accuracy_score(labels, preds)
return {
'f1': f1,
'precision': precision,
'recall': recall,
'accuracy':acc,
}
## train model
config = AutoConfig.from_pretrained(model_checkpoint)
config.num_hidden_layers = number_of_layers
config.num_labels=2
#config.max_position_embeddings = sequenz_length
print("-----------------")
print(config)
#config.num_attention_heads = 6
model = AutoModelForTokenClassification.from_pretrained(model_checkpoint,config = config)
print("Model Parameters " + str(model.num_parameters()))
# freez layers
#for name, param in model.named_parameters():
# if 'classifier' not in name:
# param.requires_grad = False
#model.prune_heads({0:[0,1,2,3,4,5,6],1:[0,1,2,3,4,5,6],2:[0,1,2,3,4,5,6],3:[0,1,2,3,4,5,6],4:[0,1,2,3,4,5,6],5:[0,1,2,3,4,5,6]})
args = TrainingArguments(
output_dir=f"models/{run_name}/checkpoints",
run_name=run_name,
evaluation_strategy = "epoch",
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
gradient_accumulation_steps=1,
num_train_epochs=3,
adafactor=True,
learning_rate=4e-5,
warmup_steps=50,
#weight_decay=0.035,
#adam_epsilon= 2e-09,
#lr_scheduler_type="cosine",
report_to=["tensorboard"],
logging_dir='runs/'+run_name, # directory for storing logs
logging_first_step=True,
logging_steps=100,
save_steps= 10000,
save_total_limit=10,
seed=16,
fp16=True
)
data_collator = DataCollatorForTokenClassification(tokenizer)
trainer = Trainer(
model,
args,
train_dataset=tokenized_dataset_train,
eval_dataset=tokenized_dataset_val,
data_collator=data_collator,
tokenizer=tokenizer,
compute_metrics=compute_metrics_sklearn,
)
trainer.train()
trainer.save_model(f"models/{run_name}/final")