alp.backend package

Adaptor for the Keras backend

Compilation & cache

The models are compiled on the fly after the build. If the model is already compiled and in the COMPILED_MODEL dictionnary mapping the models id to the in memory compiled function, this function is used instead.


build_predict_func(mod)[source]

Build Keras prediction functions based on a Keras model

Using inputs and outputs of the graph a prediction function (forward pass) is compiled for prediction purpose.

Parameters:mod (keras.models) – a Model or Sequential model
Returns:a Keras (Theano or Tensorflow) function
check_validation(dv)[source]
deserialize(name_d, func_code_d, args_d, clos_d, type_obj)[source]

A function to deserialize an object serialized with the serialize function.

Parameters:
  • name_d (unicode) – the dumped name of the object
  • func_code_d (unicode) – the dumped byte code of the function
  • args_d (unicode) – the dumped information about the arguments
  • clos_d (unicode) – the dumped information about the function closure
Returns:

a deserialized object

get_backend()[source]
get_function_name(o)[source]

Utility function to return the model’s name

Parameters:o (object) – an object to check
Returns:The name(str) of the object
model_from_dict_w_opt(model_dict, custom_objects=None)[source]

Builds a model from a serialized model using to_dict_w_opt

Parameters:
  • model_dict (dict) – a serialized Keras model
  • custom_objects (dict, optionnal) – a dictionnary mapping custom objects names to custom objects (Layers, functions, etc.)
Returns:

A Keras.Model which is compiled if the information about the optimizer is available.

save_params(model, filepath)[source]
serialize(cust_obj)[source]

A function to serialize custom objects passed to a model

Parameters:cust_obj (callable) – a custom layer or function to serialize
Returns:a dict of the serialized components of the object
to_dict_w_opt(model, metrics=None)[source]

Serialize a model and add the config of the optimizer and the loss.

Parameters:
  • model (keras.Model) – the model to serialize
  • metrics (list, optionnal) – a list of metrics to monitor
Returns:

a dictionnary of the serialized model

train(model, data, data_val, size_gen, generator=False, *args, **kwargs)[source]

Fit a model given hyperparameters and a serialized model

Parameters:
  • model (dict) – a serialized keras.Model
  • data (list) – a list of dict mapping inputs and outputs to lists or dictionnaries mapping the inputs names to np.arrays
  • data_val (list) – same structure than data but for validation
Returns:

the loss (list), the validation loss (list), the number of iterations, and the model

Adaptor for the sklearn backend

get_backend()[source]
getname(model, call=True)[source]
load_params(model, filepath)[source]

Load the attributes that have been dumped in a h5 file in a model.

Parameters:
  • model (sklearn.BaseEstimator) – a sklearn model (in SUPPORTED).
  • filepath (string) – the file name where the attributes should be read.
Returns:

the model with updated parameters.

model_from_dict_w_opt(model_dict, custom_objects=None)[source]

Builds a sklearn model from a serialized model using to_dict_w_opt

Parameters:
  • model_dict (dict) – a serialized sklearn model
  • custom_objects (dict, optionnal) – a dictionnary mapping custom objects names to custom objects (callables, etc.)
Returns:

A new sklearn.BaseEstimator (in SUPPORTED) instance. The attributes are not loaded.

save_params(model, filepath)[source]
Dumps the attributes of the (generally fitted) model
in a h5 file.
Parameters:
  • model (sklearn.BaseEstimator) – a sklearn model (in SUPPORTED).
  • filepath (string) – the file name where the attributes should be written.
to_dict_w_opt(model, metrics=None)[source]
Serializes a sklearn model. Saves the parameters,
not the attributes.
Parameters:
  • model (sklearn.BaseEstimator) – the model to serialize, must be in SUPPORTED
  • metrics (list, optionnal) – a list of metrics to monitor
Returns:

a dictionnary of the serialized model

train(model, data, data_val, size_gen, generator=False, *args, **kwargs)[source]

Fit a model given parameters and a serialized model

Parameters:
  • model (dict) – a serialized sklearn model
  • data (list) – a list of dict mapping inputs and outputs to lists or dictionnaries mapping the inputs names to np.arrays XOR -a list of fuel generators
  • data_val (list) – same structure than data but for validation.
  • it is possible to feed generators for data and plain data for data_val.
  • it is not possible the other way around.
Returns:

the loss (list), the validation loss (list), the number of iterations, and the model

typeconversion(v)[source]
Utility function to ease serialization of custom types
(namely np.types)
Parameters:
  • v (np.ndarray, list, other) – the object to return as a jsonable object.
  • If the type of v is not a np.ndarray or a list, the type of the – returned object is unchanged.
Returns:

a jsonable object, which type depends on the type of v