Pre-defined Breed Classes¶
melissa.server.deep_learning.active_sampling.parameters.ExperimentBreeder¶
Bases: RandomUniformSamplerMixIn
, HaltonSamplerMixIn
, LatinHypercubeSamplerMixIn
, StaticExperiment
A base class for breeding and sampling experimental parameters. It initializes the parameters based on the chosen sampling method and logs relevant experiment data.
Inherits from multiple mix-ins for sampling and static experiment management.
When inheriting this class, override sample()
and optionally, draw()
.
Parameters¶
- non_breed_sampler_t (
ParameterSamplerType
, default=RANDOM_UNIFORM
): The type of parameter sampler to use. Can beRANDOM_UNIFORM
,HALTON
, orLHS
. Note that the first generation will be populated from this sampler type.
Attributes¶
- checkpoint_data (
Dict[str, Any]
): Stores experiment checkpoint data. - tb_logger (
TensorboardLogger
): Logger for tracking experiment metrics in TensorBoard.
get_non_breed_samples(nb_samples=-1)
¶
Returns the nb_samples
using the default (non-breed) sampler.
next_parameters(**kwargs)
¶
This method is required for producing the next set of parameters i.e the next generation.
It must be called only through melissa.server.deep_learing.active_sampling
module and
can be overridden.
Parameters¶
- kwargs (
Dict[str, Any]
): A keyword arguments for custom preprocessing.
Returns¶
Any
: A set of parameters for the next generation.
set_tb_logger(tb_logger)
¶
Sets the Tensorboard logger.
melissa.server.deep_learning.active_sampling.parameters.DefaultBreeder¶
Bases: ExperimentBreeder
A class that extends ExperimentBreeder
for managing breeding experiments with more advanced
control over sampling and breeding parameters.
Parameters¶
- sigma (
Union[float, Iterable]
, default=0.005): Covariance initialization for breeding. - start (
float
, default=0.15): Starting breeding ratio. - end (
float
, default=0.75): Ending breeding ratio. - breakpoint (
int
, default=10): Number of steps in the breeding ratio transition. - use_true_mixing (
bool
, default=False): Use true mixing for breeding. - log_extra (
bool
, default=False): Log extra information for debugging. - scatter_function (
Callable[[NDArray], Tuple[str, NDArray, str, NDArray]]
, default=(lambda x: ("x0", x[:,0], "x1", x[:,1])): Scatter function that takes one NDarrayself.parameters
of shape(nb_sims, nb_params)
and returns two Ndarays of shape (nb_sims,) for plotting scatter on X-Y axis labeled with provided strings. - device (
str
, default="cpu"): Device for computation, e.g., "cpu" or "cuda".
Attributes¶
- sigma_opt (
float
): Optimal minimum covariance value for breeding. - covs (
NDArray[np.float32]
): Covariance matrix for each simulation. - Rs (
NDArray
): Linearly spaced breeding ratios for each breeding step. - R_i (
int
): Current breeding ratio index. - R (
float
): Current breeding ratio. - oob_factor (
float
): Factor by which the covariance decreases when the child is out-of-bounds. - max_oob_count (
int
): Maximum allowed attempts for out-of-bounds children. - oob_count (
List[List[int]]
): List of out-of-bounds count for each simulation. - parameters_is_bred (
NDArray[np.bool_]
): Boolean array indicating whether a simulation's parameters have been bred.
set_tb_logger(tb_logger)
¶
Sets the Tensorboard logger.
checkpoint_state()
¶
Saves the current state for checkpointing.
restart_from_checkpoint()
¶
Restores the state from a checkpoint.
get_breeding_status_per_parameter()
¶
Returns a boolean array stating which parameter indices were bred.
_sigma_init(sigma)
¶
Initializes the sigma
values for the sampling process,
ensuring they are within acceptable bounds.
The function checks the type and size of sigma
:
- If
sigma
is a float, it initializes an array of the same size as the number of parameters (nb_params
). - If
sigma
is an array-like object, it verifies that its size matches the number of parameters (nb_params
), otherwise raises an assertion error. - The function then ensures that
sigma
values are within a valid range, scaling them if necessary to be within bounds defined by the parameter's upper and lower limits (u_bounds
andl_bounds
).
Parameters¶
- sigma (
Union[float, Iterable]
): The value(s) to initialize the sigma. This can either be a single float value (applied to all parameters) or an array-like object (list, tuple, or NDArray) specifying the sigma for each parameter.
Raises¶
RuntimeError
: Ifsigma
is neither a float nor an iterable.
next_parameters(start_sim_id, max_breeding_count=-1)
¶
Override the parent class sampling method with custom breed-specific arguments.
This method calculates the fitness for each simulation, selects breeding candidates, and returns a new set of parameters based on the custom breeding algorithm.
Parameters¶
- start_sim_id (
int
): The starting simulation id from which breeding should begin. - max_breeding_count (
int
, optional): The maximum number of breed iterations. (Default is -1 i.e all remaining parameters).
Note: Unlike the parent, this method does not return anything as __breed_algorithm
method directly modifies parameters
attribute of the class.
reset_index()
¶
Placeholder method. __breed_algorithm
updates _current_idx
value directly.
__breed_algorithm(fitness_per_sim, sim_ids, start_sim_id, max_breeding_count=-1)
¶
Breeding algorithm to generate new parameters based on simulation fitness.
This method selects parent simulations based on their fitness scores, performs the breeding
process to generate new parameters, and updates the breeding statistics. It also logs
various statistics regarding the breeding process with TensorboardLogger
.
Parameters¶
- fitness_per_sim (
Union[NDArray, list]
): Fitness values corresponding to each simulation. - sim_ids (
Union[NDArray, list]
): List or array of simulation ids. - start_sim_id (
int
): The starting simulation id for breeding. - max_breeding_count (
int
, optional): The maximum number of parameters to consider afterstart_sim_id
. (Default is -1 i.e all remaining parameters).
__set_children(parent_idx_list, child_idx_list)
¶
Unoptimized breeding process to generate child parameters by sampling from the parent parameters and their covariance, with an additional check for out-of-bounds conditions. If a child falls out of bounds, its covariance is reduced, and the child is re-sampled.
This method uses scipy.stats.multivariate_normal
to perform
sampling per simulation iteratively.
Parameters¶
- parent_idx_list (
NDArray
): List of indices for the parent simulations. - child_idx_list (
NDArray
): List of indices for the child simulations that are being bred.