L iB(dZddlmZmZddlmZgdZGddZeZeZ ejeje jejdZ ejejdZe jejd Zejej ejej"e jej e jej"d Zejej&ejej(e jej&e jej(d Zejej,e jej,d Zejej0ejej2e jej0e jej2d Zejej6dZe jej6dZe jej<dZe jej@e jejBdZ"ejejFe jejFdZ$ejejJdZ&e jejJdZ'ejejPdZ)e jejPdZ*y)aF PyTorch provides two global :class:`ConstraintRegistry` objects that link :class:`~torch.distributions.constraints.Constraint` objects to :class:`~torch.distributions.transforms.Transform` objects. These objects both input constraints and return transforms, but they have different guarantees on bijectivity. 1. ``biject_to(constraint)`` looks up a bijective :class:`~torch.distributions.transforms.Transform` from ``constraints.real`` to the given ``constraint``. The returned transform is guaranteed to have ``.bijective = True`` and should implement ``.log_abs_det_jacobian()``. 2. ``transform_to(constraint)`` looks up a not-necessarily bijective :class:`~torch.distributions.transforms.Transform` from ``constraints.real`` to the given ``constraint``. The returned transform is not guaranteed to implement ``.log_abs_det_jacobian()``. The ``transform_to()`` registry is useful for performing unconstrained optimization on constrained parameters of probability distributions, which are indicated by each distribution's ``.arg_constraints`` dict. These transforms often overparameterize a space in order to avoid rotation; they are thus more suitable for coordinate-wise optimization algorithms like Adam:: loc = torch.zeros(100, requires_grad=True) unconstrained = torch.zeros(100, requires_grad=True) scale = transform_to(Normal.arg_constraints["scale"])(unconstrained) loss = -Normal(loc, scale).log_prob(data).sum() The ``biject_to()`` registry is useful for Hamiltonian Monte Carlo, where samples from a probability distribution with constrained ``.support`` are propagated in an unconstrained space, and algorithms are typically rotation invariant.:: dist = Exponential(rate) unconstrained = torch.zeros(100, requires_grad=True) sample = biject_to(dist.support)(unconstrained) potential_energy = -dist.log_prob(sample).sum() .. note:: An example where ``transform_to`` and ``biject_to`` differ is ``constraints.simplex``: ``transform_to(constraints.simplex)`` returns a :class:`~torch.distributions.transforms.SoftmaxTransform` that simply exponentiates and normalizes its inputs; this is a cheap and mostly coordinate-wise operation appropriate for algorithms like SVI. In contrast, ``biject_to(constraints.simplex)`` returns a :class:`~torch.distributions.transforms.StickBreakingTransform` that bijects its input down to a one-fewer-dimensional space; this a more expensive less numerically stable transform but is needed for algorithms like HMC. The ``biject_to`` and ``transform_to`` objects can be extended by user-defined constraints and transforms using their ``.register()`` method either as a function on singleton constraints:: transform_to.register(my_constraint, my_transform) or as a decorator on parameterized constraints:: @transform_to.register(MyConstraintClass) def my_factory(constraint): assert isinstance(constraint, MyConstraintClass) return MyTransform(constraint.param1, constraint.param2) You can create your own registry by creating a new :class:`ConstraintRegistry` object. ) constraints transforms)_Number)ConstraintRegistry biject_to transform_toc0eZdZdZfdZddZdZxZS)rz5 Registry to link constraints to transforms. c0i|_t| yN) _registrysuper__init__)self __class__s m/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/constraint_registry.pyrzConstraintRegistry.__init__Us c|fdSttjr tttrt tjst d|j <|S)a Registers a :class:`~torch.distributions.constraints.Constraint` subclass in this registry. Usage:: @my_registry.register(MyConstraintClass) def construct_transform(constraint): assert isinstance(constraint, MyConstraint) return MyTransform(constraint.arg_constraints) Args: constraint (subclass of :class:`~torch.distributions.constraints.Constraint`): A subclass of :class:`~torch.distributions.constraints.Constraint`, or a singleton object of the desired class. factory (Callable): A callable that inputs a constraint object and returns a :class:`~torch.distributions.transforms.Transform` object. c(j|Sr )register)factory constraintrs rz-ConstraintRegistry.register..ls4==W#ErzLExpected constraint to be either a Constraint subclass or instance, but got ) isinstancer Constrainttype issubclass TypeErrorr rrrs`` rrzConstraintRegistry.registerYsx$ ?E E j+"8"8 9j)J*d+:  ..4 ^_i^jk &-z"rc |jt|}||S#t$r$tdt|jddwxYw)ah Looks up a transform to constrained space, given a constraint object. Usage:: constraint = Normal.arg_constraints["scale"] scale = transform_to(constraint)(torch.zeros(1)) # constrained u = transform_to(constraint).inv(scale) # unconstrained Args: constraint (:class:`~torch.distributions.constraints.Constraint`): A constraint object. Returns: A :class:`~torch.distributions.transforms.Transform` object. Raises: `NotImplementedError` if no transform has been registered. zCannot transform z constraintsN)r rKeyErrorNotImplementedError__name__rs r__call__zConstraintRegistry.__call__|s`( nnT*%56G z""  %#D$4$=$=#>lK  s "-Ar )r" __module__ __qualname____doc__rrr# __classcell__)rs@rrrPs!F#rrc"tjSr )ridentity_transformrs r_transform_to_realr+s  ( ((rclt|j}tj||jSr )rbase_constraintrIndependentTransformreinterpreted_batch_ndimsrbase_transforms r_biject_to_independentr2s0z99:N  * * << rclt|j}tj||jSr )rr-rr.r/r0s r_transform_to_independentr4s0!*"<"<=N  * * << rc*tjSr )r ExpTransformr*s r_transform_to_positiver7s  " " $$rctjtjtj|jdgS)N)rComposeTransformr6AffineTransform lower_boundr*s r_transform_to_greater_thanr=s>  & &  # # %  & &z'='=q A  rctjtjtj|jdgS)N)rr:r6r; upper_boundr*s r_transform_to_less_thanrAs>  & &  # # %  & &z'='=r B  rct|jtxr|jdk(}t|jtxr|jdk(}|r|rt j S|j}|j|jz }t j t j t j||gS)Nrr9)rr<rr@rSigmoidTransformr:r;)r lower_is_0 upper_is_1locscales r_transform_to_intervalrHs :))73S 8N8NRS8S :))73S 8N8NRS8Sj**,,  C  " "Z%;%; ;E  & &  $ $ & (B(B3(NO rc*tjSr )rStickBreakingTransformr*s r_biject_to_simplexrK  , , ..rc*tjSr )rSoftmaxTransformr*s r_transform_to_simplexrOs  & & ((rc*tjSr )rLowerCholeskyTransformr*s r_transform_to_lower_choleskyrRrLrc*tjSr )rPositiveDefiniteTransformr*s r_transform_to_positive_definiterUs  / / 11rc*tjSr )rCorrCholeskyTransformr*s r_transform_to_corr_choleskyrXs  + + --rctj|jDcgc] }t|c}|j|j Scc}wr )r CatTransformcseqrdimlengthsrcs r_biject_to_catr` s<  " ")/!1/ASAS /A ctj|jDcgc] }t|c}|j|j Scc}wr )rrZr[rr\r]r^s r_transform_to_catrcs<  " "",//2Qa2JNNJDVDV 2ractj|jDcgc] }t|c}|jScc}wr )rStackTransformr[rr\r^s r_biject_to_stackrfs4  $ $)/!1/ /Actj|jDcgc] }t|c}|jScc}wr )rrer[rr\r^s r_transform_to_stackris4  $ $",//2Qa2JNN 2rgN)+r&torch.distributionsrr torch.typesr__all__rrrrrealr+ independentr2r4positive nonnegativer7 greater_thangreater_than_eqr= less_thanrAintervalhalf_open_intervalrHsimplexrKrOlower_choleskyrRpositive_definitepositive_semidefiniterU corr_choleskyrXcatr`rcstackrfrirrr~sAF8 F#F#R  !#  K$$% {''())&) K++,-{../0 K(() K++, {++, {../%0--*% K,,- K//0 {//0 {223411. K))* {,,-.+ K(() K223 {++, {5567-4*$ K''(/)/{**+),) {112/3/{445 {8892:62 K--. {001.2/. KOO$% {'(  K%%&' {(()*r