clipppy.commands.fit

Module Contents

class clipppy.commands.fit.Fit(**kwargs)

Fit using ELBO maximisation.

avgwindow :int = 100

Number of most recents steps to use for calculating various averaged quantities (average rate, mean loss…).

callback :Callable[[int, float, Mapping[str, Any]], Any]

Callback to be executed after each step.

Signature should be callback(i, loss, locals), where i is the current step index, loss is the current loss, and locals is a dictionary of the local variables in the fitting function. Depending on the python implementation, modifying its values might influence the fitting. Returning any True value interrupts the fitting.

conv_th :float

Convergence threshold. Should be positive or -float('inf') to turn off convergence-based termination.

See converged.

loss_args :Mapping

Arguments for the loss_cls constructor.

Pass the special value Command.no_call to avoid instantiating load_cls and use it directly.

loss_cls :Union[Type[pyro.infer.ELBO], Callable[Ellipsis, torch.Tensor]]

The loss class to instantiate.

lr :float = 0.001

Learning rate (passed to the optimizer).

min_steps :int = 1

Run at least min_steps steps.

n_steps :int = 1000

Run at most n_steps steps.

n_write :int

Save the guide each n_write steps.

optimizer_args :Optional[Mapping]

The (keyword!) arguments to pass to optimizer_cls.

Will be updated with the standalone lr passed, unless it is noop.

Pass the special value Command.no_call to avoid instantiating optimizer_cls and use it directly.

optimizer_cls :Union[Type[pyro.optim.PyroOptim], Callable[[Mapping], pyro.optim.PyroOptim]]

The class of PyroOptim to instantiate (or a function that acts like it).

converged(self, slope: float, windowed_losses: Iterable[float] = None)

Indicate whether the fit is considered converged.

This implementation returns True if the averaged slope slope, defined as the fitted slope of losses over the last avgwindow iterations, divided by the learning rate, is shallower than conv_th, which should be given as positive. If the slope is positive, this will always be true, so turning this check off requires setting conv_th = -float('inf').

This method can be overridden in subclasses to provide more sophisticated checks.

forward(self, model: clipppy.utils.typing._Model, guide: clipppy.utils.typing._Guide, *args, **kwargs)
property lossfunc(self) Union[pyro.infer.ELBO, Callable[Ellipsis, torch.Tensor]]

Construct a loss as loss_cls(**loss_args) or simply return loss_cls if loss_args is Command.no_call.

property optimizer(self) pyro.optim.PyroOptim

Construct an optimizer as optimizer_cls(optimizer_args) or simply return optimizer_cls if optimizer_args is Command.no_call.