PretrainedInitializer

class PretrainedInitializer(tensor: Tensor)[source]

Bases: object

Initialize tensor with pretrained weights.

Example usage:

import torch
from pykeen.pipeline import pipeline
from pykeen.nn.init import PretrainedInitializer

# this is usually loaded from somewhere else
# the shape must match, as well as the entity-to-id mapping
pretrained_embedding_tensor = torch.rand(14, 128)

result = pipeline(
    dataset="nations",
    model="transe",
    model_kwargs=dict(
        embedding_dim=pretrained_embedding_tensor.shape[-1],
        entity_initializer=PretrainedInitializer(tensor=pretrained_embedding_tensor),
    ),
)

Initialize the initializer.

Parameters:

tensor (Tensor) – the tensor of pretrained embeddings.

Methods Summary

__call__(x)

Initialize the tensor with the given tensor.

as_embedding(**kwargs)

Get a static embedding from this pre-trained initializer.

Methods Documentation

__call__(x: Tensor) Tensor[source]

Initialize the tensor with the given tensor.

Parameters:

x (Tensor)

Return type:

Tensor

as_embedding(**kwargs: Any)[source]

Get a static embedding from this pre-trained initializer.

Parameters:

kwargs (Any) – Keyword arguments to pass to pykeen.nn.representation.Embedding

Returns:

An embedding

Return type:

pykeen.nn.representation.Embedding