secml.ml.classifiers

CClassifier

class secml.ml.classifiers.c_classifier.CClassifier(preprocess=None)[source]

Bases: secml.ml.c_module.CModule

Abstract class that defines basic methods for Classifiers.

A classifier assign a label (class) to new patterns using the informations learned from training set.

This interface implements a set of generic methods for training and classification that can be used for every algorithms. However, all of them can be reimplemented if specific routines are needed.

Parameters
preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

logger

Logger for current object.

n_classes

Number of classes of training dataset.

n_features

Number of features (before preprocessing).

preprocess

Inner preprocessor (if any).

verbose

Verbosity level of logger output.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property classes

Return the list of classes on which training has been performed.

decision_function(self, x, y=None)[source]

Computes the decision function for each pattern in x.

If a preprocess has been specified, input is normalized before computing the decision function.

Note

The actual decision function should be implemented inside _decision_function method.

Parameters
xCArray

Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).

yint or None, optional

The label of the class wrt the function should be calculated. If None, return the output for all classes.

Returns
scoreCArray

Value of the decision function for each test pattern. Dense flat array of shape (n_samples,) if y is not None, otherwise a (n_samples, n_classes) array.

estimate_parameters(self, dataset, parameters, splitter, metric, pick='first', perf_evaluator='xval', n_jobs=1)[source]

Estimate parameter that give better result respect a chose metric.

Parameters
datasetCDataset

Dataset to be used for evaluating parameters.

parametersdict

Dictionary with each item as {parameter: list of values to test}. Example: {‘C’: [1, 10, 100], ‘gamma’: list(10.0 ** CArray.arange(-4, 4))}

splitterCDataSplitter or str

Object to use for splitting the dataset into train and validation. A splitter type can be passed as string, in this case all default parameters will be used. For data splitters, num_folds is set to 3 by default. See CDataSplitter docs for more informations.

metricCMetric or str

Object with the metric to use while evaluating the performance. A metric type can be passed as string, in this case all default parameters will be used. See CMetric docs for more informations.

pick{‘first’, ‘last’, ‘random’}, optional

Defines which of the best parameters set pick. Usually, ‘first’ correspond to the smallest parameters while ‘last’ correspond to the biggest. The order is consistent to the parameters dict passed as input.

perf_evaluatorCPerfEvaluator or str, optional

Performance Evaluator to use. Default ‘xval’.

n_jobsint, optional

Number of parallel workers to use for performance evaluation. Default 1. Cannot be higher than processor’s number of cores.

Returns
best_parametersdict

Dictionary of best parameters found through performance evaluation.

fit(self, dataset, n_jobs=1)[source]

Trains the classifier.

If a preprocess has been specified, input is normalized before training.

For multiclass case see .CClassifierMulticlass.

Parameters
datasetCDataset

Training set. Must be a CDataset instance with patterns data and corresponding labels.

n_jobsint

Number of parallel workers to use for training the classifier. Default 1. Cannot be higher than processor’s number of cores.

Returns
trained_clsCClassifier

Instance of the classifier trained using input dataset.

grad_f_x(self, x, y)[source]

Computes the gradient of the classifier’s decision function wrt x.

Parameters
xCArray or None, optional

The input point. The gradient will be computed at x.

yint

Binary index of the class wrt the gradient must be computed.

Returns
gradientCArray

The gradient of the linear classifier’s decision function wrt decision function input. Vector-like array.

is_fitted(self)[source]

Return True if the classifier is trained (fitted).

Returns
bool

True or False depending on the result of the call to check_is_fitted.

property n_classes

Number of classes of training dataset.

property n_features

Number of features (before preprocessing).

predict(self, x, return_decision_function=False)[source]

Perform classification of each pattern in x.

If preprocess has been specified, input is normalized before classification.

Parameters
xCArray

Array with new patterns to classify, 2-Dimensional of shape (n_patterns, n_features).

return_decision_functionbool, optional

Whether to return the decision_function value along with predictions. Default False.

Returns
labelsCArray

Flat dense array of shape (n_patterns,) with the label assigned to each test pattern. The classification label is the label of the class associated with the highest score.

scoresCArray, optional

Array of shape (n_patterns, n_classes) with classification score of each test pattern with respect to each training class. Will be returned only if return_decision_function is True.

