-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathretrieval_nre.py
153 lines (108 loc) · 4.13 KB
/
retrieval_nre.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
import argparse
import torch
import torch.nn as nn
import random
import numpy as np
import os
import sys
from torch_geometric.loader import DataLoader
from torch.utils.data import TensorDataset
from tqdm import tqdm
import json
from sklearn.model_selection import train_test_split
from models import GraphNetwork, GraphNetwork_prop
import utils_main
from collections import defaultdict
from tqdm import tqdm
import pickle
from collections import defaultdict
torch.set_num_threads(4)
os.environ['OMP_NUM_THREADS'] = "4"
def seed_everything(seed):
# To fix the random seed
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
# backends
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# def make_retrieved(mode, split, rank_matrix, k, seed):
# save_path = f'./output/new_retrieved_formation/sum_again_fast_0502_year_{mode}_seed_{seed}_retrieved_{k}'
# candidate_list = defaultdict(list)
# for idx, sim_row in enumerate(rank_matrix):
# top_k_val, top_k_idx = torch.topk(sim_row, k, largest=False)
# candidate_list[idx] = top_k_idx.tolist()
# with open(save_path, 'w') as f:
# json.dump(candidate_list, f)
def main():
args = utils_main.parse_args()
train_config = utils_main.training_config(args)
configuration = utils_main.exp_get_name1(train_config)
print(f'configuration: {configuration}')
K = args.K
device = torch.device(f'cuda:{args.device}' if torch.cuda.is_available() else 'cpu')
torch.cuda.set_device(device)
print(device)
seed_everything(seed=args.seed)
nre_train = f'./dataset/year_train_nre_retrieved_{K}'
nre_valid = f'./dataset/year_valid_nre_retrieved_{K}'
nre_test = f'./dataset/year_test_nre_retrieved_{K}'
with open(nre_train, 'r') as f:
reaction_train = json.load(f)
with open(nre_valid, 'r') as f:
reaction_valid = json.load(f)
with open(nre_test, 'r') as f:
reaction_test = json.load(f)
mpc_train = f'./dataset/year_train_mpc_retrieved_{K}'
mpc_valid = f'./dataset/year_valid_mpc_retrieved_{K}'
mpc_test = f'./dataset/year_test_mpc_retrieved_{K}'
with open(mpc_train, 'r') as f:
mpc_train = json.load(f)
with open(mpc_valid, 'r') as f:
mpc_valid = json.load(f)
with open(mpc_test, 'r') as f:
mpc_test = json.load(f)
reaction_mpc_train = defaultdict(list)
for idx, (reaction, mpc) in enumerate(zip(reaction_train, mpc_train.values())):
if reaction is None:
reaction = []
else:
reaction = reaction.copy()
if len(reaction) < K:
shortage = K - len(reaction)
reaction.extend(mpc[:shortage])
reaction_mpc_train[idx] = reaction[:K]
save_path = f'./dataset/year_train_nre_final_retrieved_{K}'
with open(save_path, 'w') as f:
json.dump(reaction_mpc_train, f)
reaction_mpc_valid = defaultdict(list)
for idx, (reaction, mpc) in enumerate(zip(reaction_valid, mpc_valid.values())):
if reaction is None:
reaction = []
else:
reaction = reaction.copy()
if len(reaction) < K:
shortage = K - len(reaction)
reaction.extend(mpc[:shortage])
reaction_mpc_valid[idx] = reaction[:K]
save_path = f'./dataset/year_valid_nre_final_retrieved_{K}'
with open(save_path, 'w') as f:
json.dump(reaction_mpc_valid, f)
reaction_mpc_test = defaultdict(list)
for idx, (reaction, mpc) in enumerate(zip(reaction_test, mpc_test.values())):
if reaction is None:
reaction = []
else:
reaction = reaction.copy()
if len(reaction) < K:
shortage = K - len(reaction)
reaction.extend(mpc[:shortage])
reaction_mpc_test[idx] = reaction[:K]
save_path = f'./dataset/year_test_nre_final_retrieved_{K}'
with open(save_path, 'w') as f:
json.dump(reaction_mpc_test, f)
if __name__ == "__main__":
main()