Skip to content
class

Transform

Transform()

Abstract bijection between two measurable spaces.

A Transform represents an invertible map T:X→YT : \mathcal{X} \to \mathcal{Y} together with its log-absolute-determinant Jacobian, used to push a base distribution through a measurable bijection. Composed with a base distribution via TransformedDistribution, it implements normalising flows, coupling layers, and standard change-of-variable arguments.

The change-of-variable formula for densities is

log⁡pY(y)=log⁡pX(x)−log⁡∣det⁡∂T(x)∂x∣,y=T(x)\log p_Y(\mathbf{y}) = \log p_X(\mathbf{x}) - \log \left|\det \frac{\partial T(\mathbf{x})}{\partial \mathbf{x}}\right|, \quad \mathbf{y} = T(\mathbf{x})

so the per-transform Jacobian determinant must be tractable.

Subclasses must override:

  • _call — forward map y=T(x)\mathbf{y} = T(\mathbf{x}).
  • _inverse — inverse map x=T−1(y)\mathbf{x} = T^{-1}(\mathbf{y}).
  • log_abs_det_jacobian — log⁡∣det⁡∂T/∂x∣\log \bigl|\det \partial T / \partial \mathbf{x}\bigr|, broadcast over the batch dimensions of the input.

Attributes

bijectivebool
True if the transform is invertible (most are; notable exception: AbsTransform).
signint or Tensor
+1+1 for monotone-increasing scalar bijections, −1-1 for monotone-decreasing (a tensor when the slope is a tensor, as with an AffineTransform whose scale is). TransformedDistribution reads it to orient its CDF. A transform with no meaningful sign (AbsTransform, the vector-valued transforms) raises NotImplementedError on access, so a missing sign is never mistaken for +1.
event_dimint
Number of trailing tensor dimensions that the transform treats as a single event. 0 for element-wise maps, ≥1\geq 1 for vector / matrix transforms (e.g. SoftmaxTransform has event_dim = 1, LowerCholeskyTransform has event_dim = 2).

Notes

Composition and inversion are provided structurally:

  • ComposeTransform — T=Tn∘⋯∘T1T = T_n \circ \cdots \circ T_1.
  • ~transform / inv — lazy _InverseTransform view.

so users can build complex bijectors without manually tracking Jacobian terms.

Examples

>>> import lucid
>>> from lucid.distributions.transforms import ExpTransform, AffineTransform, ComposeTransform
>>> # y = exp(2*x + 1) — a log-normal-like transform
>>> T = ComposeTransform([AffineTransform(loc=1.0, scale=2.0), ExpTransform()])
>>> x = lucid.tensor(0.0)
>>> y = T(x)
>>> T.log_abs_det_jacobian(x, y)  # log(2) + (2x + 1) at x = 0
tensor(1.693)

Used by 3

Constructors

2
dunder

__init__

→None
__init__()

Initialise the transform with an empty inverse cache.

dunder

__call__

→Tensor
__call__(x: Tensor)

Apply the forward map T(x) by delegating to _call.

Properties

4
prop

codomain

→Constraint
codomain: Constraint

Constraint describing valid outputs of this transform.

prop

domain

→Constraint
domain: Constraint

Constraint describing valid inputs to this transform.

prop

sign

→int | Tensor
sign: int | Tensor

Orientation of the map: +1 increasing, -1 decreasing.

Only meaningful for a monotone scalar bijection. A subclass states its own (a class attribute for a fixed one, a property for one that depends on parameters); the base refuses, so a transform that never declared a sign cannot be silently read as increasing.

Raises

NotImplementedError
For a transform with no defined sign.

Instance methods

3
fn

forward_shape

→tuple[int, ...]
forward_shape(shape: tuple[int, ...])

Infer the output shape from input shape (defaults to identity).

fn

inverse_shape

→tuple[int, ...]
inverse_shape(shape: tuple[int, ...])

Infer the input shape from output shape (defaults to identity).

fn

log_abs_det_jacobian

→Tensor
log_abs_det_jacobian(x: Tensor, y: Tensor)

Log-absolute-determinant of the Jacobian ∂T/∂x\partial T / \partial x.

Subclasses must override and return log⁡∣det⁡∂y/∂x∣\log|\det \partial y / \partial x| broadcast over the input batch dimensions.

Parameters

Pre-transform input.
Post-transform output (typically y = self(x)). Some transforms can compute the log-Jacobian in terms of y more cheaply than x, hence both are required.

Returns

Tensor

Element-wise log-absolute-Jacobian-determinant, broadcastable to x.shape.

Dunder methods

1
dunder

__repr__

→str
__repr__()

Return a developer-facing string representation of the instance.