CClassifierLinear

class secml.ml.classifiers.c_classifier_linear.CClassifierLinear(preprocess=None)[source]

Bases: secml.ml.classifiers.c_classifier.CClassifier

Abstract class that defines basic methods for linear classifiers.

A linear classifier assign a label (class) to new patterns computing the inner product between the patterns and a vector of weights for each training set feature.

This interface implements a set of generic methods for training and classification that can be used for every linear model.

Parameters
preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
b

Bias calculated from training data.

class_type

Defines class type.

classes

Return the list of classes on which training has been performed.

logger

Logger for current object.

n_classes

Number of classes of training dataset.

n_features

Number of features (before preprocessing).

preprocess

Inner preprocessor (if any).

verbose

Verbosity level of logger output.

w

Vector with each feature’s weight (dense or sparse).

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the linear classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x[, y])

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property b

Bias calculated from training data.

fit(self, dataset, n_jobs=1)[source]

Trains the linear classifier.

If a preprocess has been specified, input is normalized before training.

Training on 2nd class is avoided to speed up classification.

Parameters
datasetCDataset

Binary (2-classes) training set. Must be a CDataset instance with patterns data and corresponding labels.

n_jobsint

Number of parallel workers to use for training the classifier. Default 1. Cannot be higher than processor’s number of cores.

Returns
trained_clsCClassifier

Instance of the classifier trained using input dataset.

grad_f_x(self, x, y=1)[source]

Computes the gradient of the classifier’s decision function wrt x.

Parameters
xCArray or None, optional

The input point. The gradient will be computed at x.

yint

Binary index of the class wrt the gradient must be computed. Default is y=1 to return gradient wrt the positive class.

Returns
gradientCArray

The gradient of the linear classifier’s decision function wrt decision function input. Vector-like array.

property w

Vector with each feature’s weight (dense or sparse).

CClassifierSkLearn

class secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn(sklearn_model, preprocess=None)[source]

Bases: secml.ml.classifiers.c_classifier.CClassifier

Generic wrapper for SkLearn classifiers.

Parameters
sklearn_modelsklearn.base.BaseEstimator object

The scikit-learn model to wrap. Must implement fit and either decision_function or predict_proba methods.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘sklearn-clf’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class and SkLearn model parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

get_params(self)[source]

Returns the dictionary of class and SkLearn model parameters.

A parameter is a PUBLIC or READ/WRITE attribute.

property sklearn_model

Wrapped SkLearn classifier.

CClassifierDecisionTree

class secml.ml.classifiers.sklearn.c_classifier_decision_tree.CClassifierDecisionTree(criterion='gini', splitter='best', max_depth=None, min_samples_split=2, random_state=None, preprocess=None)[source]

Bases: secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn

Decision Tree Classifier.

Parameters
criterionstr, optional

The function to measure the quality of a split. Supported criteria are ‘gini’ (default) for the Gini impurity and ‘entropy’ for the information gain.

splitterstr, optional

The strategy used to choose the split at each node. Supported strategies are ‘best’ (default) to choose the best split and ‘random’ to choose the best random split.

max_depthint or None, optional

The maximum depth of the tree. If None (default), then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.

min_samples_splitint or float, optional

The minimum number of samples required to split an internal node. If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split. Default 2.

random_stateint, RandomState or None, optional

The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Default None.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘dec-tree’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class and SkLearn model parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

CClassifierKNN

class secml.ml.classifiers.sklearn.c_classifier_knn.CClassifierKNN(n_neighbors=5, weights='uniform', algorithm='auto', leaf_size=30, p=2, metric='minkowski', metric_params=None, preprocess=None)[source]

Bases: secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn

K Neighbors Classifiers.

Parameters
n_neighborsint, optional

Number of neighbors to use by default for kneighbors queries. Default 5.

weightsstr or callable, optional

Weight function used in prediction. If ‘uniform’ (default), all points in each neighborhood are weighted equally; if ‘distance’ points are weighted by the inverse of their distance. Can also be an user-defined function which accepts an array of distances, and returns an array of the same shape containing the weights.

algorithm{‘auto’, ‘ball_tree’, ‘kd_tree’, ‘brute’}, optional

