secml.ml.classifiers¶
CClassifier¶
-
class
secml.ml.classifiers.c_classifier.
CClassifier
(preprocess=None, n_jobs=1)[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 information 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.
- n_jobsint, optional
Number of parallel workers to use for training the classifier. Cannot be higher than processor’s number of cores. Default is 1.
- 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).
- n_jobs
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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 hyperparameters.
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')[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 information.
- 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 information.
- 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’.
- Returns
- best_parametersdict
Dictionary of best parameters found through performance evaluation.
-
fit
(self, x, y)[source]¶ Trains the classifier.
If a preprocess has been specified, input is normalized before training.
For multiclass case see .CClassifierMulticlass.
- Parameters
- xCArray
Array to be used for training with shape (n_samples, n_features).
- yCArray or None, optional
Array of shape (n_samples,) containing the class labels. Can be None if not required by the algorithm.
- Returns
- CClassifier
Trained classifier.
-
fit_forward
(self, x, y=None, caching=False)[source]¶ Fit estimator using data and then execute forward on the data.
To avoid returning over-fitted scores on the training set, this method runs a 5-fold cross validation on training data and returns the validation scores.
- Parameters
- xCArray
Array with shape (n_samples, n_features) to be transformed and to be used for training.
- yCArray or None, optional
Array of shape (n_samples,) containing the class labels. Can be None if not required by the algorithm.
- caching: bool
True if preprocessed x should be cached for backward pass
- Returns
- CArray
Transformed input data.
See also
fit
fit the preprocessor.
forward
run forward function on input data.
-
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.
CClassifierLinearMixin
[source]¶ Bases:
object
Mixin class that defines basic methods for linear classifiers.
A linear classifier assigns 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 defines the weight and bias, and the forward and backward functions for linear classifiers.
-
abstract property
b
¶ Bias term.
-
abstract property
w
¶ Vector with feature weights (dense or sparse).
-
abstract property
CClassifierSkLearn¶
-
class
secml.ml.classifiers.sklearn.c_classifier_sklearn.
CClassifierSkLearn
(sklearn_model, preprocess=None)[source]¶ Bases:
secml.ml.classifiers.sklearn.c_classifier_sklearn.CWrapperSkLearnMixin
,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.
- preprocessCModule or str or None, optional
Features preprocess to be applied to input data. Can be a CModule 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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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.
-
class
secml.ml.classifiers.sklearn.c_classifier_sklearn.
CWrapperSkLearnMixin
(sklearn_model)[source]¶ Bases:
object
Generic wrapper for SkLearn instances.
- Parameters
- sklearn_modelsklearn.base.BaseEstimator object
The scikit-learn instance to wrap.
- Attributes
sklearn_model
Wrapped SkLearn classifier.
Methods
get_params
(self)Returns the dictionary of class and SkLearn model parameters.
-
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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.CClassifierLinearMixin
,secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn
,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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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_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
b
¶ Bias term.
-
property
w
¶ Vector with feature weights (dense or sparse).
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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, max_iter=100000.0, class_weight=None, tol=0.0001, fit_intercept=True, preprocess=None)[source]¶ Bases:
secml.ml.classifiers.c_classifier_linear.CClassifierLinearMixin
,secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn
,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.
- 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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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_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
¶ Constant that multiplies the regularization term.
Equal to 1 / alpha.
-
property
b
¶ Bias term.
-
property
w
¶ Vector with feature weights (dense or sparse).
CClassifierSGD¶
-
class
secml.ml.classifiers.sklearn.c_classifier_sgd.
CClassifierSGD
(loss, regularizer, 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.CClassifierLinearMixin
,secml.ml.classifiers.sklearn.c_classifier_sklearn.CClassifierSkLearn
,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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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_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).
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
b
¶ Bias term.
-
property
loss
¶ Returns the loss function used by classifier.
-
property
regularizer
¶ Returns the regularizer function used by classifier.
-
property
w
¶ Vector with feature weights (dense or sparse).
CClassifierSVM¶
-
class
secml.ml.classifiers.sklearn.c_classifier_svm.
CClassifierSVM
(C=1.0, kernel=None, class_weight=None, preprocess=None, n_jobs=1)[source]¶ Bases:
secml.ml.classifiers.c_classifier.CClassifier
Support Vector Machine (SVM) classifier.
- Parameters
- Cfloat, optional
Penalty hyper-parameter C of the error term. Default 1.0.
- kernelNone or CKernel subclass, optional
Instance of a CKernel subclass to be used for computing similarity between patterns. If None (default), a linear SVM is trained in the primal; otherwise an SVM is trained in the dual, using the precomputed kernel values.
- 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)).
- preprocessCModule 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.
- n_jobsint, optional
Number of parallel workers to use for the classifier. Cannot be higher than processor’s number of cores. Default is 1.
See also
CKernel
Pairwise kernels and metrics.
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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 hyperparameters.
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).
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
alpha
¶ Signed coefficients of the SVs in the decision function.
-
property
b
¶
-
property
class_weight
¶ Weight of each training class.
-
grad_f_params
(self, x, y=1)[source]¶ Derivative of the decision function w.r.t. alpha and b
- Parameters
- xCArray
Samples on which the training objective is computed.
- yint
Index of the class wrt the gradient must be computed.
-
grad_loss_params
(self, x, y, loss=None)[source]¶ Derivative of the loss w.r.t. the classifier parameters (alpha, b)
dL / d_params = dL / df * df / d_params
- Parameters
- xCArray
Features of the dataset on which the loss is computed.
- yCArray
Labels of the training samples.
- loss: None (default) or CLoss
If the loss is equal to None (default) the classifier loss is used to compute the derivative.
-
grad_tr_params
(self, x, y)[source]¶ Derivative of the classifier training objective w.r.t. the classifier parameters.
dL / d_params = dL / df * df / d_params + dReg / d_params
- Parameters
- xCArray
Features of the dataset on which the loss is computed.
- yCArray
Features of the training samples
-
hessian_tr_params
(self, x=None, y=None)[source]¶ Hessian of the training objective w.r.t. the classifier parameters.
-
property
kernel
¶ Kernel type (None or string).
-
property
sv_idx
¶ Indices of Support Vectors within the training dataset.
-
property
w
¶
CClassifierDNN¶
-
class
secml.ml.classifiers.c_classifier_dnn.
CClassifierDNN
(model, input_shape=None, preprocess=None, pretrained=False, pretrained_classes=None, softmax_outputs=False, n_jobs=1)[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.
- n_jobsint, optional
Number of parallel workers to use for training the classifier. Cannot be higher than processor’s number of cores. Default is 1.
- 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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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 hyperparameters.
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_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.
-
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.
create_chain
(class_items, kwargs_list)Creates a chain of preprocessors.
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, x, y)Trains the classifier.
fit_forward
(self, x[, y, caching])Fit estimator using data and then execute forward on the data.
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 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.
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_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.
-
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
-
property
trained
¶ True if the model has been trained.
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])