Models

This package implements the knowledge embedding models. Model is the base model. We have implemented seven classic knowledge embedding models.

Base Model

class models.Model(config)[source]

The base model for knowledge embedding

Parameters:config – the instance of class config.Config.Config
get_all_instance()[source]

This method gets all samples from batch, including positive samples and negative samples

Returns:[self.batch_h, self.batch_t, self.batch_y], the shape is [batch_size * (1 + negative_ent + negative_rel)]
Return type:list
get_all_labels()[source]

This method gets all labels for the samples, 1 for positive triples and -1 for negative triples

Returns:self.batch_y, the shape is [batch_size * (1 + negative_ent + negative_rel)]
Return type:Variable
get_negtive_instance()[source]

This method gets the negative samples through negative sampling

Returns:[self.negtive_h, self.negtive_t,self.negitive_r], the shape is [batch_size]
Return type:list
get_postive_instance()[source]

This method gets the positive samples from batch

Returns:[self.postive_h, self.postive_t,self.positive_r], the shape is [batch_size]
Return type:list

TransE

class models.TransE(config)[source]

THis class implements TransE model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(p_score, n_score)[source]

This method calculatest the pairwise margin-based loss

Parameters:
  • p_score (float) – the score for positive triple
  • n_score (float) – the score for negative triple
Returns:

the margin-based loss for this pair

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

TransH

class models.TransH(config)[source]

THis class implements TransH model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(p_score, n_score)[source]

This method calculatest the pairwise margin-based loss

Parameters:
  • p_score (float) – the score for positive triple
  • n_score (float) – the score for negative triple
Returns:

the margin-based loss for this pair

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

TransR

class models.TransR(config)[source]

THis class implements Transr model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(p_score, n_score)[source]

This method calculatest the pairwise margin-based loss

Parameters:
  • p_score (float) – the score for positive triple
  • n_score (float) – the score for negative triple
Returns:

the margin-based loss for this pair

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

TransD

class models.TransD(config)[source]

THis class implements TransD model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(p_score, n_score)[source]

This method calculatest the pairwise margin-based loss

Parameters:
  • p_score (float) – the score for positive triple
  • n_score (float) – the score for negative triple
Returns:

the margin-based loss for this pair

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

RESCAL

class models.RESCAL(config)[source]

THis class implements RESCAL model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(p_score, n_score)[source]

This method calculatest the pairwise margin-based loss

Parameters:
  • p_score (float) – the score for positive triple
  • n_score (float) – the score for negative triple
Returns:

the margin-based loss for this pair

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

DistMult

class models.DistMult(config)[source]

THis class implements DistMult model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(loss, regul)[source]

This method calculatest the regularized loss

Parameters:
  • loss (float) – the loss without regularization
  • regul (float) – the regularization for loss
Returns:

the regularized loss

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float

ComplEx

class models.ComplEx(config)[source]

THis class implements ComplEx model based on models.Model

Parameters:config – configuration information
Shape:
  • Output: loss value which is a scalar
init_weights()[source]

Init entity and relation embeddings. In this model, we use xaview_uniform

loss_func(loss, regul)[source]

This method calculatest the regularized loss

Parameters:
  • loss (float) – the loss without regularization
  • regul (float) – the regularization for loss
Returns:

the regularized loss

Return type:

float

predict(predict_h, predict_t, predict_r)[source]

This method predictss the score for testting sample

Parameters:
  • predict_h – head entity of triple
  • predict_t – tail entity of triple
  • predict_r – relaiton of triple
Returns:

the score for testing triple

Return type:

float