Algorithm used to compute the nearest neighbors. If ‘auto’ (default), the most appropriate algorithm is decided based on the values passed to fit method.

leaf_sizeint, optional

Leaf size passed to BallTree or KDTree. Default 30.

pint, optional

Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2 (default). For arbitrary p, minkowski_distance (l_p) is used.

metricstr or callable, optional

The distance metric to use for the tree. If ‘minkowski’ (default) and p = 2, it is equivalent to the standard Euclidean metric. If metric is ‘precomputed’, X is assumed to be a distance matrix and must be square during fit.

metric_paramsdict, optional

Additional keyword arguments for the metric function.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘knn’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class and SkLearn model parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

kneighbors(self, x[, num_samples])

Find the training samples nearest to x

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

kneighbors(self, x, num_samples=None)[source]

Find the training samples nearest to x

Parameters
xCArray

The query point or points.

num_samples: int or None

Number of neighbors to get. if None, use n_neighbors

Returns
distCArray

Array representing the lengths to points

index_point: CArray

Indices of the nearest points in the training set

tr_dataset.X: CArray

Training samples

property tr

Training set.

CClassifierLogistic

class secml.ml.classifiers.sklearn.c_classifier_logistic.CClassifierLogistic(C=1.0, max_iter=100, random_state=None, preprocess=None)[source]

Bases: secml.ml.classifiers.c_classifier_linear.CClassifierLinear, secml.ml.classifiers.gradients.mixin_classifier_gradient_logistic.CClassifierGradientLogisticMixin

Logistic Regression (aka logit, MaxEnt) classifier.

Parameters
Cfloat, optional

Penalty parameter C of the error term. Default 1.0.

max_iterint, optional

Maximum number of iterations taken for the solvers to converge. Default 100.

random_stateint, RandomState or None, optional

The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Default None.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘logistic’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the linear classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_params(self, x[, y])

Derivative of the decision function w.r.t.

grad_f_x(self, x[, y])

Computes the gradient of the classifier’s decision function wrt x.

grad_loss_params(self, x, y[, loss])

Derivative of the classifier loss w.r.t.

grad_tr_params(self, x, y)

Derivative of the classifier training objective w.r.t. the classifier

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

hessian_tr_params(self, x, y)

Hessian of the training objective w.r.t.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property C

Penalty parameter C of the error term.

property max_iter
property random_state

CClassifierNearestCentroid

class secml.ml.classifiers.sklearn.c_classifier_nearest_centroid.CClassifierNearestCentroid(metric='euclidean', shrink_threshold=None, preprocess=None)[source]

Bases: secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn

CClassifierNearestCentroid.

Parameters
metricstr or callable, optional

The metric to use when calculating distance between instances in a feature array. Default ‘euclidean’.

shrink_thresholdfloat, optional

Threshold for shrinking centroids to remove features.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘nrst-centroid’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class and SkLearn model parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property centroids
property metric

CClassifierRandomForest

class secml.ml.classifiers.sklearn.c_classifier_random_forest.CClassifierRandomForest(n_estimators=10, criterion='gini', max_depth=None, min_samples_split=2, random_state=None, preprocess=None)[source]

Bases: secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn

Random Forest classifier.

Parameters
n_estimatorsint, optional

The number of trees in the forest. Default 10.

criterionstr, optional

The function to measure the quality of a split. Supported criteria are ‘gini’ (default) for the Gini impurity and ‘entropy’ for the information gain.

max_depthint or None, optional

The maximum depth of the tree. If None (default), then nodes are expanded until all leaves are pure or until all leaves contain less than min_samples_split samples.

min_samples_splitint or float, optional

The minimum number of samples required to split an internal node. If int, then consider min_samples_split as the minimum number. If float, then min_samples_split is a fraction and ceil(min_samples_split * n_samples) are the minimum number of samples for each split. Default 2.

random_stateint, RandomState or None, optional

The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Default None.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘random-forest’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class and SkLearn model parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

CClassifierRidge

class secml.ml.classifiers.sklearn.c_classifier_ridge.CClassifierRidge(alpha=1.0, kernel=None, max_iter=100000.0, class_weight=None, tol=0.0001, fit_intercept=True, preprocess=None)[source]

Bases: secml.ml.classifiers.c_classifier_linear.CClassifierLinear, secml.ml.classifiers.gradients.mixin_classifier_gradient_ridge.CClassifierGradientRidgeMixin

