forked from LechengKong/OneForAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlightning_model.py
More file actions
268 lines (220 loc) · 8.27 KB
/
Copy pathlightning_model.py
File metadata and controls
268 lines (220 loc) · 8.27 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
import torch
from gp.lightning.module_template import BaseTemplate
from lightning.pytorch import LightningModule
import os.path as osp
class GraphPredLightning(BaseTemplate):
def forward(self, batch):
return self.model(batch)
class TokenPredLightning(BaseTemplate):
def forward(self, batch):
return self.model(batch)
def validation_step(self, batch, batch_idx):
tokens = self.model.generate(batch)
with torch.no_grad():
if self.eval_kit.has_eval_state("valid"):
self.eval_kit.eval_step(tokens, batch, "valid")
self.val_names = ["valid"]
def test_step(self, batch, batch_idx):
tokens = self.model.generate(batch)
with torch.no_grad():
if self.eval_kit.has_eval_state("test"):
self.eval_kit.eval_step(tokens, batch, "test")
class GraphFinetuneLightning(BaseTemplate):
def forward(self, batch):
return self.model(batch)
def configure_optimizers(self):
optimizer = self.exp_config.optimizer(
self.exp_config.opt_params
if self.exp_config.opt_params is not None
else filter(lambda p: p.requires_grad, self.parameters()),
**self.exp_config.optimizer_args
)
return {
"optimizer": optimizer,
}
class CLBaseTemplate(LightningModule):
def __init__(
self,
exp_config,
encode_model: torch.nn.Module,
loss,
params,
eval_params,
proj_head=None,
pred_head=None,
sup_cl=False,
name = "",
):
super().__init__()
self.exp_config = exp_config
self.model = encode_model
self.loss = loss
self.params = params
self.eval_params = eval_params
self.name = name
self.proj_head = proj_head
self.pred_head = pred_head
self.sup_cl = sup_cl
if self.pred_head is not None:
self.bin_loss = torch.nn.BCEWithLogitsLoss()
def forward(self, batch):
return self.model(batch)
def configure_optimizers(self):
optimizer = self.exp_config.optimizer(
self.exp_config.opt_params
if self.exp_config.opt_params is not None
else self.model.parameters(),
**self.exp_config.optimizer_args
)
return {
"optimizer": optimizer,
}
def _calculate_cl_loss(self, g1, g2, z1, z2):
# mask1 = g1.h_node_mask + g1.true_nodes_mask + g1.spt_nodes_mask + g1.target_node_mask
# mask2 = g2.h_node_mask + g2.true_nodes_mask + g2.spt_nodes_mask + g2.target_node_mask
mask1 = g1.true_nodes_mask
mask2 = g2.true_nodes_mask
if self.proj_head != None:
z1 = self.proj_head(z1)[mask1]
z2 = self.proj_head(z2)[mask2]
z1 = torch.nn.functional.normalize(z1, dim=1)
z2 = torch.nn.functional.normalize(z2, dim=1)
h_node_embs = torch.stack([z1, z2], dim=1)
# generate supervised mask for h_nodes
# mask = torch.arange(self.params.n_way).repeat_interleave(self.params.q_query)
# from losses import SupConLoss
# loss = SupConLoss(temperature=0.07)
#sup_loss = loss(features=h_node_embs, labels=mask)
if self.sup_cl:
print(torch.matmul(g1.bin_labels.view(-1,1), g1.bin_labels.view(1,-1)))
cl_loss = self.loss(features=h_node_embs, mask=torch.matmul(g1.bin_labels.view(-1,1), g1.bin_labels.view(1,-1)))
else:
cl_loss = self.loss(features=h_node_embs, )
return cl_loss
def _calculate_binary_cls_loss(self, g, z):
z = self.pred_head(z)
score = z[g.true_nodes_mask]
labels = g.bin_labels
valid_ind = labels == labels
return self.bin_loss(score.view(-1)[valid_ind], labels[valid_ind])
def combine_loss(self, batch):
g1, g2 = batch
z1 = self(g1)
z2 = self(g2)
cl_loss = self._calculate_cl_loss(g1, g2, z1, z2)
if self.pred_head is not None:
binary_cls_loss = self._calculate_binary_cls_loss(g1, z1) + self._calculate_binary_cls_loss(g2, z2)
return (cl_loss, cl_loss+binary_cls_loss, binary_cls_loss,)
return (cl_loss, cl_loss)
def training_step(self, batch, batch_idx):
# get embeddings of h nodes (n_way x q_query x fs_task_num, emb_dim)
# current only consider fs_task_num = 1
loss = self.combine_loss(batch)
loss_name = [osp.join(self.name, "train", "cl_loss"),
osp.join(self.name, "train", "loss"),
osp.join(self.name, "train", "pred_loss"),]
log_dict = {loss_name[idx]: loss[idx] for idx in range(len(loss))}
self.log_dict(
log_dict,
on_step=True,
on_epoch=True,
prog_bar=True,
batch_size=batch.batch_size
if hasattr(batch, "batch_size")
else len(batch),
)
return loss[1]
def validation_step(self, batch, batch_idx):
# get embeddings of h nodes (n_way x q_query x fs_task_num, emb_dim)
# current only consider fs_task_num = 1
loss = self.combine_loss(batch)
loss_name = [osp.join(self.name, "val", "cl_loss"),
osp.join(self.name, "val", "loss"),
osp.join(self.name, "val", "pred_loss")]
log_dict = {loss_name[idx]: loss[idx] for idx in range(len(loss))}
self.log_dict(
log_dict,
on_step=True,
on_epoch=True,
prog_bar=True,
batch_size=batch.batch_size
if hasattr(batch, "batch_size")
else len(batch),
)
return loss[1]
class SimPredLightning(LightningModule):
def __init__(
self,
exp_config,
encode_model: torch.nn.Module,
loss,
head = None,
):
super().__init__()
self.exp_config = exp_config
self.model = encode_model
self.loss = loss
self.head = head
def forward(self, batch):
if self.head != None:
return self.head(self.model(batch))
else:
return self.model(batch)
def configure_optimizers(self):
optimizer = self.exp_config.optimizer(
self.exp_config.opt_params
if self.exp_config.opt_params is not None
else self.model.parameters(),
**self.exp_config.optimizer_args
)
return {
"optimizer": optimizer,
}
def training_step(self, batch, batch_idx):
# emb = self.batch
# qry_emb = self.batch[batch.target_node_mask]
print(batch.x[batch.target_node_mask])
print(batch.x[batch.true_nodes_mask])
print('-'*40)
loss, acc = self.loss(self(batch)[batch.target_node_mask], self(batch)[batch.true_nodes_mask], batch.bin_labels)
log = {"train_loss": loss, "train_acc": acc}
self.log_dict(
log,
on_step=True,
on_epoch=True,
prog_bar=True,
batch_size=batch.batch_size
if hasattr(batch, "batch_size")
else len(batch),
)
return loss
def validation_step(self, batch, batch_idx, dataloader_idx):
loss, acc = self.loss(self(batch)[batch.target_node_mask], self(batch)[batch.true_nodes_mask], batch.bin_labels)
if dataloader_idx == 0:
log = {"val_loss": loss, "val_acc": acc}
elif dataloader_idx == 1:
log = {"t_val_loss": loss, "t_val_acc": acc}
log = {"idk_loss": loss, "idk_acc": acc}
self.log_dict(
log,
on_step=False,
on_epoch=True,
prog_bar=True,
batch_size=batch.batch_size
if hasattr(batch, "batch_size")
else len(batch),
)
return loss
def test_step(self, batch, batch_idx):
loss, acc = self.loss(self(batch)[batch.target_node_mask], self(batch)[batch.true_nodes_mask], batch.bin_labels)
log = {"test_loss": loss, "test_acc": acc}
self.log_dict(
log,
on_step=False,
on_epoch=True,
prog_bar=True,
batch_size=batch.batch_size
if hasattr(batch, "batch_size")
else len(batch),
)
return loss