Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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={'norm': constants.DEFAULT_NORM_TRANSE,
'normalize_ent_emb': constants.DEFAULT_NORMALIZE_EMBEDDINGS,
'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},
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={},
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},
large_graphs=False,
verbose=constants.DEFAULT_VERBOSE):
"""Initialize an EmbeddingModel
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
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={'normalize_ent_emb': constants.DEFAULT_NORMALIZE_EMBEDDINGS,
'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):
if corrupt_side == 's+o' or corrupt_side == 'o':
obj_corruption_scores = scores_predict_o_corr_out.stack()
if corrupt_side == 's+o':
self.scores_predict = tf.concat([obj_corruption_scores, subj_corruption_scores], axis=0)
elif corrupt_side == 'o':
self.scores_predict = obj_corruption_scores
else:
self.scores_predict = subj_corruption_scores
else:
# Rather than generating corruptions in batches do it at once on the GPU for small or medium sized graphs
all_entities_np = np.arange(len(self.ent_to_idx))
corruption_entities = self.eval_config.get('corruption_entities', constants.DEFAULT_CORRUPTION_ENTITIES)
if corruption_entities == 'all':
corruption_entities = all_entities_np
elif isinstance(corruption_entities, np.ndarray):
corruption_entities = corruption_entities
else:
msg = 'Invalid type for corruption entities.'
logger.error(msg)
raise ValueError(msg)
# Entities that must be used while generating corruptions
self.corruption_entities_tf = tf.constant(corruption_entities, dtype=tf.int32)
corrupt_side = self.eval_config.get('corrupt_side', constants.DEFAULT_CORRUPT_SIDE_EVAL)
# Generate corruptions
self.out_corr = generate_corruptions_for_eval(self.X_test_tf,
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.
if self.early_stopping_criteria == 'hits10':
current_test_value = hits_at_n_score(ranks, 10)
elif self.early_stopping_criteria == 'hits3':
current_test_value = hits_at_n_score(ranks, 3)
elif self.early_stopping_criteria == 'hits1':
current_test_value = hits_at_n_score(ranks, 1)
elif self.early_stopping_criteria == 'mrr':
current_test_value = mrr_score(ranks)
if self.early_stopping_best_value is None: # First validation iteration
self.early_stopping_best_value = current_test_value
self.early_stopping_first_value = current_test_value
elif self.early_stopping_best_value >= current_test_value:
self.early_stopping_stop_counter += 1
if self.early_stopping_stop_counter == self.early_stopping_params.get(
'stop_interval', constants.DEFAULT_STOP_INTERVAL_EARLY_STOPPING):
# If the best value for the criteria has not changed from
# initial value then
# save the model before early stopping
if self.early_stopping_best_value == self.early_stopping_first_value:
self._save_trained_params()
if self.verbose:
msg = 'Early stopping at epoch:{}'.format(epoch)
logger.info(msg)
msg = 'Best {}: {:10f}'.format(
self.early_stopping_criteria,
self.early_stopping_best_value)
logger.info(msg)
self.early_stopping_epoch = epoch
self.eval_config['corruption_entities'] = self.early_stopping_params.get('corruption_entities',
constants.DEFAULT_CORRUPTION_ENTITIES)
if isinstance(self.eval_config['corruption_entities'], list):
# convert from list of raw triples to entity indices
logger.debug('Using the supplied entities for generation of corruptions for early stopping')
self.eval_config['corruption_entities'] = np.asarray([idx for uri, idx in self.ent_to_idx.items()
if uri in self.eval_config['corruption_entities']])
elif self.eval_config['corruption_entities'] == 'all':
logger.debug('Using all entities for generation of corruptions for early stopping')
elif self.eval_config['corruption_entities'] == 'batch':
logger.debug('Using batch entities for generation of corruptions for early stopping')
self.eval_config['corrupt_side'] = self.early_stopping_params.get('corrupt_side',
constants.DEFAULT_CORRUPT_SIDE_EVAL)
self.early_stopping_best_value = None
self.early_stopping_stop_counter = 0
self.early_stopping_epoch = None
try:
# If the filter has already been set in the dataset adapter then just pass x_filter = True
x_filter = self.early_stopping_params['x_filter']
if isinstance(x_filter, np.ndarray):
if x_filter.ndim <= 1 or (np.shape(x_filter)[1]) != 3:
msg = 'Invalid size for input x_valid. Expected (n,3): got {}'.format(np.shape(x_filter))
logger.error(msg)
raise ValueError(msg)
# set the filter triples in the data handler
x_filter = to_idx(x_filter, ent_to_idx=self.ent_to_idx, rel_to_idx=self.rel_to_idx)
self.eval_dataset_handle.set_filter(x_filter, mapped_status=True)
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={'num_filters': 32,
'filter_sizes': [1],
'dropout': 0.1},
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},
large_graphs=False,
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={'norm': constants.DEFAULT_NORM_TRANSE,
'normalize_ent_emb': constants.DEFAULT_NORMALIZE_EMBEDDINGS,
'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