Transform

class dnois.torch.Transform(fn: Callable[[Tensor], Tensor], *args, **kwargs)

Base class for parameter transformations (see ParamTransformModule and transformed parameters). This class also serves as a namespace containing functions to create commonly used transformations. All these function return a transformation object.

The constructor of this class has two overloaded forms:

  • Accepts an inverse transformation after fn or as inverse argument;

  • Accepts any arguments combination (*args and **kwargs) which will be passed to fn along with latent value to calculate nominal value. No inverse transformation is specified in this case.

In function descriptions below, \(x\) indicates latent value and \(y\) indicates nominal value.

static composite(*transforms: Transform) Transform

Chain some transformations into a single transformation. Resulted transformation is to apply all the transformations sequentially and resulted inverse transformation is to apply all the inversions in a reversed order.

Parameters:

transforms (Transform) – One or more transformations to apply.

forward(x: Tensor) Tensor

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

classmethod from_dict(d: dict)

Constructs an instance of cls from a dict.

Parameters:

d (dict) – A dict typically returned by to_dict().

Returns:

An instance of cls.

static gt(limit: Real | Tensor) Transform

Force parameter to be greater than \(a\) using exponential function:

\[y=a+\e^x,x=\ln(y-a)\]
Parameters:

limit (float or Tensor) – Lower bound \(a\).

static lt(limit: Real | Tensor) Transform

Force parameter to be less than \(b\) using exponential function:

\[y=b-\e^x,x=\ln(b-y)\]
Parameters:

limit (float or Tensor) – Upper bound \(b\).

static negative() Transform

Force parameter to be negative using exponential function:

\[y=-\e^x,x=\ln -y\]
static positive() Transform

Force parameter to be positive using exponential function:

\[y=\e^x,x=\ln y\]
static range(min_: Real | Tensor, max_: Real | Tensor) Transform

Limit the range of parameter to \((a,b)\) using sigmoid function:

\[y=(b-a)\sigma(x)+a,x=\sigma^{-1}(\frac{y-a}{b-a})\]

where \(\sigma(t)=\frac{1}{1+\e^{-t}}\) and \(\sigma^{-1}(t)=\ln\frac{t}{1-t}\).

Parameters:
  • min (float or Tensor) – Lower bound \(a\).

  • max (float or Tensor) – Upper bound \(b\).

static scale(s: Real | Tensor) Transform

Multiply the latent value by a scalar factor:

\[y=sx,x=y/s\]
Parameters:

s (float or Tensor) – Scale factor \(s\).

to_dict(keep_tensor: bool = True) dict[str, Any]

Converts self into a dict which recursively contains only primitive Python objects.

Return type:

dict