pyttb.gcp.samplers

Implementation of various sampling approaches for GCP OPT.

class pyttb.gcp.samplers.StratifiedCount(num_zeros: int, num_nonzeros: int)[source]

Bases: object

Contains stratified sampling counts.

num_zeros: int
num_nonzeros: int
__init__(num_zeros: int, num_nonzeros: int) None
class pyttb.gcp.samplers.Samplers(value)[source]

Bases: Enum

Implemented Samplers.

UNIFORM = 0
SEMISTRATIFIED = 1
STRATIFIED = 2
class pyttb.gcp.samplers.GCPSampler(data: tensor | sptensor, function_sampler: Samplers | None = None, function_samples: int | StratifiedCount | None = None, gradient_sampler: Samplers | None = None, gradient_samples: int | StratifiedCount | None = None, max_iters: int = 1000, over_sample_rate: float = 1.1)[source]

Bases: object

Contains Gradient and Function Sampling Details.

Create sampler.

Parameters:
  • data – Tensor we will be sampling. Allows for automated decisions and sanity checks.

  • function_sampler – Type of sampling used for evaluating function estimates.

  • function_samples – How many samples to take of the function every iteration.

  • gradient_sampler – Type of sampling used for evaluating gradient estimates.

  • gradient_samples – How many samples to take of the gradient every iteration.

  • max_iters – Maximum number of iterations to normalize number of samples.

  • over_sample_rate – Ratio of extra samples to take to account for bad draws.

__init__(data: tensor | sptensor, function_sampler: Samplers | None = None, function_samples: int | StratifiedCount | None = None, gradient_sampler: Samplers | None = None, gradient_samples: int | StratifiedCount | None = None, max_iters: int = 1000, over_sample_rate: float = 1.1)[source]

Create sampler.

Parameters:
  • data – Tensor we will be sampling. Allows for automated decisions and sanity checks.

  • function_sampler – Type of sampling used for evaluating function estimates.

  • function_samples – How many samples to take of the function every iteration.

  • gradient_sampler – Type of sampling used for evaluating gradient estimates.

  • gradient_samples – How many samples to take of the gradient every iteration.

  • max_iters – Maximum number of iterations to normalize number of samples.

  • over_sample_rate – Ratio of extra samples to take to account for bad draws.

function_sample(data: tensor | sptensor) Tuple[ndarray, ndarray, ndarray][source]

Draw a sample from the objective function.

gradient_sample(data: tensor | sptensor) Tuple[ndarray, ndarray, ndarray][source]

Draw a sample from the gradient function.

property crng: ndarray

Correction Range for possibly miss-sampled zeros.

pyttb.gcp.samplers.nonzeros(data: sptensor, samples: int, with_replacement: bool = True) Tuple[ndarray, ndarray][source]

Sample nonzeros from a sparse tensor.

Parameters:
  • data – Tensor to sample.

  • samples – Number of samples to collect

  • with_replacement – Whether or not to sample with replacement.

Returns:

Subscripts and values for samples.

pyttb.gcp.samplers.zeros(data: sptensor, nz_idx: ndarray, samples: int, over_sample_rate: float = 1.1, with_replacement=True) ndarray[source]

Sample zeros from a sparse tensor.

Parameters:
  • data – Tensor to sample.

  • nz_idx – Sorted linear indices of the nonzeros in tensor.

  • samples – Number of samples to retrieve.

  • over_sample_rate – Oversampling rate to allow success if a samples is large relative to data.nnz

  • with_replacement – Whether or not to sample with replacement.

pyttb.gcp.samplers.uniform(data: tensor, samples: int) Tuple[ndarray, ndarray, ndarray][source]

Uniformly samples indices from a tensor.

Parameters:
  • data – Tensor to sample.

  • samples – Number of samples to take.

Returns:

Subscripts of samples, values at those subscripts, and weight of samples.

pyttb.gcp.samplers.semistrat(data: sptensor, num_nonzeros: int, num_zeros: int) Tuple[ndarray, ndarray, ndarray][source]

Sample nonzero and zero entries from a sparse tensor.

Parameters:
  • data – Tensor to sample.

  • num_nonzeros – Number of nonzero samples requested.

  • num_zeros – Number of zero samples requested.

Returns:

Subscripts, values, and weights of samples (Nonzeros then zeros).

pyttb.gcp.samplers.stratified(data: sptensor | tensor, nz_idx: ndarray, num_nonzeros: int, num_zeros: int, over_sample_rate: float = 1.1) Tuple[ndarray, ndarray, ndarray][source]

Sample nonzero and zero entries from a sparse tensor.

Parameters:
  • data – Tensor to sample.

  • nz_idx – Sorted linear indices of non-zero entries in tensor.

  • num_nonzeros – Number of nonzero samples requested.

  • num_zeros – Number of zero samples requested.

  • over_sample_rate – Rate of oversampling to account for bad random draws.

Returns:

Subscripts, values, and weights of samples (Nonzeros then zeros).