eml.net package

Submodules

eml.net.describe module

class eml.net.describe.DNRActLayer(lb, ub, ylb, yub)[source]

Bases: eml.net.describe.DNRLayer

Class used to shape neural network’s activation layer

Therefore layers caracterized by an activation function.

ylb_

List of lower bounds for the current layer

Type:list(float)
yub_

List of upper bounds for the current layer

Type:list(float)
Parameters:
  • lb (list(float)) – List of lower bounds for the current layer
  • ub (list(float)) – List of upper bounds for the current layer
  • ylb (list(float)) – List of lower bounds for the current layer
  • yub (list(float)) – List of upper bounds for the current layer
bias(*idx)[source]

Get the bias associated to the neurons in the current layer

Raises:NotImplementedError – If not implemented in the instance
evaluate(data)[source]

Evalueates the output of the layer given a input

Raises:NotImplementedError – If not implemented in the instance
neuron(*idx)[source]

Get specific neuron in the current layer

Parameters:idx ((int, int)) – Index layer, neuron
Returns:Neuron
Return type:eml.net.describe.DNRActNeuron
neurons()[source]

Get the neurons in the current activaiton layer

Returns:List of Neuron – List of the neurons in the current layer
Return type:list(eml.net.describe.DNRLayer)
reset_bounds()[source]

Reset lower and upper bounds of the current layer

Raises:NotImplementedError – If not implemented in the instance
update_ylb(value, tol=0.0001)[source]

Updates output lower bounds of the layer

Parameters:
  • value (list(float)) – List of new lower bounds
  • tol (float) – Tollerance
Returns:

Acknowledge – True if executed succesfully, False otherwise

Return type:

bool

update_yub(value, tol=0.0001)[source]

Updates output upper bounds of the layer

Parameters:
  • value (list(float)) – List of new upper bounds
  • tol (float) – Tollerance
Returns:

Acknowledge – True if executed succesfully, False otherwise

Return type:

bool

weights(*idx)[source]

Get the weights associated to the neurons in the current layer

Raises:NotImplementedError – If not implemented in the instance
ylb()[source]

Get the output lower bound of the layer

Returns:List of lower bounds – List of output lower bounds for the current activation layer
Return type:list(float)
yub()[source]

Get the output upper bound of the layer

Returns:List of upper bounds – List of output upper bounds for the current activation layer
Return type:list(float)
class eml.net.describe.DNRActNeuron(layer, idx)[source]

Bases: eml.net.describe.DNRNeuron

Class used to shape neural network’s activation neurons

Therefore neurons with an activation function

layer_

Layer where the neuron is located

Type:eml.net.describe.DNRLayer
idx_

Index of the neuron

Type:int
Parameters:
  • layer (obj:eml.net.describe.DNRLayer) – Layer where the neuron is located
  • idx (int) – Index of the neuron
activation()[source]

Get the activation function of the neuron

Returns:Activation function – activation function of the neuron
Return type:keras.activations
bias()[source]

Get the bias associated to the neurons

Returns:Bias – Value corresponding to the bias of the current neuron
Return type:float
update_ylb(value, tol=0.0001)[source]

Updates output lower bound of the neuron

Parameters:
  • value (float) – Lower bound
  • tol (float) – Tollerance
Returns:

Acknowledge – True if executed succesfully, False otherwise

Return type:

bool

update_yub(value, tol=0.0001)[source]

Updates output upper bound of the neuron

Parameters:
  • value (float) – Upper bound
  • tol (float) – Tollerance
Returns:

Acknowledge – True if executed succesfully, False otherwise

Return type:

bool

weights()[source]

Get the weight associated to the neuron

Returns:Weight – Value corresponding to the weight of the current neuron
Return type:float
ylb()[source]

Get the output lower bound of the neuron

Returns:Lower bound – Output lower bounds for the current activation neuron
Return type:float
yub()[source]

Get the output upper bound of the neuron

Returns:Upper bound – Output upper bounds for the current activation neuron
Return type:float
class eml.net.describe.DNRDense(weights, bias, activation)[source]

