forked from saleha1wer/MolGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune.py
214 lines (188 loc) · 9.25 KB
/
finetune.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
from ray import tune
import torch
import pytorch_lightning as pl
from sklearn.model_selection import train_test_split
import os
import numpy as np
import torch.optim as optim
from GTOT_Tuning.chem.ftlib.finetune.delta import IntermediateLayerGetter, L2Regularization, get_attribute, \
SPRegularization, FrobeniusRegularization
from GTOT_Tuning.chem.ftlib.finetune.gtot_tuning import GTOTRegularization
from torch import nn
from GTOT_Tuning.chem.commom.early_stop import EarlyStopping
from GTOT_Tuning.chem.commom.run_time import Runtime
from GTOT_Tuning.chem.commom.eval import Meter
from GTOT_Tuning.chem.splitters import scaffold_split, random_split, random_scaffold_split
from tensorboardX import SummaryWriter
from tqdm import tqdm
from copy import deepcopy
from sklearn.metrics import mean_squared_error
from ray import tune
import shutil
from torch_geometric.nn.glob import GlobalAttention, global_mean_pool
criterion = nn.MSELoss(reduction="mean")
def train_epoch(model, device, loader, optimizer, weights_regularization, backbone_regularization,
head_regularization, target_getter,
source_getter, bss_regularization, trade_off_backbone, trade_off_head, scheduler, epoch):
model.train()
meter = Meter()
loss_epoch = []
for step, batch in enumerate(tqdm(loader, desc="Iteration", disable=True)):
batch = batch.to(device)
intermediate_output_s, output_s = source_getter(batch)
intermediate_output_t, output_t = target_getter(batch)
pred = output_t
fea_s = source_getter._model.get_bottleneck()
fea = target_getter._model.get_bottleneck()
# intermediate_output_s
y = batch.y.view(pred.shape).to(torch.float64)
# Whether y is non-null or not.
is_valid = y ** 2 > 0
# Loss matrix
loss_mat = criterion(pred.double(), y)
# loss_mat = criterion(pred.double(),(y + 1.0) / 2)
# loss matrix after removing null target
loss_mat = torch.where(is_valid, loss_mat, torch.zeros(loss_mat.shape).to(loss_mat.device).to(loss_mat.dtype))
cls_loss = torch.sum(loss_mat) / torch.sum(is_valid)
meter.update(pred, y, mask=is_valid)
loss_reg_head = head_regularization()
loss_reg_backbone = 0.0
print_str = ''
loss = torch.tensor([0.0], device=device)
loss_bss = 0.0
loss_weights = torch.tensor([0.0]).to(cls_loss.device)
if trade_off_backbone > 0.0:
loss_reg_backbone = backbone_regularization(intermediate_output_s, intermediate_output_t, batch)
else:
loss_reg_backbone = backbone_regularization()
loss = loss + cls_loss + trade_off_backbone * loss_reg_backbone + trade_off_head * loss_reg_head
loss = loss + 0.1 * loss_weights
# if torch.isnan(cls_loss): # or torch.isnan(loss_reg_backbone):
# print(pred, loss_reg_backbone)
# raise
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_value_(model.parameters(), clip_value=10)
optimizer.step()
loss_epoch.append(cls_loss.item())
avg_loss = sum(loss_epoch) / len(loss_epoch)
if scheduler is not None: scheduler.step(avg_loss)
try:
print('num_oversmooth:', backbone_regularization.num_oversmooth, end=' || ')
backbone_regularization.num_oversmooth = 0
except:
pass
metric = np.mean((meter.compute_metric('rmse')))
return metric, avg_loss
def eval(model, device, loader):
model.eval()
loss_sum = []
eval_meter = Meter()
for step, batch in enumerate(tqdm(loader, desc="Iteration", disable=True)):
batch = batch.to(device)
with torch.no_grad():
pred = model(batch)
y = batch.y.view(pred.shape)
eval_meter.update(pred, y, mask=y ** 2 > 0)
is_valid = y ** 2 > 0
# Loss matrix
# loss_mat = criterion(pred.double(),(y + 1.0) / 2)
loss_mat = criterion(pred.double(), y)
# loss matrix after removing null target
loss_mat = torch.where(is_valid, loss_mat,
torch.zeros(loss_mat.shape).to(loss_mat.device).to(loss_mat.dtype))
cls_loss = torch.sum(loss_mat) / torch.sum(is_valid)
loss_sum.append(cls_loss.item())
metric = np.mean(eval_meter.compute_metric('rmse'))
return metric, sum(loss_sum) / len(loss_sum)
def finetune(save_model_name, source_model, data_module, epochs, report_to_raytune,patience=40, order=1,
trade_off_backbone=0.0005, trade_off_head=0.1,fname='finetune_logs'):
finetuned_model = deepcopy(source_model)
device = torch.device("cuda:" + str(1)) if torch.cuda.is_available() else torch.device("cpu")
finetuned_model.to(device)
source_model.to(device)
for param in source_model.parameters():
param.requires_grad = False
source_model.eval()
train_loader = data_module.train_dataloader()
test_loader = data_module.test_dataloader()
val_loader = data_module.val_dataloader()
# set up optimizer
# different learning rate for different part of GNN
model_param_group = []
model_param_group.append({"params": finetuned_model.gnn.parameters()})
if isinstance(source_model.pool, GlobalAttention):
model_param_group.append({"params": finetuned_model.pool.parameters()})
model_param_group.append({"params": finetuned_model.fc1.parameters()})
model_param_group.append({"params": finetuned_model.fc2.parameters()})
optimizer = optim.Adam(model_param_group, lr=finetuned_model.learning_rate)
# create intermediate layer getter
return_layers = ['last_layer']
# get the output feature map of the mediate layer in full model
source_getter = IntermediateLayerGetter(source_model, return_layers=return_layers)
target_getter = IntermediateLayerGetter(finetuned_model, return_layers=return_layers)
# get regularization for finetune
weights_regularization = FrobeniusRegularization(source_model.gnn, finetuned_model.gnn)
backbone_regularization = lambda x: x
bss_regularization = lambda x: x
''' the proposed method GTOT-tuning'''
backbone_regularization = GTOTRegularization(order=order)
head_regularization = L2Regularization(nn.ModuleList([finetuned_model.gnn]))
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.2, patience=10,
verbose=False,
threshold=0.0001, threshold_mode='rel', cooldown=0,
min_lr=1e-8,
eps=1e-08)
stopper = EarlyStopping(mode='lower', patience=patience, filename=save_model_name)
if os.path.exists(fname):
shutil.rmtree(fname)
print("removed the existing tensorboard file.")
print('tensorboard file', fname)
writer = SummaryWriter(fname)
training_time = Runtime()
test_time = Runtime()
train_losses = []
val_losses = []
for epoch in range(1, epochs):
print("====epoch " + str(epoch))
training_time.epoch_start()
train_acc, train_loss = train_epoch(finetuned_model, device, train_loader, optimizer,
weights_regularization,
backbone_regularization,
head_regularization, target_getter,
source_getter, bss_regularization,
trade_off_backbone, trade_off_head,
scheduler,
epoch)
training_time.epoch_end()
print("====Evaluation")
val_acc, val_loss = eval(finetuned_model, device, val_loader)
if report_to_raytune:
tune.report(loss=val_loss) # report the validation loss for the underlying tune process during HPO
test_time.epoch_start()
test_acc, test_loss = eval(finetuned_model, device, test_loader)
test_time.epoch_end()
try:
scheduler.step(-val_loss)
except:
scheduler.step()
writer.add_scalar('data/train auc', train_acc, epoch)
writer.add_scalar('data/val auc', val_acc, epoch)
writer.add_scalar('data/test auc', test_acc, epoch)
writer.add_scalar('data/train loss', train_loss, epoch)
writer.add_scalar('data/val loss', val_loss, epoch)
writer.add_scalar('data/test loss', test_loss, epoch)
print('val loss', val_loss)
print('train loss', train_loss)
train_losses.append(train_loss)
val_losses.append(val_loss)
if stopper.step(val_loss, finetuned_model, test_score=test_loss, IsMaster=True):
stopper.report_final_results(i_epoch=epoch)
break
stopper.print_best_results(i_epoch=epoch, train_loss=train_loss, val_loss=val_loss,
test_loss=test_loss)
training_time.print_mean_sum_time(prefix='Training')
test_time.print_mean_sum_time(prefix='Test')
print('tensorboard file is saved in', fname)
writer.close()
return finetuned_model,train_losses,val_losses