-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlearn_transitivity.py
358 lines (297 loc) · 13.5 KB
/
learn_transitivity.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
import re
import csv
import string
from typing import List, Set, Tuple
from openai import OpenAI
import scallopy
import os
import torch
from torch.utils.data import DataLoader
from collections import defaultdict
import numpy as np
from dotenv import load_dotenv
from tqdm import tqdm
load_dotenv()
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
scallop_root_dir = os.path.abspath(os.path.join(os.path.curdir, "./scl"))
relation_id_map = {
'daughter': 0,
'sister': 1,
'son': 2,
'aunt': 3,
'father': 4,
'husband': 5,
'granddaughter': 6,
'brother': 7,
'nephew': 8,
'mother': 9,
'uncle': 10,
'grandfather': 11,
'wife': 12,
'grandmother': 13,
'niece': 14,
'grandson': 15,
'son-in-law': 16,
'father-in-law': 17,
'daughter-in-law': 18,
'mother-in-law': 19,
'nothing': 20,
}
id_relation_map = { val: key for key, val in relation_id_map.items()}
default_system_prompt = "Given a sentence and the names of two people choose the relationship between the people from the following options: daughter, sister, son, aunt, father, husband, granddaughter, brother, nephew, mother, uncle, grandfather, wife, grandmother, niece, grandson, son-in-law, father-in-law, daughter-in-law, mother-in-law. Answer in one word."
# all possible transitive rules
# 20 relationships, transitive involves 3 entites, thus 20 ^ 3 possible transitive rules
all_possible_transitives = [(a, b, c) for a in range(20) for b in range(20) for c in range(20) ]
class CLUTRRDataset:
def __init__(self, file_path):
self.data = [instance for instance in list(csv.reader(open(file_path)))[1:]]
def __len__(self):
return len(self.data)
def __getitem__(self, i):
sentences = [s.strip() for s in self.data[i][2].split(".") if s.strip() != ""]
query = eval(self.data[i][3])
query = (query[0], query[1])
answer = self.data[i][5]
return sentences, query, answer
def collate_fn(batch):
queries = [[query] for (_, query, _) in batch]
sentences = [sentences for (sentences, _, _) in batch]
answers = torch.stack([torch.tensor(relation_id_map[answer]) for (_, _, answer) in batch])
return (sentences, queries, answers)
def clutrr_loader(batch_size=32, max_entries=10094):
dataset = CLUTRRDataset("./data/_train.csv")
# split training and validation date 80/20 rule
split_idx = int(max_entries * 0.8)
train_loader = DataLoader([dataset[idx] for idx in range(split_idx)], batch_size=batch_size, shuffle=False, collate_fn=collate_fn)
val_loader = DataLoader([dataset[idx] for idx in range(split_idx, len(dataset))], batch_size=batch_size, shuffle=False, collate_fn=collate_fn)
test_dataset = []
for idx in range(2, 11):
ds = CLUTRRDataset(f"./data/_test{idx}.csv")
for elem in ds:
test_dataset.append(elem)
test_loader = DataLoader(test_dataset, batch_size, shuffle=True, collate_fn=collate_fn)
return (train_loader, val_loader, test_loader)
class DSRLMModel(torch.nn.Module):
# TODO organize hyperparams, maybe move to a separate config file or parameterize them
def __init__(self, gpt_model: str) -> None:
super(DSRLMModel, self).__init__()
self.scallop_ctx = scallopy.context.ScallopContext(provenance="difftopbottomkclauses", train_k=3, test_k=3)
# TODO adjust to appropriate scl file
self.scallop_ctx.import_file(os.path.join(scallop_root_dir, "bare.scl"))
# TODO perhaps configure this
self.scallop_ctx.set_iter_limit(10)
self.scallop_ctx.set_non_probabilistic(["question"])
self.reasoner = self.scallop_ctx.forward_function("answer", output_mapping=list(range(len(relation_id_map))))
self.gpt_model = gpt_model
# Transitivity probs: Initialize with 0.1
self.transitivity_probs = torch.tensor(np.ones(len(all_possible_transitives)) / 10, requires_grad=True)
self.sample_ct = 200
# returns [ (clean_sentence, set_of_names), ... ]
def get_contexts(self, sentences: List[str]) -> List[Tuple[str, Set[str]]]:
i = 0
contexts = []
while i < len(sentences):
sentence = sentences[i]
names = re.findall("\\[(\w+)\\]", sentence)
names = set(names)
clean_sentence = sentence.replace("[", "").replace("]", "") + "."
final_context = clean_sentence
if clean_sentence[-1] != ".":
final_context += "."
j = i + 1
while len(names) < 2:
if j < len(sentences):
sentence = sentences[j]
clean_sentence = sentence.replace("[", "").replace("]", "") + "."
final_context += " " + clean_sentence
if clean_sentence[-1] != ".":
final_context += "."
names.update(re.findall("\\[(\w+)\\]", sentence))
j += 1
else:
j = i - 1
while len(names) < 2:
sentence = sentences[j]
clean_sentence = sentence.replace("[", "").replace("]", "") + "."
if clean_sentence[-1] != ".":
final_context = clean_sentence + ". " + final_context
else:
final_context = clean_sentence + " " + final_context
names.update(re.findall("\\[(\w+)\\]", sentence))
j -= 1
break
contexts.append((final_context, names))
i += 1
return contexts
def prompt_gpt(self, system_prompt: str, prompt: str, logprobs=True, top_logprobs=1, max_tokens=10):
if logprobs:
completion = client.chat.completions.create(
model=self.gpt_model,
messages=[
{"role": "system",
"content": system_prompt},
{"role": "user",
"content": prompt}
],
logprobs=logprobs,
top_logprobs=top_logprobs,
max_tokens=max_tokens
)
else:
completion = client.chat.completions.create(
model=self.gpt_model,
messages=[
{"role": "system",
"content": system_prompt},
{"role": "user",
"content": prompt}
],
logprobs=logprobs,
max_tokens=max_tokens
)
# print(completion)
return completion
# returns answer and probability of that answeer
def prompt_for_answer(self, sentences, name1, name2) -> Tuple[str, float]:
prompt = f"{sentences}\n So {name1} is {name2}'s:"
completion = self.prompt_gpt(default_system_prompt, prompt)
answer = completion.choices[0].message.content
probability = 0
num = 0
for logprob in completion.choices[0].logprobs.content:
probability += np.exp(logprob.logprob)
num += 1
# make it lowercase, and remove excess punctuation
answer = answer.lower().translate(str.maketrans("", "", string.punctuation))
return answer, probability / num
# extracts [ (prob, (relation, name1, name2)), ... ]
def extract_facts(self, sentence_name_pairs):
facts = defaultdict(int)
for sentences, names in sentence_name_pairs:
names = list(names)
for i in range(len(names)):
for j in range(i+1, len(names)):
answer, prob = self.prompt_for_answer(sentences, names[i], names[j])
if answer in relation_id_map:
facts[(relation_id_map[answer], names[j], names[i])] = max(torch.tensor(prob), facts[(relation_id_map[answer], names[j], names[i])])
else:
facts[(20, names[j], names[i])] = max(torch.tensor(min(0.5, prob)), facts[(20, names[j], names[i])])
answer, prob = self.prompt_for_answer(sentences, names[j], names[i])
if answer in relation_id_map:
facts[(relation_id_map[answer], names[i], names[j])] = max(torch.tensor(prob), facts[(relation_id_map[answer], names[i], names[j])])
else:
facts[(20, names[i], names[j])] = max(torch.tensor(min(0.5, prob)), facts[(20, names[i], names[j])])
listfacts = []
for key, value in facts.items():
listfacts.append((value, key))
return listfacts
# TODO change to async
def make_api_calls(sentences, facts):
pass
def forward(self, X, phase="train"):
sentence_batch, queries_batch = X
contexts_batch = []
facts_batch = []
ct = 0
for sentences in sentence_batch:
ct += 1
print("onto ct", ct, end="\r")
contexts = self.get_contexts(sentences)
contexts_batch.append(contexts)
facts = self.extract_facts(contexts)
facts_batch.append(facts)
print()
transitivity_probs = torch.clamp(self.transitivity_probs, 0, 1)
if phase == 'train':
sampled_transitive_idx = torch.multinomial(transitivity_probs, self.sample_ct)
else:
_, sampled_transitive_idx = torch.topk(transitivity_probs, self.sample_ct)
probs = transitivity_probs[sampled_transitive_idx]
transitives = [all_possible_transitives[i] for i in sampled_transitive_idx]
transitive_relations = [[(prob, relation) for prob, relation in zip(probs, transitives)] for _ in range(len(sentence_batch))]
result = self.reasoner(
question=queries_batch,
context=facts_batch,
transitive=transitive_relations)
return result
class Trainer:
def __init__(self) -> None:
self.train_loader, self.val_loader, self.test_loader = clutrr_loader(batch_size=32, max_entries=500)
self.model = DSRLMModel(gpt_model=os.getenv("GPT_MODEL"))
self.optimizer = torch.optim.Adam([self.model.transitivity_probs], lr=0.01)
def accuracy(self, y_pred, y):
batch_size = len(y)
pred = torch.argmax(y_pred, dim=1)
num_correct = len([() for i, j in zip(pred, y) if i == j])
return (num_correct, batch_size)
def loss(self, y_pred: torch.Tensor, y: torch.Tensor):
_, dim = y_pred.shape
# y is a 1d array, where each elem contains the idx of the relation
# convert it into a 2d array to compute loss
ground_truths = torch.stack([torch.tensor([1.0 if i == t else 0.0 for i in range(dim)]) for t in y])
return torch.nn.functional.binary_cross_entropy(y_pred, ground_truths)
def train(self, num_epochs: int):
for i in range(1, num_epochs + 1):
self.train_epoch(i)
self.test_epoch(i)
def train_epoch(self, epoch: int):
self.model.train()
total_count = 0
total_correct = 0
total_loss = 0
iterator = tqdm(self.train_loader)
for (i, batch) in enumerate(iterator):
self.optimizer.zero_grad()
sentences, queries, y = batch
y_pred = self.model((sentences, queries), phase="train")
loss = self.loss(y_pred, y)
total_loss += loss.item()
loss.backward()
self.optimizer.step()
(num_correct, batch_size) = self.accuracy(y_pred, y)
total_count += batch_size
total_correct += num_correct
correct_perc = 100. * total_correct / total_count
avg_loss = total_loss / (i + 1)
iterator.set_description(f"[Train Epoch {epoch}] Avg Loss: {avg_loss}, Accuracy: {total_correct}/{total_count} ({correct_perc:.2f}%)")
def test_epoch(self, epoch: int):
self.model.eval()
total_count = 0
total_correct = 0
total_loss = 0
with torch.no_grad():
iterator = tqdm(self.test_loader)
for (i, batch) in enumerate(iterator):
sentences, queries, y = batch
y_pred = self.model((sentences, queries), 'test')
print("we loss", i)
loss = self.loss(y_pred, y)
total_loss += loss.item()
(num_correct, batch_size) = self.accuracy(y_pred, y)
total_count += batch_size
total_correct += num_correct
correct_perc = 100. * total_correct / total_count
avg_loss = total_loss / (i + 1)
iterator.set_description(f"[Test Epoch {epoch}] Avg Loss: {avg_loss}, Accuracy: {total_correct}/{total_count} ({correct_perc:.2f}%)")
# Save model
if total_correct / total_count > self.max_accu:
self.max_accu = total_correct / total_count
torch.save(self.model, f"./trans.best.model")
torch.save(self.model, f"./trans.latest.model")
def get_rules(self, threshold=0.6):
pred_probs = self.model.transitivity_probs.reshape(-1)
rules = pred_probs > threshold
indices = rules.nonzero()
rules = sorted([(pred_probs[index].item(), [id_relation_map[e] for e in all_possible_transitives[index]]) for index in indices], reverse=True)
return rules
def pretty_print_rules(rules):
rule_str = '\n'.join([f"{p};{r}" for p, r in rules])
print(rule_str)
return rule_str
if __name__ == "__main__":
trainer = Trainer()
trainer.train(num_epochs=3)
rules = trainer.get_rules()
pp_rules = pretty_print_rules(rules)
with open("learned_rules.txt", "w") as f:
f.write(pp_rules)