forked from ChangwenXu98/TransPolymer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Downstream.py
482 lines (383 loc) · 21.5 KB
/
Downstream.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
import pdb
import pandas as pd
import numpy as np
import sys
import yaml
from tqdm.auto import tqdm
import torch
import torch.nn as nn
from torch.utils.data import Dataset, DataLoader
from torch.nn.utils.clip_grad import clip_grad_norm
import torchinfo
from transformers import AdamW, get_linear_schedule_with_warmup, RobertaModel, RobertaConfig, RobertaTokenizer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, multilabel_confusion_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import KFold
from rdkit import Chem
from pylab import rcParams
import matplotlib.pyplot as plt
from matplotlib import rc
from packaging import version
import torchmetrics
from torchmetrics import R2Score
from PolymerSmilesTokenization import PolymerSmilesTokenizer
from dataset import Downstream_Dataset, DataAugmentation
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter()
from copy import deepcopy
np.random.seed(seed=1)
"""Layer-wise learning rate decay"""
def roberta_base_AdamW_LLRD(model, lr, weight_decay):
opt_parameters = [] # To be passed to the optimizer (only parameters of the layers you want to update).
named_parameters = list(model.named_parameters())
print("number of named parameters =", len(named_parameters))
# According to AAAMLP book by A. Thakur, we generally do not use any decay
# for bias and LayerNorm.weight layers.
no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
# === Pooler and Regressor ======================================================
params_0 = [p for n, p in named_parameters if ("pooler" in n or "Regressor" in n)
and any(nd in n for nd in no_decay)]
print("params in pooler and regressor without decay =", len(params_0))
params_1 = [p for n, p in named_parameters if ("pooler" in n or "Regressor" in n)
and not any(nd in n for nd in no_decay)]
print("params in pooler and regressor with decay =", len(params_1))
head_params = {"params": params_0, "lr": lr, "weight_decay": 0.0}
opt_parameters.append(head_params)
head_params = {"params": params_1, "lr": lr, "weight_decay": weight_decay}
opt_parameters.append(head_params)
print("pooler and regressor lr =", lr)
# === Hidden layers ==========================================================
for layer in range(5, -1, -1):
params_0 = [p for n, p in named_parameters if f"encoder.layer.{layer}." in n
and any(nd in n for nd in no_decay)]
print(f"params in hidden layer {layer} without decay =", len(params_0))
params_1 = [p for n, p in named_parameters if f"encoder.layer.{layer}." in n
and not any(nd in n for nd in no_decay)]
print(f"params in hidden layer {layer} with decay =", len(params_1))
layer_params = {"params": params_0, "lr": lr, "weight_decay": 0.0}
opt_parameters.append(layer_params)
layer_params = {"params": params_1, "lr": lr, "weight_decay": weight_decay}
opt_parameters.append(layer_params)
print("hidden layer", layer, "lr =", lr)
lr *= 0.9
# === Embeddings layer ==========================================================
params_0 = [p for n, p in named_parameters if "embeddings" in n
and any(nd in n for nd in no_decay)]
print("params in embeddings layer without decay =", len(params_0))
params_1 = [p for n, p in named_parameters if "embeddings" in n
and not any(nd in n for nd in no_decay)]
print("params in embeddings layer with decay =", len(params_1))
embed_params = {"params": params_0, "lr": lr, "weight_decay": 0.0}
opt_parameters.append(embed_params)
embed_params = {"params": params_1, "lr": lr, "weight_decay": weight_decay}
opt_parameters.append(embed_params)
print("embedding layer lr =", lr)
return AdamW(opt_parameters, lr=lr)
"""Model"""
class GlobalAveragePooling1D(nn.Module):
def __init__(self):
super(GlobalAveragePooling1D, self).__init__()
def forward(self, x):
return torch.mean(x, dim=1)
class DownstreamRegression(nn.Module):
def __init__(self, drop_rate=0.1):
super(DownstreamRegression, self).__init__()
self.PretrainedModel = deepcopy(PretrainedModel)
self.PretrainedModel.resize_token_embeddings(len(tokenizer))
self.pooler = GlobalAveragePooling1D()
self.Regressor = nn.Sequential(
nn.Dropout(drop_rate),
nn.Linear(self.PretrainedModel.config.hidden_size, self.PretrainedModel.config.hidden_size),
nn.SiLU(),
nn.Linear(self.PretrainedModel.config.hidden_size, 1)
)
def forward(self, input_ids, attention_mask):
outputs = self.PretrainedModel(input_ids=input_ids, attention_mask=attention_mask)
# logits = outputs.last_hidden_state[:, 0, :]
#Trying Global Average Pooling
last_hidden_state = outputs.last_hidden_state[:,:,:]
pooled_output = self.pooler(last_hidden_state)
logits = pooled_output
output = self.Regressor(logits)
return output
"""Train"""
def train(model, optimizer, scheduler, loss_fn, train_dataloader, device):
model.train()
print("Number of parameteres are: ", sum(param.numel() for param in model.parameters() if param.requires_grad))
for step, batch in enumerate(train_dataloader):
input_ids = batch["input_ids"].to(device)
attention_mask = batch["attention_mask"].to(device)
prop = batch["prop"].to(device).float()
optimizer.zero_grad()
outputs = model(input_ids, attention_mask).float()
loss = loss_fn(outputs.squeeze(), prop.squeeze())
loss.backward()
optimizer.step()
scheduler.step()
return None
def test(model, loss_fn, train_dataloader, test_dataloader, device, scaler, optimizer, scheduler, epoch):
r2score = R2Score()
train_loss = 0
test_loss = 0
# count = 0
model.eval()
with torch.no_grad():
train_pred, train_true, test_pred, test_true = torch.tensor([]), torch.tensor([]), torch.tensor(
[]), torch.tensor([])
for step, batch in enumerate(train_dataloader):
input_ids = batch["input_ids"].to(device)
attention_mask = batch["attention_mask"].to(device)
prop = batch["prop"].to(device).float()
outputs = model(input_ids, attention_mask).float()
outputs = torch.from_numpy(scaler.inverse_transform(outputs.cpu().reshape(-1, 1)))
prop = torch.from_numpy(scaler.inverse_transform(prop.cpu().reshape(-1, 1)))
loss = loss_fn(outputs.squeeze(), prop.squeeze())
train_loss += loss.item() * len(prop)
train_pred = torch.cat([train_pred.to(device), outputs.to(device)])
train_true = torch.cat([train_true.to(device), prop.to(device)])
train_loss = train_loss / len(train_pred.flatten())
r2_train = r2score(train_pred.flatten().to("cpu"), train_true.flatten().to("cpu")).item()
print("train RMSE = ", np.sqrt(train_loss))
print("train r^2 = ", r2_train)
for step, batch in enumerate(test_dataloader):
input_ids = batch["input_ids"].to(device)
attention_mask = batch["attention_mask"].to(device)
prop = batch["prop"].to(device).float()
outputs = model(input_ids, attention_mask).float()
outputs = torch.from_numpy(scaler.inverse_transform(outputs.cpu().reshape(-1, 1)))
prop = torch.from_numpy(scaler.inverse_transform(prop.cpu().reshape(-1, 1)))
loss = loss_fn(outputs.squeeze(), prop.squeeze())
test_loss += loss.item() * len(prop)
test_pred = torch.cat([test_pred.to(device), outputs.to(device)])
test_true = torch.cat([test_true.to(device), prop.to(device)])
test_loss = test_loss / len(test_pred.flatten())
r2_test = r2score(test_pred.flatten().to("cpu"), test_true.flatten().to("cpu")).item()
print("test RMSE = ", np.sqrt(test_loss))
print("test r^2 = ", r2_test)
writer.add_scalar("Loss/train", train_loss, epoch)
writer.add_scalar("r^2/train", r2_train, epoch)
writer.add_scalar("Loss/test", test_loss, epoch)
writer.add_scalar("r^2/test", r2_test, epoch)
state = {'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(),
'epoch': epoch}
torch.save(state, finetune_config['save_path'])
return train_loss, test_loss, r2_train, r2_test
"""
if r2_test > best_test_r2:
best_train_r2 = r2_train
best_test_r2 = r2_test
train_loss_best = train_loss
test_loss_best = test_loss
count = 0
else:
count += 1
if r2_test > best_r2:
best_r2 = r2_test
torch.save(state, finetune_config['best_model_path']) # save the best model
if count >= finetune_config['tolerance']:
print("Early stop")
if best_test_r2 == 0:
print("Poor performance with negative r^2")
return None
else:
return train_loss_best, test_loss_best, best_train_r2, best_test_r2, best_r2
return train_loss_best, test_loss_best, best_train_r2, best_test_r2, best_r2
"""
def main(finetune_config):
"""Tokenizer"""
if finetune_config['add_vocab_flag']:
vocab_sup = pd.read_csv(finetune_config['vocab_sup_file'], header=None).values.flatten().tolist()
tokenizer.add_tokens(vocab_sup)
best_r2 = 0.0 # monitor the best r^2 in the run
"""Data"""
if finetune_config['CV_flag']:
print("Start Cross Validation")
data = pd.read_csv(finetune_config['train_file'])
"""K-fold"""
splits = KFold(n_splits=finetune_config['k'], shuffle=True,
random_state=1) # k=1 for train-test split and k=5 for cross validation
train_loss_avg, test_loss_avg, train_r2_avg, test_r2_avg = [], [], [], [] # monitor the best metrics in each fold
for fold, (train_idx, val_idx) in enumerate(splits.split(np.arange(data.shape[0]))):
print('Fold {}'.format(fold + 1))
train_data = data.loc[train_idx, :].reset_index(drop=True)
test_data = data.loc[val_idx, :].reset_index(drop=True)
if finetune_config['aug_flag']:
print("Data Augamentation")
DataAug = DataAugmentation(finetune_config['aug_indicator'])
train_data = DataAug.smiles_augmentation(train_data)
if finetune_config['aug_special_flag']:
train_data = DataAug.smiles_augmentation_2(train_data)
train_data = DataAug.combine_smiles(train_data)
test_data = DataAug.combine_smiles(test_data)
train_data = DataAug.combine_columns(train_data)
test_data = DataAug.combine_columns(test_data)
scaler = StandardScaler()
train_data.iloc[:, 1] = scaler.fit_transform(train_data.iloc[:, 1].values.reshape(-1, 1))
test_data.iloc[:, 1] = scaler.transform(test_data.iloc[:, 1].values.reshape(-1, 1))
train_dataset = Downstream_Dataset(train_data, tokenizer, finetune_config['blocksize'])
test_dataset = Downstream_Dataset(test_data, tokenizer, finetune_config['blocksize'])
train_dataloader = DataLoader(train_dataset, finetune_config['batch_size'], shuffle=True, num_workers=finetune_config["num_workers"])
test_dataloader = DataLoader(test_dataset, finetune_config['batch_size'], shuffle=False, num_workers=finetune_config["num_workers"])
"""Parameters for scheduler"""
steps_per_epoch = train_data.shape[0] // finetune_config['batch_size']
training_steps = steps_per_epoch * finetune_config['num_epochs']
warmup_steps = int(training_steps * finetune_config['warmup_ratio'])
"""Train the model"""
model = DownstreamRegression(drop_rate=finetune_config['drop_rate']).to(device)
model = model.double()
loss_fn = nn.MSELoss()
if finetune_config['LLRD_flag']:
optimizer = roberta_base_AdamW_LLRD(model, finetune_config['lr_rate'], finetune_config['weight_decay'])
else:
optimizer = AdamW(
[
{"params": model.PretrainedModel.parameters(), "lr": finetune_config['lr_rate'],
"weight_decay": 0.0},
{"params": model.Regressor.parameters(), "lr": finetune_config['lr_rate_reg'],
"weight_decay": finetune_config['weight_decay']},
]
)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps,
num_training_steps=training_steps)
torch.cuda.empty_cache()
train_loss_best, test_loss_best, best_train_r2, best_test_r2 = 0.0, 0.0, 0.0, 0.0 # Keep track of the best test r^2 in one fold. If cross-validation is not used, that will be the same as best_r2.
count = 0 # Keep track of how many successive non-improvement epochs
for epoch in range(finetune_config['num_epochs']):
print("epoch: %s/%s" % (epoch+1, finetune_config['num_epochs']))
train(model, optimizer, scheduler, loss_fn, train_dataloader, device)
train_loss, test_loss, r2_train, r2_test = test(model, loss_fn, train_dataloader,
test_dataloader, device, scaler,
optimizer, scheduler, epoch)
if r2_test > best_test_r2:
best_train_r2 = r2_train
best_test_r2 = r2_test
train_loss_best = train_loss
test_loss_best = test_loss
count = 0
else:
count += 1
if r2_test > best_r2:
best_r2 = r2_test
state = {'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(), 'epoch': epoch, 'fold:': fold}
torch.save(state, finetune_config['best_model_path']) # save the best model
if count >= finetune_config['tolerance']:
print("Early stop")
if best_test_r2 == 0:
print("Poor performance with negative r^2")
break
train_loss_avg.append(np.sqrt(train_loss_best))
test_loss_avg.append(np.sqrt(test_loss_best))
train_r2_avg.append(best_train_r2)
test_r2_avg.append(best_test_r2)
writer.flush()
"""Average of metrics over all folds"""
train_rmse = np.mean(np.array(train_loss_avg))
test_rmse = np.mean(np.array(test_loss_avg))
train_r2 = np.mean(np.array(train_r2_avg))
test_r2 = np.mean(np.array(test_r2_avg))
std_test_rmse = np.std(np.array(test_loss_avg))
std_test_r2 = np.std(np.array(test_r2_avg))
print("Train RMSE =", train_rmse)
print("Test RMSE =", test_rmse)
print("Train R^2 =", train_r2)
print("Test R^2 =", test_r2)
print("Standard Deviation of Test RMSE =", std_test_rmse)
print("Standard Deviation of Test R^2 =", std_test_r2)
else:
print("Train Test Split")
train_data = pd.read_csv(finetune_config['train_file'])
test_data = pd.read_csv(finetune_config['test_file'])
if finetune_config['aug_flag']:
print("Data Augmentation")
DataAug = DataAugmentation(finetune_config['aug_indicator'])
train_data = DataAug.smiles_augmentation(train_data)
if finetune_config['aug_special_flag']:
train_data = DataAug.smiles_augmentation_2(train_data)
train_data = DataAug.combine_smiles(train_data)
test_data = DataAug.combine_smiles(test_data)
train_data = DataAug.combine_columns(train_data)
test_data = DataAug.combine_columns(test_data)
scaler = StandardScaler()
train_data.iloc[:, 1] = scaler.fit_transform(train_data.iloc[:, 1].values.reshape(-1, 1))
test_data.iloc[:, 1] = scaler.transform(test_data.iloc[:, 1].values.reshape(-1, 1))
train_dataset = Downstream_Dataset(train_data, tokenizer, finetune_config['blocksize'])
test_dataset = Downstream_Dataset(test_data, tokenizer, finetune_config['blocksize'])
train_dataloader = DataLoader(train_dataset, finetune_config['batch_size'], shuffle=True, num_workers=finetune_config["num_workers"])
test_dataloader = DataLoader(test_dataset, finetune_config['batch_size'], shuffle=False, num_workers=finetune_config["num_workers"])
"""Parameters for scheduler"""
steps_per_epoch = train_data.shape[0] // finetune_config['batch_size']
training_steps = steps_per_epoch * finetune_config['num_epochs']
warmup_steps = int(training_steps * finetune_config['warmup_ratio'])
"""Train the model"""
model = DownstreamRegression(drop_rate=finetune_config['drop_rate']).to(device)
model = model.double()
loss_fn = nn.MSELoss()
if finetune_config['LLRD_flag']:
optimizer = roberta_base_AdamW_LLRD(model, finetune_config['lr_rate'], finetune_config['weight_decay'])
else:
optimizer = AdamW(
[
{"params": model.PretrainedModel.parameters(), "lr": finetune_config['lr_rate'],
"weight_decay": 0.0},
{"params": model.Regressor.parameters(), "lr": finetune_config['lr_rate_reg'],
"weight_decay": finetune_config['weight_decay']},
]
)
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=warmup_steps,
num_training_steps=training_steps)
torch.cuda.empty_cache()
train_loss_best, test_loss_best, best_train_r2, best_test_r2 = 0.0, 0.0, 0.0, 0.0 # Keep track of the best test r^2 in one fold. If cross-validation is not used, that will be the same as best_r2.
count = 0 # Keep track of how many successive non-improvement epochs
for epoch in range(finetune_config['num_epochs']):
print("epoch: %s/%s" % (epoch+1,finetune_config['num_epochs']))
train(model, optimizer, scheduler, loss_fn, train_dataloader, device)
train_loss, test_loss, r2_train, r2_test = test(model, loss_fn, train_dataloader,
test_dataloader, device, scaler,
optimizer, scheduler, epoch)
if r2_test > best_test_r2:
best_train_r2 = r2_train
best_test_r2 = r2_test
train_loss_best = train_loss
test_loss_best = test_loss
count = 0
else:
count += 1
if r2_test > best_r2:
best_r2 = r2_test
state = {'model': model.state_dict(), 'optimizer': optimizer.state_dict(), 'scheduler': scheduler.state_dict(), 'epoch': epoch}
torch.save(state, finetune_config['best_model_path']) # save the best model
if count >= finetune_config['tolerance']:
print("Early stop")
if best_test_r2 == 0:
print("Poor performance with negative r^2")
break
writer.flush()
if __name__ == "__main__":
finetune_config = yaml.load(open("config_finetune.yaml", "r"), Loader=yaml.FullLoader)
print(finetune_config)
"""Device"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
#device = torch.device("cpu")
if finetune_config['model_indicator'] == 'pretrain':
print("Use the pretrained model")
PretrainedModel = RobertaModel.from_pretrained(finetune_config['model_path'])
tokenizer = PolymerSmilesTokenizer.from_pretrained("/project/rcc/hyadav/roberta-base", max_len=finetune_config['blocksize'])
PretrainedModel.config.hidden_dropout_prob = finetune_config['hidden_dropout_prob']
PretrainedModel.config.attention_probs_dropout_prob = finetune_config['attention_probs_dropout_prob']
else:
print("No Pretrain")
config = RobertaConfig(
vocab_size=50265,
max_position_embeddings=514,
num_attention_heads=12,
num_hidden_layers=6,
type_vocab_size=1,
hidden_dropout_prob=0.1,
attention_probs_dropout_prob=0.1
)
PretrainedModel = RobertaModel(config=config)
tokenizer = RobertaTokenizer.from_pretrained("/project/rcc/hyadav/ChemBERTa-77M-MLM", max_len=finetune_config['blocksize'])
max_token_len = finetune_config['blocksize']
"""Run the main function"""
main(finetune_config)