sample.optimize module

Automatic optimization of SAMPLE hyperparameters

class sample.optimize.SAMPLEOptimizer(model: ~sample.sample.SAMPLE = None, loss_fn: ~typing.Callable[[~numpy.ndarray, ~numpy.ndarray], float] = <function multiscale_spectral_loss>, loss_kw: ~typing.Optional[~typing.Dict[str, ~typing.Any]] = None, remap: ~typing.Optional[~typing.Callable[[...], ~typing.Dict[str, ~typing.Any]]] = <function sample_kwargs_remapper>, clip: bool = True, **kwargs)

Bases: object

Hyperparameter optimizer for a SAMPLE model, based on Gaussian Process minimization

Parameters:
  • sample_fn (callable) – Constructor for a SAMPLE model

  • sample_kw (dict) – Keyword arguments for sample_fn. These parameters will not be optimized. These parameters will potentially be remapped by remap

  • loss_fn (callable) – Loss function. It should take, as two positional arguments, the arrays of original and resynthesised audio samples

  • loss_kw (dict) – Keyword arguments for the loss function

  • remap (callable) – Function that accepts keyword arguments and returns a new dictionary of keyword arguments for sample_fn. Default is sample_kwargs_remapper()

  • clip (bool) – If True (default), then clip resynthesised audio to the same peak of the original audio

  • **kwargs – Parameters to optimize. See skopt.gp_minimize() dimensions for definition options

gp_minimize(x: ndarray, fs: float = 44100, state: Optional[OptimizeResult] = None, ignore_warnings: bool = True, fit_kws: Optional[Dict[str, Any]] = None, **kwargs) Tuple[SAMPLE, OptimizeResult]

Use skopt.gp_minimize() to tune the hyperparameters

Parameters:
  • x (array) – Audio samples

  • fs (float) – Sample rate

  • ignore_warnings (bool) – If True (default), then ignore warnings while optimizing

  • fit_kws (dict) – Arguments for the self.model.fit() method

  • **kwargs – Keyword arguments for skopt.gp_minimize()

Returns:

Best model, and optimization summary

Return type:

SAMPLE, OptimizeResult

loss(x: ndarray, fs: float) Callable[[...], float]

Define a loss function for the target audio based on computing loss_fn on the target and resynthesised audio

Parameters:
  • x (array) – Audio samples

  • fs (float) – Sample rate

Returns:

Loss function

Return type:

callable

property model

Default fget function

class sample.optimize.TqdmCallback(sample_opt: ~sample.optimize.SAMPLEOptimizer, n_calls: int, n_initial_points: int = 0, tqdm_fn: ~typing.Callable[[...], ~tqdm.std.tqdm] = <class 'tqdm.std.tqdm'>, minimum: bool = True)

Bases: object

Callback for using tqdm with SAMPLEOptimizer

Parameters:
  • sample_opt (SAMPLEOptimizer) – Optimizer instance

  • n_calls (int) – Number of total calls

  • n_initial_points (int) – Number of initial (random) points

  • tqdm_fn (callable) – Constructor for a tqdm object

  • minimum (bool) – If True (default), show current minimum in postfix

reset() TqdmCallback

Reset the state of the callback, e.g. for using it again

start() TqdmCallback

Start the callback. Calls to this method initialize internal objects

property started: bool

If True, the callback has already been started

sample.optimize.sample_kwargs_remapper(sinusoidal__log_n: Optional[int] = None, sinusoidal__wtype: str = 'hamming', sinusoidal__wsize: float = 1.0, sinusoidal__overlap: float = 0.5, **kwargs) Dict[str, Any]

Default argument remapper for SAMPLEOptimizer. It remaps stft window paramaters and lets every other parameter pass through

Parameters:
  • sinusoidal__log_n (int) – Logarithm of fft size. Will be remapped to sinusoidal__n if not in kwargs

  • sinusoidal__wtype (str) – Name of the window to use. Default is "hamming". It is used to compute the window sinusoidal__w if not in kwargs

  • sinusoidal__wsize (float) – Window size as a fraction of fft size. Default is 1.0. It is used to compute the window sinusoidal__w if not in kwargs

  • sinusoidal__overlap (float) – Window overlap as a fraction of the window size. Default is 0.5. It is used to compute the hop size sinusoidal__tracker__h if not in kwargs

  • **kwargs – Pass-through keyword arguments

Returns:

Remapped keyword arguments

Return type:

dict