Ridge Classifier.

Parameters
alphafloat, optional

Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization. Default 1.0.

kernelNone or CKernel subclass, optional

Deprecated since version 0.12.

Instance of a CKernel subclass to be used for computing similarity between patterns. If None (default), a linear SVM will be created. In the future this parameter will be removed from this classifier and kernels will have to be passed as preprocess.

max_iterint, optional

Maximum number of iterations for conjugate gradient solver. Default 1e5.

class_weight{dict, ‘balanced’, None}, optional

Set the parameter C of class i to class_weight[i] * C. If not given (default), all classes are supposed to have weight one. The ‘balanced’ mode uses the values of labels to automatically adjust weights inversely proportional to class frequencies as n_samples / (n_classes * np.bincount(y)).

tolfloat, optional

Precision of the solution. Default 1e-4.

fit_interceptbool, optional

If True (default), the intercept is calculated, else no intercept will be used in calculations (e.g. data is expected to be already centered).

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘ridge’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the linear classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_params(self, x[, y])

Derivative of the decision function w.r.t.

grad_f_x(self, x[, y])

Computes the gradient of the classifier’s decision function wrt x.

grad_loss_params(self, x, y[, loss])

Derivative of the classifier loss w.r.t.

grad_tr_params(self, x, y)

Derivative of the classifier training objective w.r.t. the classifier

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

hessian_tr_params(self, x[, y])

Hessian of the training objective w.r.t.

is_fitted(self)

Return True if the classifier is trained (fitted).

is_kernel_linear(self)

Return True if the kernel is None or linear.

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property C

Constant that multiplies the regularization term.

Equal to 1 / alpha.

property alpha

Returns the Constant that multiplies the regularization term.

property class_weight

Weight of each training class.

is_kernel_linear(self)[source]

Return True if the kernel is None or linear.

property kernel

Kernel function.

property n_tr_samples

Returns the number of training samples.

property tr

Training set.

CClassifierSGD

class secml.ml.classifiers.sklearn.c_classifier_sgd.CClassifierSGD(loss, regularizer, kernel=None, alpha=0.01, fit_intercept=True, max_iter=1000, tol=None, shuffle=True, learning_rate='optimal', eta0=10.0, power_t=0.5, class_weight=None, warm_start=False, average=False, random_state=None, preprocess=None)[source]

Bases: secml.ml.classifiers.c_classifier_linear.CClassifierLinear, secml.ml.classifiers.gradients.mixin_classifier_gradient_sgd.CClassifierGradientSGDMixin

Stochastic Gradient Descent Classifier.

Parameters
lossCLoss

Loss function to be used during classifier training.

regularizerCRegularizer

Regularizer function to be used during classifier training.

kernelNone or CKernel subclass, optional

Deprecated since version 0.12.

Instance of a CKernel subclass to be used for computing similarity between patterns. If None (default), a linear SVM will be created. In the future this parameter will be removed from this classifier and kernels will have to be passed as preprocess.

alphafloat, optional

Constant that multiplies the regularization term. Default 0.01. Also used to compute learning_rate when set to ‘optimal’.

fit_interceptbool, optional

If True (default), the intercept is calculated, else no intercept will be used in calculations (e.g. data is expected to be already centered).

max_iterint, optional

The maximum number of passes over the training data (aka epochs). Default 1000.

tolfloat or None, optional

The stopping criterion. If it is not None, the iterations will stop when (loss > best_loss - tol) for 5 consecutive epochs. Default None.

shufflebool, optional

If True (default) the training data is shuffled after each epoch.

learning_ratestr, optional

The learning rate schedule. If ‘constant’, eta = eta0; if ‘optimal’ (default), eta = 1.0 / (alpha * (t + t0)), where t0 is chosen by a heuristic proposed by Leon Bottou; if ‘invscaling’, eta = eta0 / pow(t, power_t); if ‘adaptive’, eta = eta0, as long as the training keeps decreasing.

eta0float, optional

The initial learning rate for the ‘constant’, ‘invscaling’ or ‘adaptive’ schedules. Default 10.0.

power_tfloat, optional

The exponent for inverse scaling learning rate. Default 0.5.

class_weight{dict, ‘balanced’, None}, optional

