Utilities

class condensa.util.AverageMeter

Computes and stores the average and current value

class condensa.util.EventTimer

Simple timer class.

property elapsed_seconds

Returns elapsed seconds.

reset()

Reset timer.

condensa.util.accuracy(output, target, topk=(1, ))

Computes the precision@k for the specified values of k

Parameters
  • output (torch.Tensor) – Predicted output batch

  • target (torch.Tensor) – Actual output batch

  • topk (Tuple) – Top-k value

Returns

Model accuracy

Return type

float

condensa.util.cnn_statistics(model, criterion, dataloader)

Computes accuracy of given CNN model.

Parameters
  • model (torch.nn.Module) – PyTorch model

  • criterion – Loss function

  • dataloader – Data loader to use

Returns

Top-1 and Top-5 accuracies

Return type

Tuple(top1, top5)

condensa.util.compressed_model_stats(w, wc)

Retrieve various statistics for compressed model.

Parameters
  • w (torch.nn.Module) – Original model

  • wc (torch.nn.Module) – Compressed model

Returns

Dictionary of compressed model statistics

Return type

dict

condensa.util.empty_stat_fn(model, criterion, dataloader)

Empty model statistics function: returns loss.

Parameters
  • model (torch.nn.Module) – PyTorch model

  • loss_fn – Loss function

  • dataloader – Data loader to use

Returns

Tuple of loss, dictionary of statistics

Return type

Tuple(float, dict)

condensa.util.is_leaf_node(module)

Checks if given module is a leaf module.

Parameters

module (torch.nn.Module) – PyTorch module

Returns

Boolean value representing whether module is a leaf.

Return type

bool

condensa.util.loss(model, criterion, dataloader)

Computes loss on given dataset.

Parameters
  • model (torch.nn.Module) – PyTorch model

  • loss_fn – Loss function

  • dataloader – Data loader to use

Returns

Loss

Return type

float

condensa.util.magnitude_threshold(module, density)

Computes a magnitude-based threshold for given module.

Parameters
  • module (torch.nn.Module) – PyTorch module

  • density (float) – Desired ratio of nonzeros to total elements

Returns

Magnitude threshold

Return type

float

condensa.util.pretrain(epochs, model, trainloader, criterion, optimizer)

No-frills pre-training method.

Parameters
  • epochs (int) – Number of epochs

  • model (torch.nn.Module) – PyTorch model

  • trainloader – Training dataloader

  • criterion – Loss criterion

  • optimizer – Optimizer to use