Optimizer

https://github.com/INGEOTEC/IngeoML/actions/workflows/test.yaml/badge.svg https://coveralls.io/repos/github/INGEOTEC/IngeoML/badge.svg?branch=develop https://badge.fury.io/py/IngeoML.svg https://readthedocs.org/projects/ingeoml/badge/?version=latest

IngeoML.optimizer API

optimize(parameters: object, batches: list, objective: Callable[[object, array, array], object], epochs: int = 5, learning_rate: float = 0.0001, every_k_schedule: int = None, n_iter_no_change: int = inf, validation=None, model: Callable[[object, array], array] = None, return_evolution: bool = None, validation_score=None, discretize_val: bool = True, optimizer=None, **kwargs)[source]

Optimize

Parameters:
  • parameters – Parameters to optimize.

  • batches (list) – Batches used in the optimization.

  • objective – Objective function.

  • epochs – Number of epochs.

  • learning_rate (float) – Learning rate, default=1e-2.

  • every_k_schedule (int) – Update the parameters every k, default=jnp.inf.

  • validation – Validation set.

  • model – Model.

  • return_evolution (bool) – Whether to use the evolution the validation scores, default=None.

  • validation_score – Function to compute the validation-set performance.

  • discretize_val (True) – whether to transform one-hot encoding to labels

  • optimizer – Optimizer default optax.adam.

>>> import jax
>>> from sklearn.datasets import load_iris
>>> from sklearn.svm import LinearSVC
>>> from sklearn.preprocessing import OneHotEncoder
>>> from IngeoML.utils import Batches
>>> from IngeoML.utils import soft_BER
>>> from IngeoML.optimizer import optimize
>>> def model(params, X):
        Y = X @ params['W'] + params['W0']
        return Y
>>> def objective(params, X, y, w):
        hy = model(params, X)
        hy = jax.nn.softmax(jnp.array(hy), axis=1)
        return soft_BER(y, hy)
>>> model = jax.jit(model)
>>> X, y = load_iris(return_X_y=True)
>>> m = LinearSVC(dual='auto').fit(X, y)
>>> parameters = dict(W=m.coef_.T,
                      W0=m.intercept_)
>>> encoder = OneHotEncoder(sparse_output=False).fit(y.reshape(-1, 1))
>>> y_enc = encoder.transform(y.reshape(-1, 1))
>>> batches = Batches()
>>> batches = [[jnp.array(X[idx]),
                jnp.array(y_enc[idx]), None]
               for idx in batches.split(y=y)]
>>> optimize(parameters, batches, objective)
{'W': Array([[ 0.18344977,  0.05524644, -0.8504886],
             [ 0.4549369 , -0.9008946 , -0.9865761],
             [-0.8149536 ,  0.409234  ,  1.3809077],
             [-0.4335734 , -0.9606271 ,  1.8651136]], dtype=float32),
 'W0': Array([ 0.10852419,  1.6873716 , -1.710725], dtype=float32)}    
estimator(parameters: object, model: Callable[[object, array], array], X, y, batches: Batches = None, class_weight: str = 'balanced', n_iter_no_change: int = inf, deviation=None, n_outputs: int = None, validation=None, discretize_val: bool = True, classifier: bool = True, model_args: tuple = None, random_state: int = 0, distribution=False, **kwargs)[source]

Estimator optimized with optax

Parameters:
  • parameters – Parameters to optimize.

  • model – Model.

  • X – Independent variables.

  • y – Dependent variable.

  • batches (Batches) – Batches used in the optimization.

  • class_weight – Element weights.

  • n_iter_no_change (int) – Number of iterations without improving the performance.

  • deviation – Deviation function between the actual and predicted values.

  • n_output – Number of outputs.

  • validation – Validation set, if None it is created with ShuffleSplit or StratifiedShuffleSplit

  • discretize_val (bool) – whether to transform one-hot encoding to labels

  • classifier (bool) – The estimator is classifier, default=True.

  • model_args (Tuple) – Extra arguments to the model

  • random_state (int) – Random State

  • distribution (bool) – Whether the classifier’s model outputs a distribution, default=False.