Set the parameter C of class i to class_weight[i] * C. If not given (default), all classes are supposed to have weight one. The ‘balanced’ mode uses the values of labels to automatically adjust weights inversely proportional to class frequencies as n_samples / (n_classes * np.bincount(y)).

warm_startbool, optional

If True, reuse the solution of the previous call to fit as initialization, otherwise, just erase the previous solution. Default False.

averagebool or int, optional

If True, computes the averaged SGD weights and stores the result in the coef_ attribute. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. Default False.

random_stateint, RandomState or None, optional

The seed of the pseudo random number generator to use when shuffling the data. If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Default None.

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

Attributes
class_type‘sgd’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the linear classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_params(self, x[, y])

Derivative of the decision function w.r.t.

grad_f_x(self, x[, y])

Computes the gradient of the classifier’s decision function wrt x.

grad_loss_params(self, x, y[, loss])

Derivative of the classifier loss w.r.t.

grad_tr_params(self, x, y)

Derivative of the classifier training objective function w.r.t.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

hessian_tr_params(self, x, y)

Hessian of the training objective w.r.t.

is_fitted(self)

Return True if the classifier is trained (fitted).

is_kernel_linear(self)

Return True if the kernel is None or linear.

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

property C

Constant that multiplies the regularization term.

Equal to 1 / alpha.

property alpha

Returns the Constant that multiplies the regularization term.

property average

When set to True, computes the averaged SGD weights. If set to an int greater than 1, averaging will begin once the total number of samples seen reaches average. So average=10 will begin averaging after seeing 10 samples.

property class_weight

Weight of each training class.

property eta0

The initial learning rate for the invscaling learning rate. Default is 10.0 (corresponding to sqrt(1.0/sqrt(alpha)), with alpha=0.0001).

is_kernel_linear(self)[source]

Return True if the kernel is None or linear.

property kernel

Kernel function.

property loss

Returns the loss function used by classifier.

property n_tr_samples

Returns the number of training samples.

property power_t

The exponent for inverse scaling learning rate.

property regularizer

Returns the regularizer function used by classifier.

property tr

Training set.

CClassifierSVM

class secml.ml.classifiers.sklearn.c_classifier_svm.CClassifierSVM(kernel=None, C=1.0, class_weight=None, preprocess=None, grad_sampling=1.0, store_dual_vars=None)[source]

Bases: secml.ml.classifiers.c_classifier_linear.CClassifierLinear, secml.ml.classifiers.gradients.mixin_classifier_gradient_svm.CClassifierGradientSVMMixin

Support Vector Machine (SVM) classifier.

Parameters
kernelNone or CKernel subclass, optional

Instance of a CKernel subclass to be used for computing similarity between patterns. If None (default), a linear SVM will be created.

Cfloat, optional

Penalty parameter C of the error term. Default 1.0.

class_weight{dict, ‘balanced’, None}, optional

Set the parameter C of class i to class_weight[i] * C. If not given (default), all classes are supposed to have weight one. The ‘balanced’ mode uses the values of labels to automatically adjust weights inversely proportional to class frequencies as n_samples / (n_classes * np.bincount(y)).

preprocessCPreProcess or str or None, optional

Features preprocess to be applied to input data. Can be a CPreProcess subclass or a string with the type of the desired preprocessor. If None, input data is used as is.

grad_samplingfloat

Percentage in (0.0, 1.0] of the alpha weights to be considered when computing the classifier gradient.

See also

CKernel

Pairwise kernels and metrics.

CClassifierLinear

Common interface for linear classifiers.

Notes

Current implementation relies on sklearn.svm.SVC for the training step.

Attributes
class_type‘svm’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Fit the SVM classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_params(self, x[, y])

Derivative of the decision function w.r.t.

grad_f_x(self, x[, y])

Computes the gradient of the classifier’s decision function wrt x.

grad_loss_params(self, x, y[, loss])

Derivative of the loss w.r.t.

grad_tr_params(self, x, y)

Derivative of the classifier training objective w.r.t.

gradient(self, x[, w])

Compute gradient at x by doing a backward pass.

hessian_tr_params(self[, x, y])

Hessian of the training objective w.r.t.

is_fitted(self)

Return True if the classifier is trained (fitted).