Bases: eml.net.describe.DNRActLayer

Class used to shape the dense layer of the neural network

lb_

List of lower bounds for the input layer

Type:list(float)
ub_

list of upper bounds for the input layer

Type:list(float)
idx_

Index of the input layer

Type:int
net_

name of the neural network

Type:string
weights_

List of weight associated to the neurons in the layer

Type:list(float)
bias_

List of bias associated to the neurons in the layer

Type:list(float)
activation_

Activation function

Type:keras.activations
Parameters:
  • weights (list(float)) – List of weight associated to the neurons in the layer
  • bias (list(float)) – List of bias associated to the neurons in the layer
  • activation (keras.activations) – Activation function
Raises:

ValueError – If bias and weight amatrixes are inconsistent

activation()[source]

Get activation function of the layer

Returns:Activation function – Activation function of the layer
Return type:keras.activations
bias(*idx)[source]

Get the bias associated to a specific neuron in the layer

Parameters:idx ((int, int)) – Index of the layer
Returns:Bias – Value corresponding to the bias of the current neuron
Return type:float
connected(*idx)[source]

Get the neurons connected to a specific one in the current layer

Parameters:idx ((int, int)) – Coordinates of a specific neuron
Yields:Index ((int, int)) – Coordinates to the connected neuron
evaluate(data)[source]

Evalueates the output of the layer given a input

Parameters:data (list(float)) – Input for the layer
Returns:Layer output – Output of the layer
Return type:float
ltype()[source]

Get type of the layer

Returns:Layer type – String identifying the type of the layer
Return type:string
reset_bounds()[source]

Reset lower and upper bounds of the layer

Returns:
Return type:None
weights(*idx)[source]

Get the weights associated to the neurons in a specific layer

Parameters:idx ((int, int)) – Index of the layer
Yields:Weight (float) – Next weight in the layer
class eml.net.describe.DNREvaluation(net, evals, yevals)[source]

Bases: object

Class used to shape the neural network evaluator

This class evaluates the results of a neural network buil according to the model at eml.net.describe.DNRNet

net_

Neural network

Type:eml.net.describe.DNRNet
evals_

Evaluations of a layer

Type:list(list(float))
yeavals_

Evaluations of an activation layer

Type:list(list(float))
Parameters:
layer(idx)[source]

Get specific evaluation of a layer

Parameters:idx (int) – Index of the layer
Returns:Evaluations – Evaluations of the specified layer
Return type:list(float)
xval(*idx)[source]

Get evaluation of a specific neuron

Parameters:idx ((int, int)) – Index of the neuron
Returns:Evaluation – Evaluation of the neuron
Return type:float
ylayer(idx)[source]

Get specific evaluation for activation layer

Parameters:idx (int) – Index of the layer
Returns:Evaluations – Evaluations of the specified activation layer
Return type:list(float)
yval(*idx)[source]

Get evaluation of a specific activation neuron

Parameters:idx ((int, int)) – Index of the activation neuron
Returns:Evaluation – Evaluation of the activation neuron
Return type:float
class eml.net.describe.DNRInput(input_shape=None, lb=None, ub=None)[source]

Bases: eml.net.describe.DNRLayer

Class used to shape the input layer of the neural network

lb_

List of lower bounds for the input layer

Type:list(float)
ub_

list of upper bounds for the input layer

Type:list(float)
idx_

Index of the input layer

Type:int
net_

name of the neural network

Type:string
Parameters:
  • input_shape ((int, int)) – Dimensions fo the input (default None)
  • lb (list(float)) – Lower bounds for the input layer
  • ub (list(float)) – Upper bounds for the input layer
Raises:

ValueError – If not enought parameters are specified or the input shape is inconsistent

connected(*idx)[source]

Get the neurons connected to a specific one in the current layer

Parameters:idx ((int, int)) – Coordinates of a specific neuron
Returns:
Return type:None
ltype()[source]

Get type of the layer

