hibou.nn.ModelSet#

class hibou.nn.ModelSet(*args, nb_models: int = 50, model_generator: Callable = None, no_init: bool = False, **kwargs)#

This class allows you to consider several identical models and use them as a single one.

Parameters#

args

The positionnal arguments to be given to model_generator.

kwargs

The keyword arguments to be given to model_generator.

nb_modelsint, optionnal, keyword-only

By default: 50. The number of models to consider.

model_generatorCallable, keyword-only

By default: default_model. A function that takes in arguments the hyperparameters and returns the compiled model.

Methods#

__call__(*args, **kwargs)#

Implements the call to a model to obtain the prediction.

Parameters#

args

The positionnal arguments to be given when the model is called.

kwargs

The keyword arguments to be given when the model is called.

Returns#

np.array

A NumPy array that contains the results of the call in two columns: (prediction, probability). The first dimension is the model.

__getitem__(index: int)#

Returns the model at the given index.

Parameters#

indexint

The index of the wanted model.

Returns#

keras.models.Sequential

The model at the given index.

fit(**kwargs)#

Implements the Model.fit method. You can use ModelSet.fit exactly as keras.Model.fit, and ditto for ModelSet.evaluate.

Parameters#

args

The positionnal arguments to be given to fit.

kwargs

The keyword arguments to be given to fit.

evaluate(**kwargs)#

Implements the Model.evaluate method. You can use ModelSet.evaluate exactly as keras.Model.evaluate. It also implements a basic filter on the models in the set. Models with too high loss or too low correlation will be ignored for prediction and deleted from the model list. The number of kept model is available in logs or you can access it via: len(my_modelset.models).

Note

Losses and correlations are calculated for each model during evaluation.

Parameters#

args

The positionnal arguments to be given to evaluate.

stdfloat, optionnal, keywork-only

By default: 2. The number of standard deviations used to define the limits of accepted models. For exemple, if std is set on 2 (the default value), all the models with a correlation coefficient under the mean correlation minus two standard deviations will be deleted.

kwargs

The keyword arguments to be given to evaluate.

save(path: str = None)#

Saves all the models of the ModelSet into the config.OUTPUT_DIR/models directory.

Parameters#

pathstr, optionnal

By default: "{config.OUTPUT_DIR}/models". The path of the directory in which the models should be saved.

static from_saved_models(path: str = None)#

Loads all the models from a given directory and return a ModelSet instance ready to be used.

Parameters#

pathstr, optionnal

By default: "{config.OUTPUT_DIR}/models". The path of the directory from which the models should be loaded.

Examples#

A basic example:

# creating a list of ten models with default parameters
my_list = ModelSet(nb_models=10)

# train the list
my_list.fit(x_train, y_train, epochs=100)

# evaluate the list
my_list.evaluate(x_test, y_test)

# use the list
result = my_list(inputs)
estimates, probabilities = result[:, 0], result[:, 1]