class documentation

Mixin for Torch models that can generate embeddings.

Parameters
modelthe Torch model, a torch:torch.nn.Module
layer_namethe name of the embeddings layer whose output to save, or None if this model instance should not expose embeddings. Prepend "<" to save the input tensor instead
as_feature_extractorwhether to operate the model as a feature extractor. If layer_name is provided, this layer is passed to torchvision's create_feature_extractor() function. If no layer_name is provided, the model's output is used as-is for feature extraction
Method __init__ Undocumented
Method embed Generates an embedding for the given data.
Method embed_all Generates embeddings for the given iterable of data.
Method get_embeddings Returns the embeddings generated by the last forward pass of the model.
Property has_embeddings Whether this instance has embeddings.
Method _predict_all Applies a forward pass to the given iterable of data and returns the raw model output with no processing applied.
Instance Variable _as_feature_extractor Undocumented
Instance Variable _embeddings_layer Undocumented
Instance Variable _model Undocumented
def __init__(self, model, layer_name=None, as_feature_extractor=False): (source)

Undocumented

def embed(self, arg): (source)

Generates an embedding for the given data.

Subclasses can override this method to increase efficiency, but, by default, this method simply calls predict and then returns get_embeddings.

Parameters
argthe data. See predict for details
Returns
a numpy array containing the embedding
def embed_all(self, args): (source)

Generates embeddings for the given iterable of data.

Subclasses can override this method to increase efficiency, but, by default, this method simply iterates over the data and applies embed to each.

Parameters
argsan iterable of data. See predict_all for details
Returns
a numpy array containing the embeddings stacked along axis 0
def get_embeddings(self): (source)

Returns the embeddings generated by the last forward pass of the model.

By convention, this method should always return an array whose first axis represents batch size (which will always be 1 when predict was last used).

Returns
a numpy array containing the embedding(s)
@property
has_embeddings = (source)

Whether this instance has embeddings.

def _predict_all(self, args): (source)

Applies a forward pass to the given iterable of data and returns the raw model output with no processing applied.

Parameters
argsan iterable of data. See predict_all for details
Returns
the raw output of the model
_as_feature_extractor = (source)

Undocumented

_embeddings_layer = (source)

Undocumented