mrpro.operators.ConstraintsOp
- class mrpro.operators.ConstraintsOp[source]
Bases:
EndomorphOperator
Transformation to map real-valued tensors to certain ranges.
- __init__(bounds: Sequence[tuple[float | None, float | None]], beta_sigmoid: float = 1.0, beta_softplus: float = 1.0) None [source]
Initialize a constraint operator.
The operator maps real-valued tensors to certain ranges. The transformation is applied element-wise. The transformation is defined by the bounds. The bounds are applied in the order of the input tensors. If there are more input tensors than bounds, the remaining tensors are passed through without transformation.
If an input tensor is bounded from below AND above, a sigmoid transformation is applied. If an input tensor is bounded from below OR above, a softplus transformation is applied.
If an input is complex valued, the bounds are to the real and imaginary parts separately, i.e., for bounds \((a, b)\), the complex number is constrained to a rectangle in the complex plane with corners \((a+ai, a+bi, b+ai, b+bi)\).
- Parameters:
bounds (
Sequence
[tuple
[float
|None
,float
|None
]]) – Sequence of(lower_bound, upper_bound)
values. If a bound isNone
, the value is not constrained. If a lower bound is-inf
, the value is not constrained from below. If an upper bound is+inf
, the value is not constrained from above. If the bounds are set to(None, None)
or(-inf, +inf)
, the value is not constrained at all.beta_sigmoid (
float
, default:1.0
) – beta parameter for the sigmoid transformation (used if an input has two bounds). A higher value leads to a steeper sigmoid.beta_softplus (
float
, default:1.0
) – parameter for the softplus transformation (used if an input is either bounded from below or above). A higher value leads to a steeper softplus.
- Raises:
ValueError – If the lower bound is greater than the upper bound.
ValueError – If the a bound is nan.
ValueError – If the parameter beta_sigmoid and beta_softplus are not greater than zero.
- __call__(*x: Tensor) tuple[Tensor, ...] [source]
Transform tensors to chosen range.
Applies element-wise transformations to map input tensors to specified bounds. - If bounded below and above: uses a sigmoid transformation. - If bounded below or above: uses a softplus transformation. - If complex: applies transformation to real and imaginary parts separately. - If more input tensors than bounds: remaining tensors pass through unchanged.
- Parameters:
*x (
Tensor
) – One or more input tensors to be transformed.- Returns:
Transformed tensors, with values mapped to the ranges defined by the bounds.
- forward(*x: Tensor) tuple[Tensor, ...] [source]
Apply forward of ConstraintsOp.
Note
Prefer calling the instance of the ConstraintsOp operator as
operator(x)
over directly calling this method. See this PyTorch discussion.
- invert(*x_constrained: Tensor) tuple[Tensor, ...] [source]
Reverses the variable transformation.
- Parameters:
x_constrained (
Tensor
) – transformed tensors with values in the range defined by the bounds- Returns:
tensors in the domain with no bounds
- __add__(other: Operator[Unpack[Tin], Tout]) Operator[Unpack[Tin], Tout] [source]
- __add__(other: Tensor | complex) Operator[Unpack[Tin], tuple[Unpack[Tin]]]
Operator addition.
Returns
lambda x: self(x) + other(x)
if other is a operator,lambda x: self(x) + other*x
if other is a tensor
- __matmul__(other: EndomorphOperator) EndomorphOperator [source]
- __matmul__(other: Operator[Unpack[Tin], Tout]) Operator[Unpack[Tin], Tout]
Operator composition.
- __mul__(other: Tensor | complex) Operator[Unpack[Tin], Tout] [source]
Operator multiplication with tensor.
Returns
lambda x: self(x*other)
- __radd__(other: Tensor | complex) Operator[Unpack[Tin], tuple[Unpack[Tin]]] [source]
Operator right addition.
Returns
lambda x: other*x + self(x)