xfer.GpRepurposer

class xfer.GpRepurposer(source_model: mxnet.module.module.Module, feature_layer_names, context_function=<function cpu>, num_devices=1, max_function_evaluations=100, apply_l2_norm=False)

Bases: xfer.meta_model_repurposer.MetaModelRepurposer

Repurpose source neural network to create a Gaussian Process (GP) meta-model through Transfer Learning.

Parameters:
  • source_model (mxnet.mod.Module) – Source neural network to do transfer learning from.
  • feature_layer_names (list[str]) – Name of layer(s) in source_model from which features should be transferred.
  • context_function (function(int)->:class:mx.context.Context) – MXNet context function that provides device type context.
  • num_devices (int) – Number of devices to use to extract features from source_model.
  • max_function_evaluations (int) – Maximum number of function evaluations to perform in GP optimization.
  • apply_l2_norm (bool) – Whether to apply L2 normalization after extracting features from source neural network. If set to True, L2 normalization will be applied to features before passing to GP during training and prediction.

Methods

__init__ Initialize self.
deserialize Uses dictionary to set attributes of repurposer.
get_features_from_source_model Extract feature outputs from feature_layer_names in source_model, merge and return all features and labels.
get_params Get parameters of repurposer that are in the constructor’s argument list.
predict_label Predict class labels on test data using the target_model (repurposed meta-model).
predict_probability Predict class probabilities on test data using the target_model (repurposed meta-model).
repurpose Train a meta-model using features extracted from training data through the source neural network.
save_repurposer Serialize the repurposed model (source_model, target_model and supporting info) and save it to given file_path.
serialize Serialize the GP repurposer (model and supporting info) and save to file.

Attributes

feature_layer_names Names of the layers to extract features from.
source_model Model to extract features from.
get_params()

Get parameters of repurposer that are in the constructor’s argument list.

Return type:dict
serialize(file_prefix)

Serialize the GP repurposer (model and supporting info) and save to file.

Parameters:file_prefix (str) – Prefix of file path to save the serialized repurposer to.
Returns:None
deserialize(input_dict)

Uses dictionary to set attributes of repurposer.

Parameters:input_dict (dict) – Dictionary containing values for attributes to be set to.
Returns:None
feature_layer_names

Names of the layers to extract features from.

get_features_from_source_model(data_iterator: mxnet.io.io.DataIter)

Extract feature outputs from feature_layer_names in source_model, merge and return all features and labels.

In addition, return mapping of feature_layer_name to indices in feature array.

Parameters:data_iterator (mxnet.io.DataIter) – Iterator for data to be passed through the source network and extract features.
Returns:features, feature_indices_per_layer and labels.
Return type:MetaModelData
predict_label(test_iterator: mxnet.io.io.DataIter)

Predict class labels on test data using the target_model (repurposed meta-model).

Parameters:test_iterator (mxnet.io.DataIter) – Test data iterator to return predictions for.
Returns:Predicted labels.
Return type:numpy.ndarray
predict_probability(test_iterator: mxnet.io.io.DataIter)

Predict class probabilities on test data using the target_model (repurposed meta-model).

Parameters:test_iterator (mxnet.io.DataIter) – Test data iterator to return predictions for.
Returns:Predicted probabilities.
Return type:numpy.ndarray
repurpose(train_iterator: mxnet.io.io.DataIter)

Train a meta-model using features extracted from training data through the source neural network.

Set self.target_model to the trained meta-model.

Parameters:train_iterator – Training data iterator to use to extract features from source_model.
save_repurposer(model_name, model_directory='', save_source_model=None)

Serialize the repurposed model (source_model, target_model and supporting info) and save it to given file_path.

Parameters:
  • model_name (str) – Name to save repurposer to.
  • model_directory (str) – File directory to save repurposer in.
  • save_source_model (boolean) – Flag to choose whether to save repurposer source model. Will use default if set to None. (MetaModelRepurposer default: True, NeuralNetworkRepurposer default: False)
source_model

Model to extract features from.