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.

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\).

classmethod load_json(file, **kwargs) Self

Constructs an instance of cls through loading JSON from a file, converting it to a dict and then calling from_dict().

Parameters:
  • file (str or pathlib.Path or file-like object) – The JSON file to load. Either its path (str or pathlib.Path) or a file-like object.

  • kwargs – Keyword arguments passed to json.load().

Returns:

An instance of cls.

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\).

save_json(file, **kwargs)

Save self into a JSON file file.

Parameters:
  • file – The JSON file to save. Either its path (str or pathlib.Path) or a file-like object.

  • kwargs – Keyword arguments passed to json.dump().

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

to_json(**kwargs) str

Converts self into a JSON string.

Parameters:

kwargs – Keyword arguments passed to json.dumps().

Return type:

str