xfer.SvmRepurposer

class xfer.SvmRepurposer(source_model: mxnet.module.module.Module, feature_layer_names, context_function=<function cpu>, num_devices=1, c=1.0, kernel='linear', gamma='auto', enable_probability_estimates=False)

Bases: xfer.meta_model_repurposer.MetaModelRepurposer

Perform Transfer Learning through a Support Vector Machine (SVM) meta-model which repurposes the source neural network.

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.
  • c (float) – Penalty parameter C of the error term.
  • kernel (string) – Specifies the kernel type to be used in the SVM algorithm in sklearn library. It must be one of ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable.
  • gamma (float) – Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’. If gamma is ‘auto’ then 1/n_features will be used instead.
  • enable_probability_estimates (bool) – Whether to enable probability estimates. This must be enabled for predict_probability to work and will slow down training.

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.
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 Saves repurposer (excluding source model) to file_prefix.json.

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.

Return type:dict
serialize(file_prefix)

Saves repurposer (excluding source model) to file_prefix.json.

Parameters:file_prefix (str) – Prefix to save file with.
deserialize(input_dict)

Uses dictionary to set attributes of repurposer.

Parameters:input_dict (dict) – Dictionary containing values for attributes to be set to.
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.