Loss Function

Abstract Class

Loss(model=None, save=False, verbose=True, MPI=False)[source]

Wrap a function of type f(x)=y. See LossFunc for more info.

Parameters
  • model (function, default=None) – Function of type f(x)=y. x must be a solution. A solution can be a list of float, int… It can also be of mixed types, containing, strings, float, int…

  • objective (Objective, default=Minimizer) – Objectve object determines what and and how to optimize. (minimization, maximization, ratio…)

  • save (string, optional) – Filename where to save the best found model. Only one model is saved for memory issues.

  • MPI (boolean, optional) – Wrap the function with MPILoss if True, with SerialLoss else.

Returns

wrapper – Wrapped original function

Return type

LossFunc

Examples

>>> import numpy as np
>>> from zellij.core.loss_func import Loss
>>> @Loss(save=False, verbose=True)
... def himmelblau(x):
...   x_ar = np.array(x)
...   return np.sum(x_ar**4 -16*x_ar**2 + 5*x_ar) * (1/len(x_ar))
>>> print(f"Best solution found: f({himmelblau.best_point}) = {himmelblau.best_score}")
Best solution found: f(None) = inf
>>> print(f"Number of evaluations:{himmelblau.calls}")
Number of evaluations:0
>>> print(f"All evaluated solutions:{himmelblau.all_solutions}")
All evaluated solutions:[]
>>> print(f"All loss values:{himmelblau.all_scores}")
All loss values:[]

Main wrapper

class LossFunc(model, objective=<class 'zellij.core.objective.Minimizer'>, historic=True, save=False, verbose=True, only_score=False, kwargs_mode=False)[source]

Bases: abc.ABC

LossFunc allows to wrap function of type f(x)=y. With x a set of hyperparameters. However, Zellij supports alternative pattern: f(x)=results,model for example. Where:

  • results can be a list or a dictionary. Be default the first element of the list or the dictionary is considered as the loss vale.

  • model is optionnal, it is an object with a save() method. (e.g. a neural network from Tensorflow)

You must wrap your function so it can be used in Zellij by adding several features, such as calls count, saves, parallelization, historic…

model

Function of type f(x)=y or f(x)=results,model. :math: must be a solution. A solution can be a list of float, int… It can also be of mixed types…

Type

function

objective

Objectve object determines what and and how to optimize. (minimization, maximization, ratio…)

Type

Objective, default=Minimizer

best_score

Best score found so far.

Type

float

best_point

Best solution found so far.

Type

list

best_argmin

Index of the best solution found so far.

Type

int

all_scores

Historic of all evaluated scores.

Type

float

all_solutions

Historic of all evaluated solutions.

Type

float

calls

Number of loss function calls

Type

int

See also

Loss

Wrapper function

MPILoss

Distributed version of LossFunc

SerialLoss

Basic version of LossFunc

build_bar(total)[source]

build_bar is a method to build a progress bar. It is a purely aesthetic feature to get info on the execution. You can deactivate it, with verbose=False.

Parameters

total (int) – Length of the progress bar.

close_bar()[source]

Delete the progress bar.

get_best(n_process=1, idx=None)[source]
reset()[source]

Reset all attributes of LossFunc at their initial values.

Concrete loss

class SerialLoss(model, objective=<class 'zellij.core.objective.Minimizer'>, historic=True, save=False, verbose=True, only_score=False, kwargs_mode=False)[source]

Bases: zellij.core.loss_func.LossFunc

SerialLoss adds methods to save and evaluate the original loss function.

__call__(X, filename='', \*\*kwargs)[source]

Evaluate a list X of solutions with the original loss function.

_save_model(score, source)[source]

See LossFunc, save a model according to its score and the worker rank.

See also

Loss

Wrapper function

LossFunc

Inherited class

MPILoss

Distributed version of LossFunc

class MPILoss(model, objective=<class 'zellij.core.objective.Minimizer'>, historic=True, save=False, verbose=True, only_score=False, kwargs_mode=False)[source]

Bases: zellij.core.loss_func.LossFunc

MPILoss adds method to dynamically distribute the evaluation of multiple solutions within a distributed environment, where a version of MPI is available.

comm

All created processes and their communication context are grouped in comm.

Type

MPI_COMM_WORLD

status

Data structure containing information about a received message.

Type

MPI_Status

rank

Process rank

Type

int

p

comm size

Type

int

master

If True the process is the master, else it is the worker.

Type

boolean

__call__(X, filename='', \*\*kwargs)[source]

Evaluate a list X of solutions with the original loss function.

worker()[source]

Initialize a worker.

stop()[source]

Stops all the workers and master.

_save_model(score, source)[source]

See LossFunc, save a model according to its score and the worker rank.

See also

Loss

Wrapper function

LossFunc

Inherited class

SerialLoss

Basic version of LossFunc

worker()[source]

Initialize worker. While it does not receive a stop message, a worker will wait for a solution to evaluate.

stop()[source]

Send a stop message to all workers.

Objective

Abstract Class

class Objective(target=0)[source]

Bases: abc.ABC

This absract object allows to define what is the objective of the optimization process.

Parameters

target (int or str, default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target
reset()[source]
class Minimizer(target=0)[source]

Bases: zellij.core.objective.Objective

Minimizer allows to minimize the given target. Do, f(y)=y. With y a given scores. /!By default Zellij metaheuristics minimize the loss value. So this object will just return the given scores.

Parameters

target (int or str, default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target
class Maximizer(target=0)[source]

Bases: zellij.core.objective.Objective

Maximizer allows to maximize the given target. Do, f(y)=-y. With y a given scores. /!By default Zellij metaheuristics minimize the loss value. So this object will compute the negative of the given scores.

Parameters

target (int or str, default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target
class Lambda(function, selector='min', target=0)[source]

Bases: zellij.core.objective.Objective

Lambda allows to transform the given target. Do, f(y)=function(y). With y a given scores. /!By default Zellij metaheuristics minimize the loss value.

Parameters
  • function (Callable) – Function with len(target) parameters which return an objective value.

  • selector ({"min","max"}) – Minimize or maximize the results from function

  • target ({int,str,list[{int, str}]} default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target

Concrete objective

class Minimizer(target=0)[source]

Bases: zellij.core.objective.Objective

Minimizer allows to minimize the given target. Do, f(y)=y. With y a given scores. /!By default Zellij metaheuristics minimize the loss value. So this object will just return the given scores.

Parameters

target (int or str, default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target
class Maximizer(target=0)[source]

Bases: zellij.core.objective.Objective

Maximizer allows to maximize the given target. Do, f(y)=-y. With y a given scores. /!By default Zellij metaheuristics minimize the loss value. So this object will compute the negative of the given scores.

Parameters

target (int or str, default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target
class Lambda(function, selector='min', target=0)[source]

Bases: zellij.core.objective.Objective

Lambda allows to transform the given target. Do, f(y)=function(y). With y a given scores. /!By default Zellij metaheuristics minimize the loss value.

Parameters
  • function (Callable) – Function with len(target) parameters which return an objective value.

  • selector ({"min","max"}) – Minimize or maximize the results from function

  • target ({int,str,list[{int, str}]} default=0) – Which outputs of the loss function should it target. Default is 0, it will consider the value at index 0 in the outputs of the loss function. If output is a dict, it can target one of its key.

target