EmbeddingLookupLayer

class ampligraph.latent_features.layers.encoding.EmbeddingLookupLayer(*args, **kwargs)

Attributes

max_ent_size

Returns the size of the entity embedding matrix.

max_rel_size

Returns the size of relation embedding matrix.

Methods

__init__(k[, max_ent_size, max_rel_size, ...])

Initializes the embeddings of the model.

build(input_shape)

Builds the embedding lookup error.

call(sample[, type_of])

Looks up the embeddings of entities and relations of the triples.

compute_output_shape(input_shape)

Returns the output shape of outputs of call function.

get_config()

Returns the config of the layer.

partition_change_updates(partition_ent_emb, ...)

Perform the changes that are required when the partition is changed during training.

set_ent_rel_initial_value(ent_init, rel_init)

Sets the initial value of entity and relation embedding matrix.

set_initializer(initializer)

Set the initializer of the weights of this layer.

set_regularizer(regularizer)

Set the regularizer of the weights of this layer.

__init__(k, max_ent_size=None, max_rel_size=None, entity_kernel_initializer='glorot_uniform', entity_kernel_regularizer=None, relation_kernel_initializer='glorot_uniform', relation_kernel_regularizer=None, **kwargs)

Initializes the embeddings of the model.

Parameters
  • k (int) – Embedding size.

  • max_ent_size (int) – Max entities that can occur in any partition (default: None).

  • max_rel_size (int) – Max relations that can occur in any partition (default: None).

  • entity_kernel_initializer (str (name of objective function), objective function or) –

  • instance (tf.keras.initializers.Initializer) – An objective function is any callable with the signature init = fn(shape). Initializer of the entity embeddings.

  • entity_kernel_regularizer (str (name of objective function), objective function or) –

  • instance – An objective function is any callable with the signature init = fn(shape) Initializer of the relation embeddings.

  • relation_kernel_initializer (str or objective function or tf.keras.regularizers.Regularizer instance) – Regularizer of entity embeddings.

  • relation_kernel_regularizer (str or objective function or tf.keras.regularizers.Regularizer instance) – Regularizer of relations embeddings.

  • seed (int) – Random seed.

build(input_shape)

Builds the embedding lookup error.

The trainable weights are created based on the hyperparams.

call(sample, type_of='t')

Looks up the embeddings of entities and relations of the triples.

Parameters
  • sample (ndarray, shape (n, 3) or list) – Batch of input triples if a ndarray of shape (n,3) is given or a list (of lists) of entities or relations or distances as specified in type_of.

  • type_of (str) – Specifies whether we get in input triples or entities or relations. Possible values are “t” for triples, “e” for entities, “r” for relations and “d” for distances (useful in Nodepiece) (default: “t”).

Returns

emb_triples – List of embeddings of subjects, predicates, objects if type_of="t" or a list of embeddings of entities (relations/distance) if type_of="e" (type_of="r"/type_of="d").

Return type

list

compute_output_shape(input_shape)

Returns the output shape of outputs of call function.

Parameters

input_shape (list) – Shape of inputs of call function.

Returns

output_shape – Shape of outputs of call function.

Return type

list

get_config()

Returns the config of the layer.

A layer config is a Python dictionary (serializable) containing the configuration of a layer. The same layer can be reinstantiated later (without its trained weights) from this configuration.

The config of a layer does not include connectivity information, nor the layer class name. These are handled by Network (one layer of abstraction above).

Note that get_config() does not guarantee to return a fresh copy of dict every time it is called. The callers should make a copy of the returned dict if they want to modify it.

Returns

Python dictionary.

partition_change_updates(partition_ent_emb, partition_rel_emb)

Perform the changes that are required when the partition is changed during training.

Parameters
  • batch_ent_emb – Entity embeddings that need to be trained for the partition (all triples of the partition will have an embedding in this matrix).

  • batch_rel_emb – Relation embeddings that need to be trained for the partition (all triples of the partition will have an embedding in this matrix).

set_ent_rel_initial_value(ent_init, rel_init)

Sets the initial value of entity and relation embedding matrix.

This function is mainly used during the partitioned training where the full embedding matrix is initialized outside the model.

set_initializer(initializer)

Set the initializer of the weights of this layer.

Parameters

initializer (str (name of objective function) or objective function or tf.keras.initializers.Initializer or list) – Initializer of the entity and relation embeddings. This is either a single value or a list of size 2. If it is a single value, then both the entities and relations will be initialized based on the same initializer. If it is a list, the first initializer will be used for entities and the second for relations. Any callable with the signature init = fn(shape) can be interpreted as an objective function.

set_regularizer(regularizer)

Set the regularizer of the weights of this layer.

Parameters

regularizer (str (name of objective function) or objective function or tf.keras.regularizers.Regularizer instance or list) – Regularizer of the weights determining entity and relation embeddings. If it is a single value, then both the entities and relations will be regularized based on the same regularizer. If it is a list, the first regularizer will be used for entities and the second for relations.