ibmfl.model
¶
Base Class¶
-
class
ibmfl.model.fl_model.
FLModel
(model_type, model_spec, **kwargs)[source]¶ Base class for all FLModels. This includes supervised and unsupervised ones.
-
get_model_absolute_path
()[source]¶ Construct absolute path using MODEL_DIR env variable
- Parameters
filename (str) – Name of file
- Returns
absolute_path; constructed absolute path using model_dir
- Return type
str
-
Decision Tree¶
-
class
ibmfl.model.dt_fl_model.
DTFLModel
(model_name, model_spec, dt_model=None, latest_counts=None)[source] Class implementation for basic decision tree
-
__init__
(model_name, model_spec, dt_model=None, latest_counts=None, **kwargs)[source]¶ Create a 'DTFLModel' instance for decision tree model. For the given model_spec, it will extract the data information required to grow the tree including: list_of_features, feature_values, list_of_labels. If an initial tree structure is given, model_spec is optional.
- Parameters
model_name (str) – String specifying the name of the model
model_spec (dict) – Specification of the tree model
dt_model (dict) – A tree structure
latest_counts (list) – A list of last_counts saved from last query call, served as party model_update information
kwargs (dict) – A dictionary contains other parameter settings on to initialize a decision tree model.
- Returns
None
-
fit_model
(train_data, fit_params, **kwargs)[source]¶ Computes the count according to split information and provided training data
- Parameters
train_data (pandas.core.frame.DataFrame) – Training dataset
fit_params (dict) – A dictionary containing the aggregator query information, e.g., ‘splits’, which is a list of node split candidates in the current tree; ‘list_of_labels’, which is a list of labels needs count information
kwargs (dict) – Training dataset
- Returns
None
-
update_model
(model_update=None, new_feature_values=None, new_list_of_features=None)[source]¶ Update the feature list and their corresponding range of feature values.
- Parameters
model_update (ModelUpdate) – Optional, an ModelUpdate object that contains information to update the DTFLModel.
new_feature_values (list) – Optional, new range of feature values
new_list_of_features (list) – Optional, new list of feature under consideration
- Returns
None
-
get_model_update
(self)[source]¶ Generate a ModelUpdate object that will be sent to the entities
- Returns
counts - a list of counts for the latest query
- Return type
ModelUpdate
-
predict
(self, x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
evaluate
(self, test_dataset, **kwargs)[source]¶ Evaluates the model given testing data
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, test) or a datagenerator of of type keras.utils.Sequence, keras.preprocessing.image.ImageDataGenerator
kwargs (dict) – Dictionary of metrics available for the model
- Returns
dict_metrics
- Return type
dict
-
evaluate_model
(self, x, y=None, class_report=False, **kwargs)[source]¶ Evaluate decision tree given x and y
- Parameters
x (pandas.core.frame.DataFrame) – Feature vectors
y (list) – Optional, corresponding labels of x, could be given in x as its last column
class_report (bool) – A boolean value indicating if classification report will be included in the evaluation results.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
dict_metrics
- Return type
dict
-
save_model
(filename=None, path=None)[source]¶ Save the tree to a file in the format specific to the framework requirements.
- Parameters
filename (str) – Name of the file to store the tree
path (str) – Path of the folder to store the tree. If no path is specified, the tree will be stored in the default path
- Returns
filename
- Return type
str
-
print_tree
(node=None, depth=0)[source]¶ Print the tree tht stores in the DTFLModel object.
- Parameters
node (dict) – A given tree node to start printing the tree
depth (int) – Tree depth
- Returns
print_string
- Return type
str
-
static
load_data_info_from_spec
(spec)[source]¶ Loads data information from provided model_spec, where model_spec is a dict that contains three items:
1) data information, which is a dictionary that contains list_of_features, feature_values, list_of_labels, all in ‘list’ type;
2) dt_model optional, a dictionary contains the tree structure
3) last_counts optional, a list served as a place to store
- Parameters
spec (dict) – Model specification contains required information, like list_of_features, feature_values, list_of_labels, to initialize a DTFLModel.
- Returns
(list_of_features, feature_values, list_of_labels) where list_of_features is a list of features in training data, feature_values is a list of value ranges for each feature in the feature list, and list_of_labels is a list of possible labels for training data
- Return type
tuple
-
Pytorch Wrapper¶
-
class
ibmfl.model.pytorch_fl_model.
PytorchFLModel
(model_name, model_spec, keras_model=None)[source]¶ Wrapper class for importing a Pytorch model.
-
__init__
(self, model_name, model_spec=None, pytorch_module=None, module_init_params=None, **kwargs)[source]¶ Create a `PytorchFLModel` instance from a Pytorch model. If pytorch_model is provided, it will use it; otherwise it will take the model_spec to create the model.
- Parameters
model_name (str) – String specifying the type of model e.g., Pytorch_NN
model_spec (dict) – A dictionary specifying path to saved nn.sequence container
pytorch_module (torch.nn.Module class reference) – uninstantiated pytorch model class
module_init_params (dict) – A dictionary with the values for the model's init arguments. The key for the init parameters must be prefixed with module__ (ex. if the parameter name is hidden_size, then key must be module__hidden_size)
kwargs (dict) – A dictionary contains other parameter settings on to initialize a PyTorch model.
- Returns
None
-
initialize_model
(self, pytorch_module, optimizer, criterion, module_init_params)[source]¶ Initializes a pytorch model via skorch library.
- Parameters
pytorch_module (torch.nn.Module) – uninstantiated pytorch model class
optimizer (pytorch optimizer class) – the optimizer to use
criterion (pytorch loss function class) – the loss function to use
module_init_params (dict) – A dictionary with the values for the model's init arguments. The key for the init parameters must be prefixed with module__ (ex. if the parameter name is hidden_size, then key must be module__hidden_size)
- Returns
an initialized skorch model
- Return type
skorch.NeuralNet
-
fit_model
(self, train_data, fit_params=None, **kwargs)[source]¶ Fits current model with provided training data.
- Parameters
model_update (ModelUpdate) – Training data, a tuple given in the form (x_train, y_train). Otherwise, input compatible with skorch.dataset.Dataset
- Returns
None
-
update_model
(self, model_update)[source]¶ Update model with provided model_update, where model_update should be generated according to `PytorchFLModel.get_model_update()`.
- Parameters
model_update (ModelUpdate) – `ModelUpdate` object that contains the weights that will be used to update the model.
- Returns
None
-
get_model_update
(self)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict
(self, x)[source]¶ Perform prediction for a batch of inputs. Note that for classification problems, it returns the resulting probabilities.
- Parameters
x (np.ndarray) – Samples with shape as expected by the model.
- Returns
Array predictions
- Return type
np.ndarray
-
predict_generator
(self, dataloader)[source]¶ Performs predictions using a pytorch dataloader.
- Parameters
x (torch.utils.data.DataLoader) – A pytorch dataloader with a dataset for predicting
- Returns
Array predictions
- Return type
np.ndarray
-
evaluate
(self, test_dataset, eval_metrics=None, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, y_test) or a pytorch DataLoader
eval_metrics (sklearn.metrics or function) – A list of sklearn.metric class, or a function for evaluating a pytorch dataloader batch
kwargs (dict) – Dictionary of metrics available for the model
- Returns
metric_dict - Dictionary of metrics
- Return type
dict
-
evaluate_model
(self, x, y, eval_metrics=None, **kwargs)[source]¶ Evaluates the model given x and y.
- Parameters
x (np.ndarray) – Samples with shape as expected by the model.
y (np.ndarray) – Corresponding labels to x.
eval_metrics (sklearn.metrics or function) – A list of sklearn.metric class or a functions with the signature (y_true, y_pred, **kwargs) => float.
- Returns
metric_dict - Dictionary of metrics
- Return type
dict
-
evaluate_generator_model
(self, dataloader, eval_metrics=None, **kwargs)[source]¶ Evaluates the model based on the provided dataloader.
- Parameters
dataloader (torch.utils.data.DataLoader) – A pytorch dataloader with test dataset and labels
eval_metrics (function) – a function for how to evaluate the batched examples and labels must have the signature (x_batch, y_batch) => float
- Returns
metric_dict - Dictionary of metrics
- Return type
dict
-
save_model
(self, filename=None, optimizer_filename=None, history_filename=None)[source]¶ Save a model to file in the format specific to the backend framework.
- Parameters
filename (str) – Name of the file that contains the model to be loaded.
optimizer_filename (str) – Name of the file that contains the optimizer to be loaded.
history_filename (str) – Name of the file that contains the model history to be loaded.
- Returns
None
-
load_model
(self, pytorch_module, model_filename, optimizer_filename=None, history_filename=None, optimizer=None, module_init_params=None)[source]¶ Loads a model from disk given the specified file_name.
- Parameters
pytorch_module (torch.nn.Module class reference) – Uninstantiated pytorch model class
model_filename (str) – Name of the file that contains the model to be loaded.
optimizer_filename (str) – Name of the file that contains the optimizer to be loaded.
history_filename (str) – Name of the file that contains the model history to be loaded.
optimizer (pytorch optimizer class) – The optimizer to be loaded.
module_init_params (dict) – A dictionary with the values for the model's init arguments. The key for the init parameters must be prefixed with module__ (ex. if the parameter name is hidden_size, then key must be module__hidden_size).
- Returns
None
-
get_weights
(self, to_numpy=False)[source]¶ Returns the weights of the model.
- Parameters
to_numpy (boolean) – Determines whether the weights should be returned as numpy array, or tensor.
- Returns
List of model weights
- Return type
list
-
parameters_to_numpy
(self, params)[source]¶ Transforms parameter tensors to numpy arrays.
- Parameters
params – The parameter tensor to be transformed.
- Returns
Numpy array of parameters
- Return type
nd.array
-
load_model_from_spec
(self, model_spec)[source]¶ Loads model from provided model_spec, where model_spec is a `dict` that contains one item: model_spec['model_definition'], which has a pointer to the file where an nn.sequence container is saved.
- Parameters
model_spec (dict) – A dictionary contains model specification
- Returns
model
- Return type
nn.sequence
-
get_gradient
(self, train_data)[source]¶ Returns the gradients for each layer in the model.
- Parameters
train_data (tuple) - Training data, a tuple given in the form (x_train, y_train). otherwise, input compatible with skorch.dataset.Dataset
- Returns
gradients
- Return type
list
-
static
valid_acc
(net, x, y)[source]¶ Returns the gradients for each layer in the model.
- Parameters
net (nn.sequence) - The model that will be used
x (nd.array) - the validation data set
y (nd.array) - the training targets
- Returns
Accuracy of the validation training pass
- Return type
float
-
static
loss
(y, y_pred, net)[source]¶ Returns resulting loss computed based on the provided dataset.
- Parameters
y (nd.array)
y_pred (nd.array)
net (nn.sequence)
- Returns
loss
-
static
__expand_linear_layer__
(net, layer_name, new_dimensions, layer_idx)[source]¶ Expands a linear layer of a pytorch module.
- Parameters
net (torch.nn.Module) - an instantiated pytorch module
layer_name (str) - layer variable name which needs to be expanded
new_dimensions (list) - new dimensions
layer_idx (int) - layer idx which needs to be expanded. this corresponds with the index of `new_dimensions`
- Returns
None
-
expand_model_by_layer_name
(self, new_dimension, layer_name="dense")[source]¶ Expands the current PyTorch models layers with the provided dimensions.
- Parameters
new_dimension (list) - new dimensions of the particular `layer_name`
layer_name (str) - layer variable name which needs to be expanded
- Returns
None
-
is_fitted
(self)[source]¶ Return a boolean value indicating if the model is fitted or not. In particular, it calls `skorch.utils.check_is_fitted` to checks whether the net is initialized. If it has, return True; otherwise return false.
- Parameters
None
- Returns
res
- Return type
bool
-
get_loss
(self, dataset)[source]¶ Return the resulting loss computed based on the provided dataset.
- Parameters
train_data (tuple) - Training data, a tuple given in the form (x_train, y_train). otherwise, input compatible with skorch.dataset.Dataset
- Returns
orig_loss_value - the resulting loss.
- Return type
float
-
TensorFlow Wrapper¶
-
class
ibmfl.model.tensorflow_fl_model.
TensorFlowFLModel
(model_name, model_spec, keras_model=None)[source]¶ Wrapper class for importing tensorflow models.
-
__init__
(self, model_name, model_spec, tf_model=None, **kwargs)[source]¶ Create a `PytorchFLModel` instance from a Pytorch model. If pytorch_model is provided, it will use it; otherwise it will take the model_spec to create the model.
- Parameters
model_name (str) – String specifying the type of model e.g., tf_CNN
model_spec (dict) – Specification of the `tf_model`
tf_model (tf.keras.Model) – Compiled TensorFlow model
kwargs (dict) – A dictionary contains other parameter settings on to initialize a TensorFlow model.
- Returns
None
-
fit_model
(self,train_data, fit_params=None, **kwargs)[source]¶ Fits current model with provided training data.
- Parameters
train_data (nd.ndarray) – Training data, a tuple given in the form (x_train, y_train).
fit_params (dict) – (optional) Dictionary with hyperparameters that will be used to call fit function. Hyperparameter parameters should match pytorch expected values e.g., `epochs`, which specifies the number of epochs to be run. If no `epochs` or `batch_size` are provided, a default value will be used (1 and 128, respectively).
- Returns
None
-
update_model
(self, model_update)[source]¶ Update TensorFlow model with provided model_update, where model_update should be generated according to `TensorFlowFLModel.get_model_update()`.
- Parameters
model_update (ModelUpdate) – `ModelUpdate` object that contains the weights that will be used to update the model.
- Returns
None
-
get_model_update
(self)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict
(self, x, **kwargs)[source]¶ Perform prediction for a batch of inputs. Note that for classification problems, it returns the resulting probabilities.
- Parameters
x (np.ndarray) – Samples with shape as expected by the model.
kwargs (dict) – Dictionary of tf-specific arguments.
- Returns
Array predictions
- Return type
np.ndarray
-
evaluate
(self, test_dataset, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, y_test) or a datagenerator of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model
- Returns
metrics
- Return type
dict
-
evaluate_model
(x, y, batch_size=128, **kwargs)[source]¶ Evaluates the model given x and y.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
batch_size (int) – Size of batches.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
Dictionary of metrics
- Return type
dict
-
evaluate_generator_model
(dataloader, eval_metrics=None)[source]¶ Evaluates the model based on the provided dataloader.
- Parameters
test_generator (`ImageDataGenerator` or `keras.utils.Sequence`) – Testing datagenerator of type `keras.utils.Sequence`, or `keras.preprocessing.image.ImageDataGenerator`.
kwargs (function) – Dictionary of metrics available for the model
- Returns
metrics
- Return type
dict
-
load_model
(file_name, custom_objects={})[source]¶ Loads a model from disk given the specified file_name.
- Parameters
file_name (str) – Name of the file that contains the model to be loaded.
custom_objects (dict)
- Returns
TensorFlow model loaded to memory
- Return type
tf.keras.models.Model
-
save_model
(filename=None)[source]¶ Save a model to file in the format specific to the backend framework.
- Parameters
filename (str) – Name of the file where to store the model.
- Returns
filename
- Return type
str
-
model_from_json_via_tf_keras
(json_file_name)[source]¶ Loads a model architecture from disk via tf.keras given the specified json file name.
- Parameters
json_file_name (str) Name of the file that contains the model architecture to be loaded.
- Returns
tf.keras model with only model architecture loaded to memory
- Return type
tf.keras.models.Model
-
load_model_from_spec
(model_spec)[source]¶ Loads model from provided model_spec, where model_spec is a `dict` that contains one item: model_spec['model_definition'], which has a pointer to the file where an nn.sequence container is saved.
- Parameters
model_spec
- Returns
model
- Return type
keras.models.Model
-
get_gradient
(train_data)[source]¶ Returns the gradients for each layer in the model.
- Parameters
train_data (nd.array)
- Returns
gradients
- Return type
list of tf.Tensor
-
expand_model_by_layer_name
(new_dimension, layer_name="dense")[source]¶ Expand the current Keras model with provided dimension of the hidden layers or model weights. This method by default expands the dense layer of the current neural network. It can be extends to expand other layers specified by `layer_name`, for example, it can be use to increase the number of CNN filters or increase the hidden layer size inside LSTM.
- Parameters
new_dimension (list) - an instantiated pytorch module
layer_name (str) - layer variable name which needs to be expanded
- Returns
None
-
is_fitted
()[source]¶ Return a boolean value indicating if the model is fitted or not. In particular, check if the tensorflow model has weights. If it has, return True; otherwise return false.
- Parameters
None
- Returns
res
- Return type
bool
-
get_loss
(dataset)[source]¶ Return the resulting loss computed based on the provided dataset.
- Parameters
train_data ((`nd.array`, nd.array`)) - Training data, a tuple given in the form (x_train, y_train). otherwise, input compatible with skorch.dataset.Dataset
- Returns
The resulting loss.
- Return type
float
-
Keras Wrapper¶
-
class
ibmfl.model.keras_fl_model.
KerasFLModel
(model_name, model_spec, keras_model=None)[source]¶ Wrapper class for importing keras models.
-
fit_model
(filename)[source]¶ Fits current model with provided training data.
- Parameters
train_data (category) – Training data, a tuple given in the form (x_train, y_train) or a datagenerator of type keras.utils.Sequence, keras.preprocessing.image.ImageDataGenerator
train_data (nd.ndarray) – Training data, a tuple given in the form (x_train, y_train) or a datagenerator of type keras.utils.Sequence, keras.preprocessing.image.ImageDataGenerator
fit_params (dict) – (optional) Dictionary with hyperparameters that will be used to call Keras fit function. Hyperparameter parameters should match keras expected values e.g., `epochs`, which specifies the number of epochs to be run. If no `epochs` or `batch_size` are provided, a default value will be used (1 and 128, respectively).
- Returns
None
-
fit
(x_test, node=None)[source]¶ Fits current model using model.fit with provided training data.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
predict
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
update_model
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
get_model_update
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
evaluate
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
evaluate_model
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
load_model
(x_test, node=None)[source]¶ Perform prediction for a data instance.
- Parameters
x_test (category) – A data instance in the format as expected by the model
node (dict) – A tree root to start searching for leaf node
- Returns
A list of predictions
- Return type
list
-
Differentially Private Gaussian Naive Bayes Wrapper¶
-
class
ibmfl.model.naive_bayes_fl_model.
NaiveBayesFLModel
(config)[source]¶ -
fit_model
(filename)[source]¶ Fits current model with provided training data.
- Parameters
train_data (nd.ndarray) – Training data, a tuple given in the form (x_train, y_train). Otherwise, input compatible with skorch.dataset.Dataset
fit_params (dict) – (optional) Dictionary with hyperparameters that will be used to call fit function. Hyperparameter parameters should match pytorch expected values e.g., `epochs`, which specifies the number of epochs to be run. If no `epochs` or `batch_size` are provided, a default value will be used (1 and 128, respectively).
- Returns
None
-
update_model
(filename)[source]¶ Update model with provided model_update, where model_update should be generated according to `TensorFlowFLModel.get_model_update()`.
- Parameters
train_data (nd.ndarray) – Training data, a tuple given in the form (x_train, y_train). Otherwise, input compatible with skorch.dataset.Dataset
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
evaluate
(x_test, node=None)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, y_test) or a pytorch DataLoader
eval_metrics (sklearn.metrics or function) – A list of sklearn.metric class, or a function for evaluating a pytorch dataloader batch
kwargs (dict) –Dictionary of metrics available for the model
-
evaluate_model
(x, y, eval_metrics=None)[source]¶ Evaluates the model given x and y.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
eval_metrics (sklearn.metrics or function) – A list of sklearn.metric class or a functions with the signature (y_true, y_pred, **kwargs) => float.
- Returns
Dictionary of metrics
- Return type
dict
RL Import Wrapper¶
-
class
ibmfl.model.rllib_fl_model.
RLlibFLModel
(model_name, model_spec, keras_model=None)[source]¶ Wrapper class for importing keras models.
-
fit_model
(train_data, fit_params)[source]¶ Fits current model with provided training data.
- Parameters
train_data (category) – Samples with shape as expected by the model.
fit_params (dict) – (Optional) Dictionary with hyperparameters that will be used to train the model.
- Returns
model
- Return type
nn.sequence
-
update_model
(model_update, **kwargs)[source]¶ Update RLlib model with provided model_update, where model_update should be generated according to `RLlibFLModel.get_model_update()`.
- Parameters
model_update (category) – `ModelUpdate` object that contains the weights that will be used to update the model
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
evaluate
(test_dataset, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (category) – esting data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model.
- Returns
model
- Return type
nn.sequence
-
evaluate_model
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the RLlib model with test environment and latest checkpoint using RLlib rollout.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
batch_size (int) – Size of batches.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
Mean Episode Reward
- Return type
dict
-
save_model
(filename=None, path=None)[source]¶ Save a model to file in the ray results folder.
- Parameters
filename (str) – Name of the file where to store the model.
path (str) – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library `DATA_PATH`.
- Returns
None
-
load_model
(file_name)[source]¶ Loads a model from disk given the specified file_name.
- Parameters
file_name (str) – Name of the file that contains the model to be loaded.
- Returns
None
-
create_rl_trainer
(env_class_ref)[source]¶ Creates RL trainer using model_spec where model_spec['model_definition'] specifies RL algorithm and env_class_ref contains environment definition.
- Parameters
env_class_ref (category) – Samples with shape as expected by the model.
train_data (nd.array) – Corresponding labels to x.
test_data (nd.array) – A list of sklearn.metric class or a functions with the signature (y_true, y_pred, **kwargs) => float.
- Returns
None
-
Sklearn Wrapper¶
-
class
ibmfl.model.sklearn_fl_model.
SklearnFLModel
(connection, synch=False, max_timeout=None)[source]¶ Wrapper class for sklearn models.
-
__init__
(connection, synch=False, max_timeout=None)[source]¶ Create a SklearnFLModel instance from a sklearn model. If sklearn_model is provided, it will use it; otherwise it will take the model_spec to create the model.
- Parameters
model_name (str) – A name specifying the type of model, e.g., linear_SVM
model_spec (dict) – A dictionary contains model specification
sklearn_model (sklearn) – A compiled sklearn model
kwargs (dict) – tA dictionary contains other parameter settings on to initialize a sklearn model.
-
fit_model
(party)[source]¶ Fits current model with provided training data.
- Parameters
party (PartyConnection) – party details that are needed by the protocol handler
- Return id
guide assigned to the party
-
update_model
(model_update, **kwargs)[source]¶ Update sklearn model with provided model_update.
- Parameters
model_update (category) – `ModelUpdate` object that contains the weights that will be used to update the model
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict
(x, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (category) – esting data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model.
- Returns
model
- Return type
nn.sequence
-
evaluate_model
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given test data x and the corresponding labels y.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
batch_size (int) – Size of batches.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
Mean Episode Reward
- Return type
dict
-
save_model
(filename=None, path=None)[source]¶ Save a model to file in the ray results folder.
- Parameters
filename (str) – Name of the file where to store the model.
path (str) – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library `DATA_PATH`.
- Returns
None
-
Sklearn K Means Wrapper¶
-
class
ibmfl.model.sklearn_kmeans_fl_model.
SklearnKMeansFLModel
(connection, synch=False, max_timeout=None)[source]¶ Wrapper class for sklearn.cluster.KMeans
-
__init__
(connection, synch=False, max_timeout=None)[source]¶ Create a SklearnKMeansFLModel instance from a sklearn model. If sklearn_model is provided, it will use it; otherwise it will take the model_spec to create the model.
- Parameters
model_name (str) – A name specifying the type of model, e.g., linear_SVM
model_spec (dict) – A dictionary contains model specification
sklearn_model (sklearn) – A compiled sklearn model
kwargs (dict) – tA dictionary contains other parameter settings on to initialize a sklearn model.
-
fit_model
(party)[source]¶ Fits current model with provided training data.
- Parameters
party (PartyConnection) – party details that are needed by the protocol handler
- Return id
guid assigned to the party
-
update_model
(model_update, **kwargs)[source]¶ Update sklearn model with provided model_update.
- Parameters
model_update (category) – `ModelUpdate` object that contains the weights that will be used to update the model
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict
(x, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (category) – testing data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model.
- Returns
model
- Return type
nn.sequence
-
evaluate
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Return type
dict
-
evaluate_model
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given test data x and the corresponding labels y.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
batch_size (int) – Size of batches.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Dictionary with all evaluation metrics provided by specific implementation- Return type
dict
-
save_model
(filename=None, path=None)[source]¶ Save a model to file in the ray results folder.
- Parameters
filename (str) – Name of the file where to store the model.
path (str) – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library `DATA_PATH`.
- Returns
None
-
load_model_from_spec
(file_name)[source]¶ Loads model from provided model_spec, where model_spec is a `dict` that contains the following items: model_spec['model_definition'] contains the model definition as type sklearn.cluster.KMeans
- Parameters
model_spec (dict) – Model specification contains a compiled sklearn model.
- Returns
model
- Return type
sklearn.cluster
-
SGD Classifier Wrapper¶
-
class
ibmfl.model.sklearn_SGD_linear_fl_model.
SklearnSGDFLModel
(config)[source]¶ -
__init__
(connection, synch=False, max_timeout=None)[source]¶ Create a SklearnSGDFLModel instance from a sklearn model. If sklearn_model is provided, it will use it; otherwise it will take the model_spec to create the model.
- Parameters
model_name (str) – A name specifying the type of model, e.g., linear_SVM
model_spec (dict) – A dictionary contains model specification
sklearn_model (sklearn) – A compiled sklearn model
kwargs (dict) – tA dictionary contains other parameter settings on to initialize a sklearn model.
-
fit_model
(party)[source]¶ Fits current model with provided training data.
- Parameters
party (PartyConnection) – party details that are needed by the protocol handler
- Return id
guid assigned to the party
-
update_model
(model_update, **kwargs)[source]¶ Update sklearn model with provided model_update.
- Parameters
model_update (category) – `ModelUpdate` object that contains the weights that will be used to update the model
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict_proba
(x, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (category) – testing data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model.
- Returns
model
- Return type
nn.sequence
-
evaluate
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Return type
dict
-
evaluate_model
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
batch_size (int) – Size of batches.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Dictionary with all evaluation metrics provided by specific implementation- Return type
dict
-
get_classes
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Returns an array of shape (n_classes,). If self.classes is not None, return self.classes, else obtains the array based on provided labels.
- Parameters
labels (np.ndarray) – Provided class labels to obtain the array of classes.
- Returns
An array of shape `(n_classes,)`
- Dictionary with all evaluation metrics provided by specific implementation- Return type
np.ndarray
-
save_model
(filename=None, path=None)[source]¶ Save a model to file in the ray results folder.
- Parameters
filename (str) – Name of the file where to store the model.
path (str) – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library `DATA_PATH`.
- Returns
None
-
load_model_from_spec
(file_name)[source]¶ Loads model from provided model_spec, where model_spec is a `dict` that contains the following items: model_spec['model_definition'] contains the model definition as type sklearn.cluster.KMeans
- Parameters
model_spec (dict) – Model specification contains a compiled sklearn model.
- Returns
model
- Return type
sklearn.cluster
Prejudice Remover¶
-
class
ibmfl.model.prej_remover_fl_model
PrejRemoverFLModel
(config)[source]¶ -
__init__
(connection, synch=False, max_timeout=None)[source]¶ Create a `SklearnPRFLModel` instance.
- Parameters
model_name (str) – A name specifying the type of model, e.g., linear_SVM
model_spec (dict) – A dictionary contains model specification
-
fit_model
(party)[source]¶ Learns the LRwPRType4 model.
- Parameters
train_data (np.ndarray) – training dataset
fit_params (dict) – party details that are needed by the protocol handler
- Returns
self
-
update_model
(model_update, **kwargs)[source]¶ Update model with provided model_update, where model_update should contains `coef_` and `intercept_`.
- Parameters
model_update (category) – `ModelUpdate` object that contains the weights that will be used to update the model
- Returns
None
-
get_model_update
(filename)[source]¶ Generates a `ModelUpdate` object that will be sent to other entities.
- Returns
ModelUpdate
- Return type
ModelUpdate
-
predict
(x, **kwargs)[source]¶ Perform prediction for the given input.
- Parameters
x (np.ndarray) – samples with shape as expected by the model
- Returns
array of predictions
- Return type
np.ndarray
-
predict_pr
(x, **kwargs)[source]¶ Predicts class labels for testing dataset via trained LRwPRType4 model.
- Parameters
test_data (category) – testing data, a tuple given in the form (x_test, test)
fit_params (dict) – Dictionary of metrics available for the model.
- Returns
labels
- Return type
np.ndarray
-
evaluate
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
test_dataset (np.ndarray) – Testing data, a tuple given in the form (x_test, test) or a datagenerator of of type `keras.utils.Sequence`, `keras.preprocessing.image.ImageDataGenerator`
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Return type
dict
-
evaluate_model
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Evaluates the model given testing data.
- Parameters
x (category) – Samples with shape as expected by the model.
y (dict) – Corresponding labels to x.
kwargs (dict) – Dictionary of metrics available for the model
- Returns
acc
- Dictionary with all evaluation metrics provided by specific implementation- Return type
dict
-
get_classes
(x=None, y=None, batch_size=128, **kwargs)[source]¶ Returns an array of shape (n_classes,). If self.classes is not None, return self.classes, else obtains the array based on provided labels.
- Parameters
labels (np.ndarray) – Provided class labels to obtain the array of classes.
- Returns
An array of shape `(n_classes,)`
- Dictionary with all evaluation metrics provided by specific implementation- Return type
np.ndarray
-
save_model
(filename=None, path=None)[source]¶ Save a sklearn model to file in the format specific to the framework requirement.
- Parameters
filename (str) – Name of the file where to store the model.
path (str) – Path of the folder where to store the model. If no path is specified, the model will be stored in the default data location of the library `DATA_PATH`.
- Returns
None