TorchOptimizer#

class TorchOptimizer(optimizer_class, **kwargs)[source]#

Bases: OptimizerInterface

The wrapper class for PyTorch optimizers.

This class wraps a torch.optim.Optimizer class and defers its instantiation until runtime. It enables a consistent interface across different optimizer backends while leveraging PyTorch’s optimization algorithms.

Example:
>>> from pina.optim import TorchOptimizer
>>> import torch
>>> optimizer = TorchOptimizer(torch.optim.Adam, lr=0.001)
>>> optimizer.optimizer_class
<class 'torch.optim.adam.Adam'>

Initialization of the TorchOptimizer class.

Parameters:
  • optimizer_class (torch.optim.Optimizer) – The subclass of torch.optim.Optimizer to be instantiated.

  • kwargs (dict) – Additional keyword arguments forwarded to the optimizer constructor. See more here.

Raises:

ValueError – If optimizer_class is not a subclass of torch.optim.Optimizer.

hook(parameters)[source]#

Execute custom logic associated with the optimizer instance.

This method is intended to encapsulate any additional behavior that should be triggered during the optimization process.

Parameters:

parameters (dict) – The parameters of the model to be optimized.

property instance#

The underlying optimizer object.

Returns:

The optimizer instance.

Return type:

torch.optim.Optimizer