Returns:Layer type – String identifying the type of the layer
Return type:String
reset_bounds()[source]

Reset lower and upper bounds of the layer

Returns:
Return type:None
class eml.net.describe.DNRLayer(lb, ub)[source]

Bases: object

Class used to shape neural network layers

Layer of the network. It is composed by a sequence of neurons with lower and upper bound. Multiple layer together define a neural network.

lb_

List of lower bounds for this layer

Type:list(float)
ub_

List of upper bounds for this layer

Type:list(float)
idx_

Index for this layer

Type:int
net_

Name of the neural network where the layer is in

Type:string
Parameters:
  • lb (float) – List of lower bounds for this layer
  • ub (float) – List of upper bounds for this layer
connected(*idx)[source]

Get the neurons connected to a specific one in the current layer

Parameters:idx ((int, int)) – Coordinates of a specific neuron
Raises:NotImplementedError – If not implemented in the instanca
lb()[source]

Get the lower bound of the current layer

Returns:List of lower bounds – List of the lower bound of the current layer
Return type:list(float)
ltype()[source]

Get the type of the layer

Raises:NotImplementedError – If not implemented in the instanca
network()[source]

Get network name

Returns:Name – Name of the network
Return type:string
neuron(*idx)[source]

Get neuron specific neuron

Every neuron in a network can be identified by a index that specifies the layer and the position in the layer of it.

Parameters:idx ((int, int)) – index
Returns:Neuron – Neuron fuond at the index given
Return type:eml.net.describe.DNRNeuron
neurons()[source]

Get all the neurons in this layer

Yields:Neuron (eml.net.describe.DNRNeuron) – Next neuron in the layer
reset_bounds()[source]

Reset lower and upper bounds of the current layer

Raises:NotImplementedError – If not implemented in the instanca
size()[source]

Get size layer

Returns:Size of the layer – Number of neuron in the layer
Return type:int
ub()[source]

Get the upper bound of the current layer

Returns:List of upper bounds – List of the upper bound of the current layer
Return type:list(float)
update_lb(value, tol=0.0001)[source]

Update lower bounds of the current layer

Parameters:
  • value (list(float)) – New values for the lower bounds
  • tol (float) – Tollerance
Returns:

Acknoledge – True if executed succesfully, Flase otherwise

Return type:

bool

update_ub(value, tol=0.0001)[source]

Update upper bounds of the current layer

Parameters:
  • value (list(float)) – New values for the upper bounds
  • tol (float) – Tollerance
Returns:

Acknoledge – True if executed succesfully, Flase otherwise

Return type:

bool

class eml.net.describe.DNRNet[source]

Bases: object

Class used to shape the neural network

layers_

List of layers composing the network

Type:list(eml.net.describe.DNRLayers)
add(layer)[source]

Add layer to the network

Parameters:layer (eml.net.describe.DNRLayers) – Layer to add
Returns:
Return type:None
evaluate(data)[source]

Evalueates the output of the neural netwokr

Parameters:data (list(float)) – Input for the network
Returns:Network output – output of the network given the input
Return type:float
layer(idx)[source]

Get layer with specific index

Parameters:idx (int) – Layer index
Returns:Layer – Layer located at the given coordinates
Return type:eml.net.describe.DNRLayer
layers()[source]

Get layers in the network

Yields:Layer (eml.net.describe.DNRLayers) – Next layer in the network
neuron(*idx)[source]

Get neuron in a specific location in the network

Parameters:idx ((int, int)) – Index of the neuron, (layer, neuron)
Returns:Neuron – Neuron located at the given coordinates
Return type:eml.net.describe.DNRNeuron
neurons()[source]

Get all neurons in the network

Yields:Neuron (eml.net.describe.DNRNeuron) – Next neuron in the network
nlayers()[source]

Get number of layers

Returns:Number of layers – Integer representing the number of layers
Return type:int
reset_bounds()[source]

Reset lower and upper bound in the network

Returns:
Return type:None
size()[source]

Get size of the network