is_kernel_linear(self)

Return True if the kernel is None or linear.

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

sv_margin(self[, tol])

Margin Support Vectors.

sv_margin_idx(self[, tol])

Indices of Margin Support Vectors.

sv_margin_y(self[, tol])

Margin Support Vectors class (-1/+1).

timed([msg])

Timer decorator.

property C

Penalty parameter C of the error term.

property alpha

Signed coefficients of the SVs in the decision function.

property class_weight

Weight of each training class.

fit(self, dataset, n_jobs=1)[source]

Fit the SVM classifier.

We use sklearn.svm.SVC for weights and Support Vectors computation. The routine will set alpha, sv, sv_idx and b parameters. For linear SVM (i.e. if kernel is None) we also store the ‘w’ flat vector with each feature’s weight.

If a preprocess has been specified, input is normalized before computing the decision function.

Parameters
datasetCDataset

Binary (2-classes) Training set. Must be a CDataset instance with patterns data and corresponding labels.

n_jobsint, optional

Number of parallel workers to use for training the classifier. Default 1. Cannot be higher than processor’s number of cores.

Returns
trained_clsCClassifierSVM

Instance of the SVM classifier trained using input dataset.

property grad_sampling

Percentage of samples for approximate gradient.

is_kernel_linear(self)[source]

Return True if the kernel is None or linear.

property kernel

Kernel function (None if a linear classifier).

property n_sv

Return the number of support vectors.

In the 1st and in the 2nd column is stored the number of SVs for the negative and positive class respectively.

property store_dual_vars

Controls the store of dual space variables (SVs and alphas).

By default is None and dual variables are stored only if kernel is not None. If set to True, dual variables are stored even if kernel is None (linear SVM). If kernel is not None, cannot be set to False.

property sv

Support Vectors.

property sv_idx

Indices of Support Vectors within the training dataset.

sv_margin(self, tol=1e-06)[source]

Margin Support Vectors.

Parameters
tolfloat

Alpha value threshold for considering a Support Vector on the margin.

Returns
CArray or None

Margin support vector, 2D CArray. If no margin support vector are found, return None.

indicesCArray or None

Flat array with the indices of the margin support vectors. If no margin support vector are found, return None.

sv_margin_idx(self, tol=1e-06)[source]

Indices of Margin Support Vectors.

Parameters
tolfloat

Alpha value threshold for considering a Support Vector on the margin.

Returns
indicesCArray

Flat array with the indices of the Margin Support Vectors.

sv_margin_y(self, tol=1e-06)[source]

Margin Support Vectors class (-1/+1).

Parameters
tolfloat

Alpha value threshold for considering a Support Vector on the margin.

Returns
CArray

Flat CArray with the class (-1/+1) of the Margin Support Vectors.

CClassifierDNN

class secml.ml.classifiers.c_classifier_dnn.CClassifierDNN(model, input_shape=None, preprocess=None, pretrained=False, pretrained_classes=None, softmax_outputs=False)[source]

Bases: secml.ml.classifiers.c_classifier.CClassifier

CClassifierDNN, wrapper for DNN models.

Parameters
modelmodel dtype of the specific backend

The model to wrap.

input_shapetuple or None, optional

Shape of the input for the DNN, it will be used for reshaping the input data to the expected shape.

preprocessCPreprocess or str or None, optional

Preprocessing module.

pretrainedbool, optional

Whether or not the model is pretrained. If the model is pretrained, the user won’t need to call fit after loading the model. Default False.

pretrained_classesNone or CArray, optional

List of classes labels if the model is pretrained. If set to None, the class labels for the pretrained model should be inferred at the moment of initialization of the model and set to CArray.arange(n_classes). Default None.

softmax_outputsbool, optional

Whether or not to add a softmax layer after the logits. Default False.

Attributes
class_type‘dnn-clf’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_layer_gradient(self, x, w[, layer])

Computes the gradient of the classifier’s decision function wrt input.

get_layer_output(self, x[, layer])

Returns the output of the desired net layer(s).

get_params(self)

Returns the dictionary of class parameters.

get_state(self)

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

gradient(self, x[, w])

Compute gradient at x by doing a forward and a backward pass.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_model(self, filename)

Restores the model and optimization parameters.

load_state(self, path)

