melissa.server.deep_learning.active_sampling.breed_utils

melissa.server.deep_learning.active_sampling.breed_utils defines all the helper functions required for active sampling breed algorithm.

TensorType

Bases: Protocol

A static type protocol for record_increments.

LastN(n=1)

Maintains the last n unique values in the order of insertion.

Attributes
  • n (int): Maximum number of unique values to keep.
  • queue (deque): Stores the ordered unique values.
  • set (set): Tracks the unique values for fast membership checks.
insert(value)

Inserts a value, maintaining the order and uniqueness.

get_values()

Returns the current list of values in the queue.

initialize_container_with_sliding_window(n)

Initializes the LastN container.

weighted_average(values)

Computes the weighted average of the input values using a logarithmic weighting scheme.

Parameters
  • values (Union[List, NDArray]): A list or array of numerical values to compute the weighted average.
Returns
  • NDArray: An array containing the weighted average values.

calculate_delta_loss(loss_per_sample_in_batch)

Calculates delta loss values for every sample in batch - loss_per_sample_in_batch (NDArray): an array of losses per sample in a batch.

Returns
  • NDArray: An array containing delta loss values calculated per sample in a batch.

_record_increment(sim_id, t_step, value)

Records the delta loss for a given simulation ID and time step, updating its mean iteratively based on occurance.

Parameters
  • sim_id (int): The ID of the simulation.
  • t_step (int): The current time step in the simulation.

record_increments(sim_ids, time_ids, values)

Records delta losses for multiple simulation IDs and time steps, iteratively updating their means based on occurrence.

This function processes multiple simulation records in batch, converting inputs to raw numerical values before passing them to _record_increment.

Parameters
  • sim_ids (RecordType): A collection of simulation IDs.
  • time_ids (RecordType): A collection of time steps corresponding to sim_ids.
  • values (RecordType): A collection of loss values to record.

Each element in sim_ids, time_ids, and values must be convertible to an int or float.

get_fitnesses(weighted=False, lastN=True)

Computes the fitness values for recent simulations based on their mean delta loss.

Parameters
  • weighted (bool, optional): computes a weighted average of delta losses; otherwise, computes a simple mean. (Default is False).
Returns
  • Tuple[List[float], List[int]]:
    • List[float]: A list of fitness values (averaged or weighted delta losses).
    • List[int]: A list of simulation IDs corresponding to the fitness values.