-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_flow.py
207 lines (149 loc) · 7.9 KB
/
main_flow.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
# %%
from MTNCI import MTNCI, Prediction
import sys
import torch
import numpy as np
from DatasetManager import DatasetManager, Filter
from preprocessing.utils import load_data_with_pickle, save_data_with_pickle, euclidean_similarity
from preprocessing.CorpusManager import CorpusManager
from geoopt.optim import RiemannianAdam
import time
from sklearn.metrics.pairwise import cosine_similarity
multilabel = False
load_dataset = True
filter_dataset = True
threshold = 0.1
distance = cosine_similarity
normalize = True
exclude_min_threshold = 20
NAME = '1_fair_cosine_50'
tensorboard_run_ID = NAME
results_path = 'results/excel_results/' + NAME + '.txt'
TSV_path = 'results/excel_results/export_' + NAME + '.txt'
nickel = True
lr = 1e-3
regularized = True
regul_dict = {'negative_sampling': 0, 'mse': 50, 'distance_power':1}
llambda = 0.1
weighted = True
epochs = 50
FILE_ID = '16_3'
SOURCE_FILES_PATH = '/datahdd/vmanuel/MTNCI_datasets/source_files/'
# SOURCE_FILES_PATH = '../source_files/'
EMBEDDING_PATH = SOURCE_FILES_PATH + 'embeddings/'
# PATH_TO_HYPERBOLIC_EMBEDDING = EMBEDDING_PATH + FILE_ID + 'final_tree_HyperE_MTNCI'
# PATH_TO_HYPERBOLIC_EMBEDDING = EMBEDDING_PATH + FILE_ID + 'final_tree_HyperE_MTNCI_32'
# PATH_TO_HYPERBOLIC_EMBEDDING = EMBEDDING_PATH + FILE_ID + '_multilabel_final_tree_HyperE_MTNCI_10'
PATH_TO_HYPERBOLIC_EMBEDDING = EMBEDDING_PATH + FILE_ID + '16_3_nickel.pth'
PATH_TO_DISTRIBUTIONAL_EMBEDDING = EMBEDDING_PATH + FILE_ID + 'final_tree_type2vec_MTNCI'
CONCEPT_EMBEDDING_PATHS = [PATH_TO_DISTRIBUTIONAL_EMBEDDING,
PATH_TO_HYPERBOLIC_EMBEDDING]
DATASET_PATH = SOURCE_FILES_PATH + 'vectors/'
if not multilabel:
X_PATH = DATASET_PATH + FILE_ID + 'X'
Y_PATH = DATASET_PATH + FILE_ID + 'Y'
ENTITIES_PATH = DATASET_PATH + FILE_ID + 'entities'
else:
X_PATH = DATASET_PATH + FILE_ID + 'X_multilabel'
Y_PATH = DATASET_PATH + FILE_ID + 'Y_multilabel'
ENTITIES_PATH = DATASET_PATH + FILE_ID + 'entities_multilabel'
FILTERED_DATASET_PATH = '/home/vmanuel/Notebooks/pytorch/source_files/vectors/' + FILE_ID + '/fair/'
# FILTERED_DATASET_PATH = '/home/vmanuel/Notebooks/pytorch/source_files/vectors/' + FILE_ID + '/_' + str(exclude_min_threshold) + '/'
X_TRAIN_PATH = FILTERED_DATASET_PATH + 'filtered_X_train'
X_VAL_PATH = FILTERED_DATASET_PATH + 'filtered_X_val'
X_TEST_PATH = FILTERED_DATASET_PATH + 'filtered_X_test'
Y_TRAIN_PATH = FILTERED_DATASET_PATH + 'filtered_Y_train'
Y_VAL_PATH = FILTERED_DATASET_PATH + 'filtered_Y_val'
Y_TEST_PATH = FILTERED_DATASET_PATH + 'filtered_Y_test'
E_TRAIN_PATH = FILTERED_DATASET_PATH + 'filtered_entities_train'
E_VAL_PATH = FILTERED_DATASET_PATH + 'filtered_entities_val'
E_TEST_PATH = FILTERED_DATASET_PATH + 'filtered_entities_test'
# %%
if __name__ == "__main__":
# %%
if torch.cuda.is_available():
device = torch.device("cuda")
torch.set_default_tensor_type(torch.DoubleTensor)
datasetManager = DatasetManager(FILE_ID)
datasetManager.set_device(device)
datasetManager.load_concept_embeddings(CONCEPT_EMBEDDING_PATHS = CONCEPT_EMBEDDING_PATHS, nickel = nickel)
if not load_dataset and filter_dataset:
# create dataset
datasetManager.load_entities_data(X_PATH = X_PATH,
Y_PATH = Y_PATH,
ENTITIES_PATH = ENTITIES_PATH)
datasetManager.find_and_filter_not_present()
if normalize:
datasetManager.normalize()
if filter_dataset:
filter = Filter()
datasetManager.setup_filter(filter_name = filter.FILTERS['ClassCohesion'],
selfXY = True,
log_file_path = '../source_files/logs/19_3filter_log3',
filtered_dataset_path = SOURCE_FILES_PATH + 'vectors/{}filtered/'.format(FILE_ID),
threshold=threshold,
cluster_distance = distance)
# datasetManager.filter()
print('... saving dataset ...')
datasetManager.save_raw_dataset(save_path = FILTERED_DATASET_PATH + '0.6_3_aprile/')
fraction = 0.1
datasetManager.shuffle_dataset_and_sample(fraction = fraction, in_place = True)
datasetManager.split_data_by_unique_entities(exclude_min_threshold=exclude_min_threshold)
print('Train: {} vectors, Val: {} vectors, Test: {} vectors'.format(len(datasetManager.Y_train),
len(datasetManager.Y_val),
len(datasetManager.Y_test)
)
)
datasetManager.save_datasets(save_path = FILTERED_DATASET_PATH)
elif load_dataset and filter_dataset:
print('... loading filtered datasets ...')
datasetManager.load_raw_dataset(FILTERED_DATASET_PATH + '0.6_3_aprile/')
fraction = 1
datasetManager.shuffle_dataset_and_sample(fraction = fraction, in_place = True)
datasetManager.split_data_by_unique_entities(exclude_min_threshold=exclude_min_threshold)
print('Train: {} vectors, Val: {} vectors, Test: {} vectors'.format(len(datasetManager.Y_train),
len(datasetManager.Y_val),
len(datasetManager.Y_test)
)
)
elif load_dataset:
print('... loading datasets ...')
t = time.time()
datasetManager.X_train = load_data_with_pickle(X_TRAIN_PATH)
datasetManager.X_test = load_data_with_pickle(X_TEST_PATH)
datasetManager.X_val = load_data_with_pickle(X_VAL_PATH)
datasetManager.Y_train = load_data_with_pickle(Y_TRAIN_PATH)
datasetManager.Y_test = load_data_with_pickle(Y_TEST_PATH)
datasetManager.Y_val = load_data_with_pickle(Y_VAL_PATH)
datasetManager.E_train = load_data_with_pickle(E_TRAIN_PATH)
datasetManager.E_test = load_data_with_pickle(E_TEST_PATH)
datasetManager.E_val = load_data_with_pickle(E_VAL_PATH)
print('--- dataset loaded in {:.2f} seconds ---'.format(time.time() - t))
# datasetManager.plot_datasets()
# datasetManager.print_statistic_on_dataset()
print('... creating numeric dataset ...')
datasetManager.create_numeric_dataset()
print('... creating aligned dataset ...')
datasetManager.create_aligned_dataset()
print('... creating dataloaders ...')
datasetManager.create_dataloaders()
out_spec = [{'manifold':'euclid', 'dim':[64, len(datasetManager.aligned_y_train['distributional'][0])]},
{'manifold':'poincare', 'dim':[128, 128, len(datasetManager.aligned_y_train['hyperbolic'][0])]}]
model = MTNCI(input_d=len(datasetManager.X_train[0]),
out_spec = out_spec,
dims = [512, 512])
model.set_results_paths(results_path = results_path, TSV_path = TSV_path)
model.set_checkpoint_path(checkpoint_path = '../source_files/checkpoints/{}'.format(tensorboard_run_ID))
model.set_dataset_manager(datasetManager)
model.initialize_tensorboard_manager(tensorboard_run_ID)
model.set_device(device)
model.set_optimizer(optimizer = RiemannianAdam(model.parameters(), lr = lr))
model.set_lambda(llambdas = {'hyperbolic' : 1 - llambda,
'distributional': llambda})
model.set_hyperparameters(epochs = epochs, weighted=weighted, regularized = regularized)
if regularized:
model.set_regularization_params(regul_dict)
print('... training model ... ')
model.train_model()
topn = [1]
model.type_prediction_on_test(topn=topn)