Sets the object state from file.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_model(self, filename)

Stores the model and optimization parameters.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

get_layer_gradient(self, x, w, layer=None)[source]

Computes the gradient of the classifier’s decision function wrt input.

Parameters
xCArray

Input sample

wCArray

Will be passed to backward and must have a proper shape depending on the chosen output layer (the last one if layer is None). This is required if layer is not None.

layerstr or None, optional

Name of the layer. If None, the gradient at the last layer will be returned and y is required if w is None or softmax_outputs is True. If not None, w of proper shape is required.

Returns
gradientCArray

Gradient of the classifier’s df wrt its input. Vector-like array.

get_layer_output(self, x, layer=None)[source]

Returns the output of the desired net layer(s).

Parameters
xCArray

Input data.

layerstr or None, optional

Name of the layer to get the output from. If None, the output of the last layer will be returned.

Returns
CArray

Output of the desired layer.

gradient(self, x, w=None)[source]

Compute gradient at x by doing a forward and a backward pass.

The gradient is pre-multiplied by w.

property input_shape

Returns the input shape of the first layer of the neural network.

property layer_names

Returns the names of the layers of the model.

abstract property layer_shapes

Returns a dictionary containing the shapes of the output of each layer of the model.

abstract property layers

Returns list of tuples containing the layers of the model. Each tuple is structured as (layer_name, layer).

abstract load_model(self, filename)[source]

Restores the model and optimization parameters. Notes: the model class should be defined before loading the params.

Parameters
filenamestr

path where to find the stored model

abstract save_model(self, filename)[source]

Stores the model and optimization parameters.

Parameters
filenamestr

path of the file for storing the model

property softmax_outputs

CClassifierPyTorch

class secml.ml.classifiers.pytorch.c_classifier_pytorch.CClassifierPyTorch(model, loss=None, optimizer=None, optimizer_scheduler=None, pretrained=False, pretrained_classes=None, input_shape=None, random_state=None, preprocess=None, softmax_outputs=False, epochs=10, batch_size=1, n_jobs=1)[source]

Bases: secml.ml.classifiers.c_classifier_dnn.CClassifierDNN, secml.ml.classifiers.gradients.mixin_classifier_gradient.CClassifierGradientMixin

CClassifierPyTorch, wrapper for PyTorch models.

Parameters
model:

torch.nn.Module object to use as classifier

loss:

loss object from torch.nn

optimizer:

optimizer object from torch.optim

random_state: int or None, optional

random state to use for initializing the model weights. Default value is None.

preprocess:

preprocessing module.

softmax_outputs: bool, optional

if set to True, a softmax function will be applied to the return value of the decision function. Note: some implementation adds the softmax function to the network class as last layer or last forward function, or even in the loss function (see torch.nn.CrossEntropyLoss). Be aware that the softmax may have already been applied. Default value is False.

epochs: int

number of epochs for training the neural network. Default value is 10.

batch_size: int

size of the batches to use for loading the data. Default value is 1.

n_jobs: int

number of workers to use for data loading and processing. This parameter follows the library expected behavior of having 1 worker as the main process. The loader will spawn n_jobs-1 workers. Default value for n_jobs is 1 (zero additional workers spawned).

Attributes
class_type‘pytorch-clf’

Defines class type.

Methods

backward(self[, w])

Returns the preprocessor gradient wrt data.

copy(self)

Returns a shallow copy of current class.

create([class_item])

This method creates an instance of a class with given type.

decision_function(self, x[, y])

Computes the decision function for each pattern in x.

deepcopy(self)

Returns a deep copy of current class.

estimate_parameters(self, dataset, …[, …])

Estimate parameter that give better result respect a chose metric.

fit(self, dataset[, n_jobs])

Trains the classifier.

forward(self, x[, caching])

Forward pass on input x.

get_class_from_type(class_type)

Return the class associated with input type.

get_layer_gradient(self, x, w[, layer])

Computes the gradient of the classifier’s decision function wrt input.

get_layer_output(self, x[, layer])

Returns the output of the desired net layer(s).

get_params(self)

Returns the dictionary of class parameters.

get_state(self[, return_optimizer])

Returns the object state dictionary.

get_subclasses()

Get all the subclasses of the calling class.

grad_f_params(self, x, y)

