Skip to content
class

AffineTransform

extendsTransform
AffineTransform(loc: Tensor | float, scale: Tensor | float, event_dim: int = 0)

Element-wise affine bijection y=loc+scale⋅xy = \mathrm{loc} + \mathrm{scale}\cdot x.

The fundamental location-scale transform. Composing with a standard Normal yields N(loc,scale2)\mathcal{N}(\mathrm{loc}, \mathrm{scale}^2); it underpins virtually every reparameterised sampler in the codebase. Bijective, with sign that of scale (+1, 0 or -1 for a number; the element-wise sign tensor for a tensor).

Parameters

locTensor or float
Additive offset bb.
scaleTensor or float
Multiplicative slope aa; a negative one makes the map decreasing.
event_dimint= 0
Number of trailing dimensions treated as a single event. Default 0.

Notes

Forward: y=b+a xy = b + a\,x.

Inverse: x=(y−b)/ax = (y - b) / a.

Log Jacobian determinant (broadcast to x.shape):

log⁡ ⁣∣∂y∂x∣=log⁡∣a∣\log\!\left|\frac{\partial y}{\partial x}\right| = \log|a|

For element-wise application (event_dim = 0) this is summed by IndependentTransform if the caller reinterprets trailing dims as event dims.

Examples

>>> import lucid
>>> from lucid.distributions.transforms import AffineTransform
>>> T = AffineTransform(loc=1.0, scale=2.0)
>>> T(lucid.tensor(3.0))
tensor(7.)

Used by 2

Constructors

1
dunder

__init__

→None
__init__(loc: Tensor | float, scale: Tensor | float, event_dim: int = 0)

Store the affine offset loc, multiplier scale, and event_dim.

Properties

3
prop

codomain

→Constraint
codomain: Constraint

real for scalar events, or independent(real, event_dim).

prop

domain

→Constraint
domain: Constraint

real for scalar events, or independent(real, event_dim).

prop

sign

→int | Tensor
sign: int | Tensor

Sign of scale: 1 / 0 / -1 for a number, else the sign tensor.

Instance methods

1
fn

log_abs_det_jacobian

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

Constant log-Jacobian log⁡∣scale∣\log|\text{scale}|.

The Jacobian of y=scale⋅x+locy = \text{scale} \cdot x + \text{loc} is just scale, so the log-determinant is constant in x. We add zeros_like(x) so the result broadcasts to x's full shape (callers expect element-wise log-Jacobians).

Parameters

Pre-transform input; used only for shape.
Post-transform output; ignored.

Returns

Tensor

log|scale| broadcast to x.shape and summed over event_dim.