-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyperparam_optimization_binary.py
More file actions
283 lines (224 loc) · 11.6 KB
/
hyperparam_optimization_binary.py
File metadata and controls
283 lines (224 loc) · 11.6 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
# wandb api-keys:
# binary: 2115d24f1160165e03f31b6d0327dde07b3a9876
# multi:
import torch
from torch.utils.data import DataLoader, TensorDataset
from transformers import AutoTokenizer, AutoModelForSequenceClassification, AdamW, get_linear_schedule_with_warmup
import pandas as pd
import os
from tqdm import tqdm
import time
import wandb
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from imblearn.over_sampling import RandomOverSampler
# Constants
PROJECT_NAME = 'SciBERT_ep6_binary'
TOKENIZER_NAME = "allenai/scibert_scivocab_uncased"
MODEL_NAME = 'allenai/scibert_scivocab_uncased'
DATA_DIR = './../data/data_splits_stratified'
CLASSIFICATION_TYPE = "NUM_LABEL_BINARY"
EPOCHS = 6
MAX_LENGTH = 256
SWEEP_CONFIG = {
'method': 'bayes',
'metric': {'name': 'val_loss', 'goal': 'minimize'},
'parameters': {
'learning_rate': {'min': 1e-5, 'max': 1e-3},
'batch_size': {'values': [8, 16, 32]},
"weight_decay": {"min": 1e-5, "max": 1e-3}
}
}
if CLASSIFICATION_TYPE == "NUM_LABEL_BINARY":
NUM_LABELS = 2
else:
NUM_LABELS = 13
def load_data(max_length):
train_df = pd.read_csv(os.path.join(DATA_DIR, 'train_data.csv'))
val_df = pd.read_csv(os.path.join(DATA_DIR, 'val_data.csv'))
print("Validation Dataset Length before tokenization:", len(val_df))
tokenizer = AutoTokenizer.from_pretrained(TOKENIZER_NAME)
train_encodings = tokenizer(
train_df['text'].tolist(),
padding=True,
truncation=True,
max_length=max_length,
return_tensors='pt'
)
val_encodings = tokenizer(
val_df['text'].tolist(),
padding=True,
truncation=True,
max_length=max_length,
return_tensors='pt'
)
print("Train Encodings Shape:", train_encodings['input_ids'].shape)
print("Val Encodings Shape:", val_encodings['input_ids'].shape)
train_labels = torch.tensor(train_df[CLASSIFICATION_TYPE].values) # Assuming col_name contains binary labels (0 or 1)
val_labels = torch.tensor(val_df[CLASSIFICATION_TYPE].values)
print("Train Labels Shape:", train_labels.shape)
print("Val Labels Shape:", val_labels.shape)
train_dataset = TensorDataset(
train_encodings['input_ids'],
train_encodings['attention_mask'],
train_labels
)
val_dataset = TensorDataset(
val_encodings['input_ids'],
val_encodings['attention_mask'],
val_labels
)
print("Validation Dataset Length after tokenization:", len(val_dataset))
return train_dataset, val_dataset
# def train():
# with wandb.init() as run:
# # Set configs for the run
# config = run.config
# train_dataset, val_dataset = load_data(max_length=MAX_LENGTH)
# print("Train Dataset Length:", len(train_dataset))
# print("Validation Dataset Length:", len(val_dataset))
# train_dataloader = DataLoader(train_dataset, batch_size=config.batch_size, shuffle=True)
# val_dataloader = DataLoader(val_dataset, batch_size=config.batch_size, shuffle=False)
# print("Train Dataloader Length:", len(train_dataloader))
# print("Validation Dataloader Length:", len(val_dataloader))
# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2) # num_labels=2 for binary classification
# print(f'Model: \n{model}')
# optimizer = AdamW(model.parameters(), lr=config.learning_rate, weight_decay=config.weight_decay)
# total_steps = len(train_dataloader) * EPOCHS
# print(f'Total steps: {total_steps}')
# scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=int(total_steps * 0.05), num_training_steps=total_steps)
# for epoch in range(EPOCHS):
# start_time = time.time() # Start time for the epoch
# model.to(device)
# model.train()
# total_loss = 0
# train_iterator = tqdm(train_dataloader, desc=f"Epoch {epoch+1}/{EPOCHS} Training", leave=False)
# for batch in train_iterator: # iterate through batches
# batch = [b.to(device) for b in batch] # send to device
# inputs = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[2]}
# optimizer.zero_grad()
# outputs = model(**inputs)
# loss = outputs.loss
# loss.backward()
# optimizer.step()
# scheduler.step()
# total_loss += loss.item()
# train_iterator.set_postfix({'train_loss': total_loss / len(train_iterator)}) # Update progress bar with current loss
# # Log average training loss for the epoch
# avg_train_loss = total_loss / len(train_dataloader)
# wandb.log({"epoch": epoch, "train_loss": avg_train_loss})
# # Calculate elapsed time for the epoch
# epoch_time = time.time() - start_time
# print(f"Epoch {epoch+1} completed in {epoch_time:.2f} seconds.")
# # Validation
# model.eval()
# val_loss = 0.0
# val_pred, val_true = [],[]
# val_iterator = tqdm(val_dataloader, desc=f"Epoch {epoch+1}/{EPOCHS} Validation", leave=False)
# for batch in val_iterator: # iterate through batches
# batch = [b.to(device) for b in batch]
# inputs = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[2]}
# outputs = model(**inputs)
# if outputs is not None and outputs.loss is not None: # Check if outputs and loss exist
# val_loss += outputs.loss.item()
# logits = outputs.logits
# preds = torch.argmax(logits, dim=1).cpu().numpy()
# val_pred.extend(preds)
# val_true.extend(batch[2].cpu().numpy())
# print(f"Epoch {epoch+1}: Number of validation predictions: {len(val_true)}, Number of validation labels: {len(val_pred)}")
# accuracy = accuracy_score(val_true, val_pred)
# precision = precision_score(val_true, val_pred, zero_division=0)
# recall = recall_score(val_true, val_pred, zero_division=0)
# macro_f1 = f1_score(val_true, val_pred, zero_division=0, average='macro')
# micro_f1 = f1_score(val_true, val_pred, zero_division=0, average='micro')
# # Log metrics to W&B
# wandb.log({
# "epoch": epoch,
# "val_loss": val_loss / len(val_dataloader),
# "accuracy": accuracy,
# "macro_f1": macro_f1,
# "micro_f1": micro_f1}
# )
# # Print metrics for each epoch
# print(f"Epoch {epoch+1}: val_loss: {val_loss / len(val_dataloader)}, accuracy: {accuracy}, macro_f1: {macro_f1}, micro_f1: {micro_f1}")
def train():
with wandb.init() as run:
# Set configs for the run
config = run.config
train_dataset, val_dataset = load_data(max_length=MAX_LENGTH)
print("Train Dataset Length:", len(train_dataset))
print("Validation Dataset Length:", len(val_dataset))
train_dataloader = DataLoader(train_dataset, batch_size=config.batch_size, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=config.batch_size, shuffle=False)
print("Train Dataloader Length:", len(train_dataloader))
print("Validation Dataloader Length:", len(val_dataloader))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME, num_labels=2) # num_labels=2 for binary classification
print(f'Model: \n{model}')
optimizer = AdamW(model.parameters(), lr=config.learning_rate, weight_decay=config.weight_decay)
total_steps = len(train_dataloader) * EPOCHS
print(f'Total steps: {total_steps}')
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=int(total_steps * 0.05), num_training_steps=total_steps)
for epoch in range(EPOCHS):
start_time = time.time() # Start time for the epoch
model.to(device)
model.train()
total_loss = 0
train_iterator = tqdm(train_dataloader, desc=f"Epoch {epoch+1}/{EPOCHS} Training", leave=False)
for batch in train_iterator: # iterate through batches
batch = [b.to(device) for b in batch] # send to device
inputs = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[2]}
optimizer.zero_grad()
outputs = model(**inputs)
loss = outputs.loss
loss.backward()
optimizer.step()
scheduler.step()
total_loss += loss.item()
train_iterator.set_postfix({'train_loss': total_loss / len(train_iterator)}) # Update progress bar with current loss
# Log average training loss for the epoch
avg_train_loss = total_loss / len(train_dataloader)
wandb.log({"epoch": epoch, "train_loss": avg_train_loss})
# Calculate elapsed time for the epoch
epoch_time = time.time() - start_time
print(f"Epoch {epoch+1} completed in {epoch_time:.2f} seconds.")
# Validation
model.eval()
val_loss = 0.0
val_pred, val_true = [],[]
val_iterator = tqdm(val_dataloader, desc=f"Epoch {epoch+1}/{EPOCHS} Validation", leave=False)
for batch in val_iterator: # iterate through batches
batch = [b.to(device) for b in batch]
inputs = {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[2]}
outputs = model(**inputs)
if outputs is not None and outputs.loss is not None: # Check if outputs and loss exist
val_loss += outputs.loss.item()
logits = outputs.logits
preds = torch.argmax(logits, dim=1).cpu().numpy()
val_pred.extend(preds)
val_true.extend(batch[2].cpu().numpy())
print(f"Epoch {epoch+1}: Number of validation predictions: {len(val_true)}, Number of validation labels: {len(val_pred)}")
accuracy = accuracy_score(val_true, val_pred)
precision = precision_score(val_true, val_pred, zero_division=0)
recall = recall_score(val_true, val_pred, zero_division=0)
macro_f1 = f1_score(val_true, val_pred, zero_division=0, average='macro')
micro_f1 = f1_score(val_true, val_pred, zero_division=0, average='micro')
# Log metrics to W&B
wandb.log({
"epoch": epoch,
"val_loss": val_loss / len(val_dataloader),
"accuracy": accuracy,
"precision": precision,
"recall": recall,
"macro_f1": macro_f1,
"micro_f1": micro_f1
})
# Print metrics for each epoch
print(f"Epoch {epoch+1}: val_loss: {val_loss / len(val_dataloader)}, accuracy: {accuracy}, precision: {precision}, recall: {recall}, macro_f1: {macro_f1}, micro_f1: {micro_f1}")
def main():
sweep_id = wandb.sweep(SWEEP_CONFIG, project=PROJECT_NAME)
wandb.agent(sweep_id, function=train, count=100)
wandb.finish() # terminate wandb session to avoid lags and overlaps
if __name__ == "__main__":
main()