xfer.LrRepurposer¶
-
class
xfer.
LrRepurposer
(source_model: mxnet.module.module.Module, feature_layer_names, context_function=<function cpu>, num_devices=1, tol=0.0001, c=1.0, n_jobs=-1)¶ Bases:
xfer.meta_model_repurposer.MetaModelRepurposer
Perform Transfer Learning through a Logistic Regression 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.
- tol (float) – Tolerance for stopping criteria.
- c (float) – Inverse of regularization strength; must be a positive float.
- n_jobs (int) – Number of CPU cores used during the cross-validation loop. If given a value of -1, all cores are used.
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. -
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.
- source_model (