pydpf.base.constrained_parameter#

class pydpf.base.constrained_parameter(function: Callable[[Module], Tuple[Tensor, Tensor]])#

Bases: object

Wrapper for constraining parameters. The wrapped function must belong to a Module and should take only a reference to its parent Module and return a reference to the original parameter and a tensor containing the new value.

constrained_parameter applies the change in-place; the underlying data of the parameter is modified. Necessarily, therefore this is done without gradient tracking.

The constraint is applied on calling Module.update(), so you should do this after every gradient update to preserve the constraint.

Notes

constrained_parameters can be applied to any parameter accessible from a Module including as attributes of a child Module. This is safe as the parameter is changed in-place.

Warning

@constrained_parameter should be used to impose constraints only, the underlying data is modified inplace and without gradient. @constrained_parameter should always act directly on the underlying parameter, they cannot be stacked or used to constrain functions of parameters.

__init__(function: Callable[[Module], Tuple[Tensor, Tensor]])#

Methods

__init__(function)