-
Notifications
You must be signed in to change notification settings - Fork 251
/
ComplEx.py
300 lines (239 loc) · 14.4 KB
/
ComplEx.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
from .EmbeddingModel import EmbeddingModel, register_model
from ampligraph.latent_features import constants as constants
from ampligraph.latent_features.initializers import DEFAULT_XAVIER_IS_UNIFORM
import tensorflow as tf
@register_model("ComplEx", ["negative_corruption_entities"])
class ComplEx(EmbeddingModel):
r"""Complex embeddings (ComplEx)
The ComplEx model :cite:`trouillon2016complex` is an extension of
the :class:`ampligraph.latent_features.DistMult` bilinear diagonal model
. ComplEx scoring function is based on the trilinear Hermitian dot product in :math:`\mathcal{C}`:
.. math::
f_{ComplEx}=Re(\langle \mathbf{r}_p, \mathbf{e}_s, \overline{\mathbf{e}_o} \rangle)
Note that because embeddings are in :math:`\mathcal{C}`, ComplEx uses twice as many parameters as
:class:`ampligraph.latent_features.DistMult`.
Examples
--------
>>> import numpy as np
>>> from ampligraph.latent_features import ComplEx
>>>
>>> model = ComplEx(batches_count=2, seed=555, epochs=100, k=20, eta=5,
>>> loss='pairwise', loss_params={'margin':1},
>>> regularizer='LP', regularizer_params={'p': 2, 'lambda':0.1})
>>> X = np.array([['a', 'y', 'b'],
>>> ['b', 'y', 'a'],
>>> ['a', 'y', 'c'],
>>> ['c', 'y', 'a'],
>>> ['a', 'y', 'd'],
>>> ['c', 'y', 'd'],
>>> ['b', 'y', 'c'],
>>> ['f', 'y', 'e']])
>>> model.fit(X)
>>> model.predict(np.array([['f', 'y', 'e'], ['b', 'y', 'd']]))
[[0.019520484], [-0.14998421]]
>>> model.get_embeddings(['f','e'], embedding_type='entity')
array([[-0.33021057, 0.26524785, 0.0446662 , -0.07932718, -0.15453218,
-0.22342539, -0.03382565, 0.17444217, 0.03009969, -0.33569157,
0.3200497 , 0.03803705, 0.05536304, -0.00929996, 0.24446663,
0.34408194, 0.16192885, -0.15033236, -0.19703785, -0.00783876,
0.1495124 , -0.3578853 , -0.04975723, -0.03930473, 0.1663541 ,
-0.24731971, -0.141296 , 0.03150219, 0.15328223, -0.18549544,
-0.39240393, -0.10824018, 0.03394471, -0.11075485, 0.1367736 ,
0.10059565, -0.32808647, -0.00472086, 0.14231135, -0.13876757],
[-0.09483694, 0.3531292 , 0.04992269, -0.07774793, 0.1635035 ,
0.30610007, 0.3666711 , -0.13785957, -0.3143734 , -0.36909637,
-0.13792469, -0.07069954, -0.0368113 , -0.16743314, 0.4090072 ,
-0.03407392, 0.3113114 , -0.08418448, 0.21435146, 0.12006859,
0.08447982, -0.02025972, 0.38752195, 0.11451488, -0.0258422 ,
-0.10990044, -0.22661531, -0.00478273, -0.0238297 , -0.14207476,
0.11064807, 0.20135397, 0.22501846, -0.1731076 , -0.2770435 ,
0.30784574, -0.15043163, -0.11599299, 0.05718031, -0.1300622 ]],
dtype=float32)
"""
def __init__(self,
k=constants.DEFAULT_EMBEDDING_SIZE,
eta=constants.DEFAULT_ETA,
epochs=constants.DEFAULT_EPOCH,
batches_count=constants.DEFAULT_BATCH_COUNT,
seed=constants.DEFAULT_SEED,
embedding_model_params={'negative_corruption_entities': constants.DEFAULT_CORRUPTION_ENTITIES,
'corrupt_sides': constants.DEFAULT_CORRUPT_SIDE_TRAIN},
optimizer=constants.DEFAULT_OPTIM,
optimizer_params={'lr': constants.DEFAULT_LR},
loss=constants.DEFAULT_LOSS,
loss_params={},
regularizer=constants.DEFAULT_REGULARIZER,
regularizer_params={},
initializer=constants.DEFAULT_INITIALIZER,
initializer_params={'uniform': DEFAULT_XAVIER_IS_UNIFORM},
verbose=constants.DEFAULT_VERBOSE):
"""Initialize an EmbeddingModel
Also creates a new Tensorflow session for training.
Parameters
----------
k : int
Embedding space dimensionality
eta : int
The number of negatives that must be generated at runtime during training for each positive.
epochs : int
The iterations of the training loop.
batches_count : int
The number of batches in which the training set must be split during the training loop.
seed : int
The seed used by the internal random numbers generator.
embedding_model_params : dict
ComplEx-specific hyperparams:
- **'negative_corruption_entities'** - Entities to be used for generation of corruptions while training.
It can take the following values :
``all`` (default: all entities),
``batch`` (entities present in each batch),
list of entities
or an int (which indicates how many entities that should be used for corruption generation).
- **corrupt_sides** : Specifies how to generate corruptions for training.
Takes values `s`, `o`, `s+o` or any combination passed as a list
optimizer : string
The optimizer used to minimize the loss function. Choose between 'sgd',
'adagrad', 'adam', 'momentum'.
optimizer_params : dict
Arguments specific to the optimizer, passed as a dictionary.
Supported keys:
- **'lr'** (float): learning rate (used by all the optimizers). Default: 0.1.
- **'momentum'** (float): learning momentum (only used when ``optimizer=momentum``). Default: 0.9.
Example: ``optimizer_params={'lr': 0.01}``
loss : string
The type of loss function to use during training.
- ``pairwise`` the model will use pairwise margin-based loss function.
- ``nll`` the model will use negative loss likelihood.
- ``absolute_margin`` the model will use absolute margin likelihood.
- ``self_adversarial`` the model will use adversarial sampling loss function.
- ``multiclass_nll`` the model will use multiclass nll loss.
Switch to multiclass loss defined in :cite:`chen2015` by passing 'corrupt_sides'
as ['s','o'] to embedding_model_params.
To use loss defined in :cite:`kadlecBK17` pass 'corrupt_sides' as 'o' to embedding_model_params.
loss_params : dict
Dictionary of loss-specific hyperparameters. See :ref:`loss functions <loss>`
documentation for additional details.
Example: ``optimizer_params={'lr': 0.01}`` if ``loss='pairwise'``.
regularizer : string
The regularization strategy to use with the loss function.
- ``None``: the model will not use any regularizer (default)
- 'LP': the model will use L1, L2 or L3 based on the value of ``regularizer_params['p']`` (see below).
regularizer_params : dict
Dictionary of regularizer-specific hyperparameters. See the :ref:`regularizers <ref-reg>`
documentation for additional details.
Example: ``regularizer_params={'lambda': 1e-5, 'p': 2}`` if ``regularizer='LP'``.
initializer : string
The type of initializer to use.
- ``normal``: The embeddings will be initialized from a normal distribution
- ``uniform``: The embeddings will be initialized from a uniform distribution
- ``xavier``: The embeddings will be initialized using xavier strategy (default)
initializer_params : dict
Dictionary of initializer-specific hyperparameters. See the
:ref:`initializer <ref-init>`
documentation for additional details.
Example: ``initializer_params={'mean': 0, 'std': 0.001}`` if ``initializer='normal'``.
verbose : bool
Verbose mode.
"""
super().__init__(k=k, eta=eta, epochs=epochs, batches_count=batches_count, seed=seed,
embedding_model_params=embedding_model_params,
optimizer=optimizer, optimizer_params=optimizer_params,
loss=loss, loss_params=loss_params,
regularizer=regularizer, regularizer_params=regularizer_params,
initializer=initializer, initializer_params=initializer_params,
verbose=verbose)
self.internal_k = self.k * 2
def _initialize_parameters(self):
"""Initialize the complex embeddings.
"""
if not self.dealing_with_large_graphs:
self.ent_emb = tf.get_variable('ent_emb', shape=[len(self.ent_to_idx), self.internal_k],
initializer=self.initializer.get_tf_initializer(), dtype=tf.float32)
self.rel_emb = tf.get_variable('rel_emb', shape=[len(self.rel_to_idx), self.internal_k],
initializer=self.initializer.get_tf_initializer(), dtype=tf.float32)
else:
self.ent_emb = tf.get_variable('ent_emb', shape=[self.batch_size * 2, self.internal_k],
initializer=self.initializer.get_tf_initializer(), dtype=tf.float32)
self.rel_emb = tf.get_variable('rel_emb', shape=[self.batch_size * 2, self.internal_k],
initializer=self.initializer.get_tf_initializer(), dtype=tf.float32)
def _fn(self, e_s, e_p, e_o):
r"""ComplEx scoring function.
.. math::
f_{ComplEx}=Re(\langle \mathbf{r}_p, \mathbf{e}_s, \overline{\mathbf{e}_o} \rangle)
Additional details available in :cite:`trouillon2016complex` (Equation 9).
Parameters
----------
e_s : Tensor, shape [n]
The embeddings of a list of subjects.
e_p : Tensor, shape [n]
The embeddings of a list of predicates.
e_o : Tensor, shape [n]
The embeddings of a list of objects.
Returns
-------
score : TensorFlow operation
The operation corresponding to the ComplEx scoring function.
"""
# Assume each embedding is made of an img and real component.
# (These components are actually real numbers, see [trouillon2016complex].
e_s_real, e_s_img = tf.split(e_s, 2, axis=1)
e_p_real, e_p_img = tf.split(e_p, 2, axis=1)
e_o_real, e_o_img = tf.split(e_o, 2, axis=1)
# See Eq. 9 [trouillon2016complex):
return tf.reduce_sum(e_p_real * e_s_real * e_o_real, axis=1) + \
tf.reduce_sum(e_p_real * e_s_img * e_o_img, axis=1) + \
tf.reduce_sum(e_p_img * e_s_real * e_o_img, axis=1) - \
tf.reduce_sum(e_p_img * e_s_img * e_o_real, axis=1)
def fit(self, X, early_stopping=False, early_stopping_params={}):
"""Train a ComplEx model.
The model is trained on a training set X using the training protocol
described in :cite:`trouillon2016complex`.
Parameters
----------
X : ndarray, shape [n, 3]
The training triples
early_stopping: bool
Flag to enable early stopping (default:False).
If set to ``True``, the training loop adopts the following early stopping heuristic:
- The model will be trained regardless of early stopping for ``burn_in`` epochs.
- Every ``check_interval`` epochs the method will compute the metric specified in ``criteria``.
If such metric decreases for ``stop_interval`` checks, we stop training early.
Note the metric is computed on ``x_valid``. This is usually a validation set that you held out.
Also, because ``criteria`` is a ranking metric, it requires generating negatives.
Entities used to generate corruptions can be specified, as long as the side(s) of a triple to corrupt.
The method supports filtered metrics, by passing an array of positives to ``x_filter``. This will be used to
filter the negatives generated on the fly (i.e. the corruptions).
.. note::
Keep in mind the early stopping criteria may introduce a certain overhead
(caused by the metric computation).
The goal is to strike a good trade-off between such overhead and saving training epochs.
A common approach is to use MRR unfiltered: ::
early_stopping_params={x_valid=X['valid'], 'criteria': 'mrr'}
Note the size of validation set also contributes to such overhead.
In most cases a smaller validation set would be enough.
early_stopping_params: dictionary
Dictionary of hyperparameters for the early stopping heuristics.
The following string keys are supported:
- **'x_valid'**: ndarray, shape [n, 3] : Validation set to be used for early stopping.
- **'criteria'**: string : criteria for early stopping 'hits10', 'hits3', 'hits1' or 'mrr'(default).
- **'x_filter'**: ndarray, shape [n, 3] : Positive triples to use as filter if a 'filtered'
early stopping criteria is desired (i.e. filtered-MRR if 'criteria':'mrr').
Note this will affect training time (no filter by default).
- **'burn_in'**: int : Number of epochs to pass before kicking in early stopping (default: 100).
- **check_interval'**: int : Early stopping interval after burn-in (default:10).
- **'stop_interval'**: int : Stop if criteria is performing worse over n consecutive checks (default: 3)
- **'corruption_entities'**: List of entities to be used for corruptions.
If 'all', it uses all entities (default: 'all')
- **'corrupt_side'**: Specifies which side to corrupt. 's', 'o', 's+o' (default)
Example: ``early_stopping_params={x_valid=X['valid'], 'criteria': 'mrr'}``
"""
super().fit(X, early_stopping, early_stopping_params)
def predict(self, X, from_idx=False):
__doc__ = super().predict.__doc__ # NOQA
return super().predict(X, from_idx=from_idx)
def calibrate(self, X_pos, X_neg=None, positive_base_rate=None, batches_count=100, epochs=50):
__doc__ = super().calibrate.__doc__ # NOQA
super().calibrate(X_pos, X_neg, positive_base_rate, batches_count, epochs)
def predict_proba(self, X):
__doc__ = super().calibrate.__doc__ # NOQA
return super().predict_proba(X)