-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp.py
67 lines (55 loc) · 2.94 KB
/
exp.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
from src.system import *
import numpy as np
import json
import torch
import random
import argparse
from utils import slice_list, test_prediction_acc
parser = argparse.ArgumentParser()
parser.add_argument('--datapath', type=str, required=True)
parser.add_argument('--harampath', type=str, required=True)
args = parser.parse_args()
with open(f'{args.datapath}/one_hop.json') as f:
one_hop_datas = json.load(f)
random.shuffle(one_hop_datas)
with open(f'{args.datapath}/reverse.json') as f:
reverse_datas = json.load(f)
random.shuffle(reverse_datas)
with open(f'{args.datapath}/subrep.json') as f:
subrep_datas = json.load(f)
random.shuffle(subrep_datas)
rels = []
onehops = []
for data in one_hop_datas:
oneedit = OneEdit(f'{args.datapath}/hparams.yaml')
oneedit.edit_knowledge(data['prompt']+ ' ' + data['ans'])
onehop = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['onehop'], data['onehop_ans'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
rel = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['prompt'], data['ans'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
onehops.append(onehop[0])
rels.append(rel[0])
oneedit.rollback_knowledge(data['prompt']+ ' ' + data['ans'])
oneedit.release()
reverses = []
for data in reverse_datas:
oneedit = OneEdit(f'{args.datapath}/hparams.yaml')
oneedit.edit_knowledge(data['prompt']+ ' ' + data['ans'])
onehop = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['reverse'], data['subject'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
rel = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['prompt'], data['ans'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
reverses.append(onehop[0])
rels.append(rel[0])
oneedit.rollback_knowledge(data['prompt']+ ' ' + data['ans'])
oneedit.release()
subreps = []
for data in subrep_datas:
oneedit = OneEdit(f'{args.datapath}/hparams.yaml')
oneedit.edit_knowledge(data['prompt']+ ' ' + data['ans'])
subrep = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['sub_prompt'], data['ans'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
rel = test_prediction_acc(oneedit.editor.editor, oneedit.editor.tok, data['prompt'], data['ans'], oneedit.editor.device,locality=False,vanilla_generation=True if oneedit.editor.method=='MEMIT' else True)
subreps.append(subrep[0])
rels.append(rel[0])
oneedit.rollback_knowledge(data['prompt']+ ' ' + data['ans'])
oneedit.release()
print(sum(rels)/len(rels))
print(sum(onehops)/len(onehops))
print(sum(reverses)/len(reverses))
print(sum(subreps)/len(subreps))