Utils

Individual_helper

gaggle.utils.individual_helper.from_gene_pool(gene_pool: dict) -> (<class 'torch.Tensor'>, tuple[list, list])[source]

From a gene_pool generates a flattened tensor that contains all the params of the gene_pool as well as metadata to be passed to from_tensor to recreate the gene_pool after any modification to the newly created flattened tensor.

Parameters:

gene_pool – dictionary of parameters that defines an Individual.

Returns:

Flattened gene tensor and a metadata tuple.

gaggle.utils.individual_helper.from_gene_pool_no_metadata(gene_pool: dict) Tensor[source]

Same as from_gene_pool but does return the metadata. It is used in cases where we do not care about transforming the tensor back into the gene pool (for example metric computation on the gene pool).

Parameters:

gene_pool

Returns:

Flattened gene tensor.

gaggle.utils.individual_helper.from_tensor(gene_pool: dict, tensor: Tensor, metadata: tuple[list, list]) dict[source]

Updates the parameters in gene_pool from the flattened tensor tensor inplace.

Parameters:
  • gene_pool – dictionary of parameters that defines an Individual.

  • tensor – the flattened tensor of weights that will be transformed into the gene_pool.

  • metadata – the metadata that was acquired when originally running from_gene_pool on gene_pool to get the tensor.

Returns:

The modified gene_pool dictionary

Notes

It applies the transformation of the gene_pool inplace on the gene_pool argument (even though it still returns it)

Metrics

gaggle.utils.metrics.accuracy(y_pred, y) float[source]

Accuracy metric for classification.

Parameters:
  • y_pred

  • y

Returns:

Numpy_helper

gaggle.utils.numpy_helper.tmp_seed(seed: int)[source]

Smooth_value

class gaggle.utils.smooth_value.SmoothedValue(window_size: int = 10, fmt: str = '{avg:.3f}')[source]

Bases: object

Track a series of values and provide access to smoothed values over a window or the global series average.

property avg: float
property global_avg: float
property max: float
property median: float
property min: float
reset()[source]
update(value: float, n: int = 1)[source]
update_list(value_list: list[float])[source]
property value: float

Special_images

gaggle.utils.special_images.image_to_tensor(image_file)[source]

Loads a PIL image and returns it with values scaled to [0,1]

gaggle.utils.special_images.plot_images(x: Tensor, n_row=2, dpi=400, savefig=None, title=None)[source]

Special_print

class gaggle.utils.special_print.bcolors[source]

Bases: object

BOLD = '\x1b[1m'
ENDC = '\x1b[0m'
FAIL = '\x1b[91m'
HEADER = '\x1b[95m'
OKBLUE = '\x1b[94m'
OKCYAN = '\x1b[96m'
OKGREEN = '\x1b[92m'
UNDERLINE = '\x1b[4m'
WARNING = '\x1b[93m'
gaggle.utils.special_print.print_dict_highlighted(content: dict, title=None, color='\x1b[92m')[source]
gaggle.utils.special_print.print_highlighted(content, color='\x1b[92m')[source]

Prints a highlighted message separated by dashes

content

gaggle.utils.special_print.print_response(content, color='\x1b[92m')[source]

Prints a highlighted message separated by dashes

content

gaggle.utils.special_print.print_warning(content)[source]

Torch_helper

class gaggle.utils.torch_helper.UnNormalize(mean, std)[source]

Bases: object

gaggle.utils.torch_helper.set_multiple_indices_to_zero(tensor, indices)[source]
gaggle.utils.torch_helper.tensor_hash(tensor)[source]
gaggle.utils.torch_helper.yield_batches(images, batch_size)[source]

Web

gaggle.utils.web.is_valid_url(string)[source]