>>> import jax
>>> from sklearn.datasets import load_iris
>>> from sklearn.svm import LinearSVC
>>> from IngeoML.optimizer import estimator
>>> def model(params, X):
        Y = X @ params['W'] + params['W0']
        return Y
>>> model = jax.jit(model)
>>> X, y = load_iris(return_X_y=True)
>>> m = LinearSVC(dual='auto').fit(X, y)
>>> parameters = dict(W=m.coef_.T,
                      W0=m.intercept_)
>>> p, evolution = estimator(parameters, model, X, y,
                             return_evolution=True)
classifier(parameters: object, model: Callable[[object, array], array], X, y, batches: Batches = None, class_weight: str = 'balanced', deviation=None, n_outputs: int = None, validation=None, discretize_val: bool = True, every_k_schedule=4, epochs=100, learning_rate=0.0001, n_iter_no_change=5, **kwargs)[source]

Classifier optimized with optax

Parameters:
  • parameters – Parameters to optimize.

  • model – Model.

  • X – Independent variables.

  • y – Dependent variable.

  • batches (Batches) – Batches used in the optimization.

  • class_weight – Element weights.

  • deviation – Deviation function between the actual and predicted values.

  • n_output – Number of outputs.

  • validation – Validation set.

  • discretize_val (bool) – whether to transform one-hot encoding to labels

  • every_k_schedule (int) – Update the parameters every k, default=4.

  • epochs – Number of epochs.

  • learning_rate (float) – Learning rate, default=1e-4.

  • n_iter_no_change (int) – Number of iterations without improving the performance, default=5.

>>> import jax
>>> from sklearn.datasets import load_iris
>>> from sklearn.svm import LinearSVC
>>> from IngeoML.optimizer import classifier
>>> def model(params, X):
        Y = X @ params['W'] + params['W0']
        return Y
>>> model = jax.jit(model)
>>> X, y = load_iris(return_X_y=True)
>>> m = LinearSVC(dual='auto').fit(X, y)
>>> parameters = dict(W=m.coef_.T,
                      W0=m.intercept_)
>>> p, evolution = classifier(parameters, model, X, y,
                              return_evolution=True)
regression(parameters: object, model: ~typing.Callable[[object, ~jax.numpy.array], ~jax.numpy.array], X, y, deviation=<PjitFunction of <function cos_distance>>, discretize_val=False, classifier=False, validation_score=<PjitFunction of <function cos_similarity>>, every_k_schedule=4, epochs=100, learning_rate=0.0001, n_iter_no_change=5, **kwargs)[source]

Regression optimized with optax

Parameters:
  • parameters – Parameters to optimize.

  • model – Model.

  • X – Independent variables.

  • y – Dependent variable.

  • deviation – Deviation function between the actual and predicted values, default=cos_distance.

  • discretize_val (True) – whether to transform one-hot encoding to labels

  • classifier (bool) – The estimator is classifier, default=False.

  • validation_score – Function to compute the validation-set performance.

  • every_k_schedule (int) – Update the parameters every k, default=4.

  • epochs – Number of epochs.

  • learning_rate (float) – Learning rate, default=1e-4.

  • n_iter_no_change (int) – Number of iterations without improving the performance, default=5.

>>> import jax
>>> import jax.nn as nn
>>> from sklearn.datasets import load_breast_cancer
>>> from sklearn.linear_model import LinearRegression
>>> from IngeoML.optimizer import regression
>>> def model(params, X):
        Y = X @ params['W'] + params['W0']
        return nn.sigmoid(Y).flatten()
>>> model = jax.jit(model)
>>> X, y = load_breast_cancer(return_X_y=True)
>>> m = LinearRegression().fit(X, y)
>>> parameters = dict(W=m.coef_.T,
                      W0=m.intercept_)
>>> p, evolution = regression(parameters, model, X, y,
                              return_evolution=True)