-
Notifications
You must be signed in to change notification settings - Fork 0
/
JOIE_db.py
124 lines (114 loc) · 3.46 KB
/
JOIE_db.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
import logging
from joie.loss import MarginLoss
from joie.models import CrossViewGrouping
from joie.models import TransE
from joie.trainer import JoieTrainer
from joie.utils import load_data, init_emb
log = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
# загрузчик датасетов
data = load_data(
instance_file='db_insnet_train.txt',
ontology_files=['db_ontonet_small_train.txt', 'db_HA_train.txt'],
instype_file='db_InsType_train.txt',
path_prefix='joie/data/db',
gen_corrupted_head=False,
create_test_files=True
)
# первичная инициализация эмбеддингов для фактов
instance_ent_emb, instance_rel_emb = init_emb(
data,
files=['db_insnet_train.txt'],
dim=300,
file_type='instance'
)
instype_ent_emb, instype_rel_emb = init_emb(
data,
files=['db_InsType_train.txt'],
dim=50,
file_type='instype'
)
# первичная инициализация эмбеддингов для онтологий
# ontology_ent_emb, ontology_rel_emb = init_emb(
# data,
# files=['db_ontonet_small_train.txt', 'db_HA_train.txt'],
# dim=50,
# file_type='ontology'
# )
# print(ontology_ent_emb[0])
ontology_ent_emb, ontology_rel_emb = init_emb(
data,
files=['db_ontonet_small_train.txt'],
dim=50,
file_type='ontology'
)
ha_ent_emb, ha_rel_emb = init_emb(
data,
files=['db_HA_train.txt'],
dim=50,
file_type='ontology'
)
###############################################################################
# INTRA-VIEW MODEL
###############################################################################
common_params = {
'p_norm': 1.0, # евклидова норма (1 или 2) для scoring functions
'margin': 0.5,
}
model_insnet = TransE(
file_name='db_insnet_train.txt',
nbatches=10,
entity_emb=instance_ent_emb,
rel_emb=instance_rel_emb,
loss=MarginLoss,
d1=200, d2=200,
**common_params
)
# внутренняя модель векторизации графа онтологий
model_ontonet = TransE(
file_name='db_ontonet_small_train.txt',
nbatches=10,
entity_emb=ontology_ent_emb,
rel_emb=ontology_rel_emb,
d1=200, d2=200,
loss=MarginLoss,
**common_params
)
# иерархическая модель графа онтологий
model_ha = CrossViewGrouping(
file_name='db_HA_train.txt',
nbatches=10,
entity_emb=ha_ent_emb,
concept_emb=ha_ent_emb,
d1=200, d2=200,
loss=MarginLoss,
**common_params
)
###############################################################################
# CROSS-VIEW MODEL
###############################################################################
model_cvt = CrossViewGrouping(
file_name='db_InsType_train.txt',
nbatches=10,
entity_emb=instype_ent_emb,
concept_emb=instype_ent_emb,
loss=MarginLoss,
d1=200, d2=200,
**common_params
)
##############################################################################
# UNITED JOIE MODEL
##############################################################################
JoieTrainer(
data=data,
# intra_view_models=[model_insnet],
intra_view_models=[model_insnet, model_ontonet, model_ha],
cross_view_model=model_cvt,
train_times=120,
save=True,
path_prefix='./joie/data/db',
).fit()