Returns:Number of neurons – Integer describing the size of the network
Return type:int
class eml.net.describe.DNRNeuron(layer, idx)[source]

Bases: object

Class used to shape neural network’s neurons

Layers are build using neurons. Eache neuron has a lower and upper bound. Bound represent the range of value the neuron can have.

layer_

Layer where the neuron is located

Type:eml.net.describe.DNRLayer
idx_

Identifier of the neuron

Type:int
Parameters:
connected()[source]

Get the neurons connected to the current one

Returns:List of nuerons
Return type:list(eml.net.describe.DNRNeuron)
idx()[source]

Get idenfier of the neuron

Returns:Index – Coordinates of layer and neuron in the netwokr
Return type:(int, int)
layer()[source]

Get the layer where the neuron is located

Returns:Layer – Layer where the neuron is located
Return type:eml.net.describe.DNRLayer
lb()[source]

Get the lower bound of the current neuron

Returns:Lower bound – Lower bound of the current neuron
Return type:float
network()[source]

Get the name of the network where the neuron is

Returns:Name – Name of the network
Return type:string
ub()[source]

Get the upper bound of the current neuron

Returns:Upper bound – Upper bound of the current neuron
Return type:float
update_lb(value, tol=0.0001)[source]

Updates the lower bound of the current neuron

Parameters:
  • value (float) – New lower bound
  • tol (float) – Tollerance
Returns:

Acknowledge – True is executed succesfully, False otherwise

Return type:

bool

update_ub(value, tol=0.0001)[source]

Updates the upper bound of the current neuron

Parameters:
  • value (float) – New upper bound
  • tol (float) – Tollerance
Returns:

Acknowledge – True is executed succesfully, False otherwise

Return type:

bool

eml.net.describe.act_eval(activation, value)[source]

Evaluates the activation function

Parameters:
  • activation (keras.activations) – Activation function, either relu or linear
  • value (float) – Input of the activation function
Returns:

Activation Function Result – Value computed by the activation function

Return type:

float

Raises:

ValueError – If activation function is invalid (it is not relu or linear)

eml.net.embed module

eml.net.embed.encode(bkd, net, mdl, net_in, net_out, name, verbose=0)[source]

Encodes the network in the optimization model.

Codifies each neuron as a variable in the combinatorial problem, while each edge is considered as a constraint on the the neurons connected.

Parameters:
  • bkd (eml.backend.cplex_backend.CplexBackend) – Backend Cplex
  • net (eml.net.describe.DNRNet) – Network to embed
  • mdl (docplex.mp.model.Model) – Model CPLEX
  • net_in (list(docplex.mp.linear.Var)) – Input continuous varibles
  • net_out (docplex.mp.linear.Var) – Output continuous varibles
  • name (string) – Name of the network
  • verbose (int) – If higher than 0 notifies every neuron embeded
Returns:

Descriptor – Descriptor of the neural network

Return type:

eml.util.ModelDesc

eml.net.process module

eml.net.process.fwd_bound_tighthening(bkd, net=None, desc=None, timelimit=None, skip_layers=None, verbose=0)[source]

Forward bound tightening via Mixed Integer Linear Programming

Parameters:
  • bkd (eml.backend.cplex_backend.CplexBackend) – Cplex backend
  • net (obj:eml.net.describe.DNRNet) – Neural network of interest (default None)
  • desc (eml.util.ModelDesc) – Model descriptor (default None)
  • timelimit (int) – Time limit for the process (default None)
  • skip_layer (int) – Skips bound tightening for the specified layer (default None)
  • verbose (int) – if higher than 0 prints more info on the process (default 0)
Returns:

Total time – Time used to perform bound tightening by the optimizer

Return type:

int

Raises:

ValueError – Neither a model descriptor or a network where given in input

eml.net.process.ibr_bounds(net)[source]

Internal Based Reasoning Bounding

The bounds of the units in the neural network are updated based on the values evaluated using the activation function

net : eml.net.describe.DNRNet
Neural network of interest
Returns:
Return type:None

Module contents