secml.optim.constraints

CConstraint

class secml.optim.constraints.c_constraint.CConstraint[source]

Bases: secml.core.c_creator.CCreator

Interface for equality/inequality constraints.

Attributes
class_type

Defines class type.

logger

Logger for current object.

verbose

Verbosity level of logger output.

Methods

constraint(self, x)

Returns the value of the constraint for the sample x.

copy(self)

Returns a shallow copy of current class.

create([class_item])

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

deepcopy(self)

Returns a deep copy of current class.

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.

gradient(self, x)

Returns the gradient of c(x) in x.

is_active(self, x[, tol])

Returns True if constraint is active.

is_violated(self, x)

Returns the violated status of the constraint for the sample 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.

projection(self, x)

Project x onto feasible domain / within the given constraint.

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.

constraint(self, x)[source]

Returns the value of the constraint for the sample x.

Parameters
xCArray

Input sample.

Returns
float

Value of the constraint.

gradient(self, x)[source]

Returns the gradient of c(x) in x.

Parameters
xCArray

Input sample.

Returns
CArray

The gradient of the constraint computed on x.

is_active(self, x, tol=0.0001)[source]

Returns True if constraint is active.

A constraint is active if c(x) = 0.

By default we assume constraints of the form c(x) <= 0.

Parameters
xCArray

Input sample.

tolfloat, optional

Tolerance to use for comparing c(x) against 0. Default 1e-4.

Returns
bool

True if constraint is active, False otherwise.

is_violated(self, x)[source]

Returns the violated status of the constraint for the sample x.

We assume the constraint violated if c(x) <= 0.

Parameters
xCArray

Input sample.

Returns
bool

True if constraint is violated, False otherwise.

projection(self, x)[source]

Project x onto feasible domain / within the given constraint.

If constraint is not violated by x, x is returned.

Parameters
xCArray

Input sample.

Returns
CArray

Projected x onto feasible domain if constraint is violated. Otherwise, x is returned as is.

CConstraintBox

class secml.optim.constraints.c_constraint_box.CConstraintBox(lb=None, ub=None)[source]

Bases: secml.optim.constraints.c_constraint.CConstraint

Class that defines a box constraint.

Parameters
lb, ubscalar or CArray or None, optional

Bounds of the constraints. If scalar, the same bound will be applied to all features. If CArray, should contain a bound for each feature. If None, a +/- inf ub/lb bound will be used for all features.

Attributes
class_type‘box’

Defines class type.

Methods

constraint(self, x)

Returns the value of the constraint for the sample x.

copy(self)

Returns a shallow copy of current class.

create([class_item])

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

deepcopy(self)

Returns a deep copy of current class.

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.

gradient(self, x)

Returns the gradient of c(x) in x.

is_active(self, x[, tol])

Returns True if constraint is active.

is_violated(self, x)

Returns the violated status of the constraint for the sample 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.

projection(self, x)

Project x onto feasible domain / within the given constraint.

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_center_radius(self, c, r)

Set constraint bounds in terms of center and radius.

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 center

Center of the constraint.

is_active(self, x, tol=0.0001)[source]

Returns True if constraint is active.

A constraint is active if c(x) = 0.

By default we assume constraints of the form c(x) <= 0.

Parameters
xCArray

Input sample.

tolfloat, optional

Tolerance to use for comparing c(x) against 0. Default 1e-4.

Returns
bool

True if constraint is active, False otherwise.

is_violated(self, x)[source]

Returns the violated status of the constraint for the sample x.

We assume the constraint violated if c(x) <= 0.

Parameters
xCArray

Input sample.

Returns
bool

True if constraint is violated, False otherwise.

property lb

Lower bound.

property radius

Radius of the constraint.

set_center_radius(self, c, r)[source]

Set constraint bounds in terms of center and radius.

This method will transform the input center/radius as follows:

lb = center - radius ub = center + radius

Parameters
cscalar

Constraint center.

rscalar

Constraint radius.

property ub

Upper bound.

CConstraintL1

class secml.optim.constraints.c_constraint_l1.CConstraintL1(center=0, radius=1)[source]

Bases: secml.optim.constraints.c_constraint.CConstraint

L1 Constraint.

Parameters
centerscalar or CArray, optional

Center of the constraint. Use an array to specify a different value for each dimension. Default 0.

radiusscalar, optional

The semidiagonal of the constraint. Default 1.

Attributes
class_type‘l1’

Defines class type.

Methods

constraint(self, x)

Returns the value of the constraint for the sample x.

copy(self)

Returns a shallow copy of current class.

create([class_item])

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

deepcopy(self)

Returns a deep copy of current class.

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.

gradient(self, x)

Returns the gradient of c(x) in x.

is_active(self, x[, tol])

Returns True if constraint is active.

is_violated(self, x)

Returns the violated status of the constraint for the sample 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.

projection(self, x)

Project x onto feasible domain / within the given constraint.

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 center

Center of the constraint.

property radius

Semidiagonal of the constraint.

CConstraintL2

class secml.optim.constraints.c_constraint_l2.CConstraintL2(center=0, radius=1)[source]

Bases: secml.optim.constraints.c_constraint.CConstraint

L2 Constraint.

Parameters
centerscalar or CArray, optional

Center of the constraint. Use an array to specify a different value for each dimension. Default 0.

radiusscalar, optional

The semidiagonal of the constraint. Default 1.

Attributes
class_type‘l2’

Defines class type.

Methods

constraint(self, x)

Returns the value of the constraint for the sample x.

copy(self)

Returns a shallow copy of current class.

create([class_item])

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

deepcopy(self)

Returns a deep copy of current class.

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.

gradient(self, x)

Returns the gradient of c(x) in x.

is_active(self, x[, tol])

Returns True if constraint is active.

is_violated(self, x)

Returns the violated status of the constraint for the sample 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.

projection(self, x)

Project x onto feasible domain / within the given constraint.

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 center

Center of the constraint.

property radius

Radius of the constraint.