Derivative of the decision function w.r.t.

grad_f_x(self, x, y)

Computes the gradient of the classifier’s decision function wrt x.

grad_loss_params(self, x, y[, loss])

Derivative of a given loss w.r.t.

grad_tr_params(self, x, y)

Derivative of the classifier training objective function w.r.t.

gradient(self, x[, w])

Compute gradient at x by doing a forward and a backward pass.

hessian_tr_params(self, x, y)

Hessian of the training objective w.r.t.

hook_layer_output(self[, layer_names])

Creates handlers for the hooks that store the layer outputs.

is_fitted(self)

Return True if the classifier is trained (fitted).

list_class_types()

This method lists all types of available subclasses of calling one.

load(path)

Loads object from file.

load_model(self, filename[, classes])

Restores the model and optimizer’s parameters.

load_state(self, path)

Sets the object state from file.

n_jobs(self)

Returns the number of workers being used for loading and processing the data.

predict(self, x[, return_decision_function])

Perform classification of each pattern in x.

save(self, path)

Save class object to file.

save_model(self, filename)

Stores the model and optimizer’s parameters.

save_state(self, path)

Store the object state to file.

set(self, param_name, param_value[, copy])

Set a parameter of the class.

set_params(self, params_dict[, copy])

Set all parameters passed as a dictionary {key: value}.

set_state(self, state_dict[, copy])

Sets the object state using input dictionary.

timed([msg])

Timer decorator.

get_layer_shape

property batch_size

Returns the batch size used for the dataset loader.

property epochs

Returns the number of epochs for which the model will be trained.

get_layer_shape(self, layer_name)[source]
get_params(self)[source]

Returns the dictionary of class parameters.

get_state(self, return_optimizer=True)[source]

Returns the object state dictionary.

Parameters
return_optimizerbool, optional

If True (default), state of optimizer and optimizer_scheduler, if defined, will be included in the state dictionary.

Returns
dict

Dictionary containing the state of the object.

hook_layer_output(self, layer_names=None)[source]

Creates handlers for the hooks that store the layer outputs.

Parameters
layer_nameslist or str, optional

List of layer names to hook. Cleans previously defined hooks to prevent multiple hook creations.

property layer_shapes

Returns a dictionary containing the shapes of the output of each layer of the model.

property layers

Returns the layers of the model, if possible.

load_model(self, filename, classes=None)[source]

Restores the model and optimizer’s parameters. Notes: the model class and optimizer should be defined before loading the params.

Parameters
filenamestr

path where to find the stored model

classeslist, tuple or None, optional

This parameter is used only if the model was stored with native PyTorch. Class labels (sorted) for matching classes to indexes in the loaded model. If classes is None, the classes will be assigned new indexes from 0 to n_classes.

property loss

Returns the loss function used by classifier.

property model

Returns the model used by classifier.

n_jobs(self)[source]

Returns the number of workers being used for loading and processing the data.

property optimizer

Returns the optimizer used by classifier.

property optimizer_scheduler

Returns the optimizer used by classifier.

save_model(self, filename)[source]

Stores the model and optimizer’s parameters.

Parameters
filenamestr

path of the file for storing the model

set_state(self, state_dict, copy=False)[source]

Sets the object state using input dictionary.

property trained

True if the model has been trained.

secml.ml.classifiers.pytorch.c_classifier_pytorch.get_layers(net)[source]

clf_utils

secml.ml.classifiers.clf_utils.check_binary_labels(labels)[source]

Check if input labels are binary {0, +1}.

Parameters
labelsCArray or int

Binary labels to be converted. As of PRALib convention, binary labels are {0, +1}.

Raises
ValueError

If input labels are not binary.

secml.ml.classifiers.clf_utils.convert_binary_labels(labels)[source]

Convert input binary labels to {-1, +1}.

Parameters
labelsCArray or int

Binary labels in {0, +1} to be converted to {-1, +1}.

Returns
converted_labelsCArray or int

Binary labels converted to {-1, +1}.

Examples

>>> from secml.ml.classifiers.clf_utils import convert_binary_labels
>>> from secml.array import CArray
>>> print(convert_binary_labels(0))
-1
>>> print(convert_binary_labels(CArray([0,1,1,1,0,0])))
CArray([-1  1  1  1 -1 -1])