L i=1vddlZddlmZddlmZddlZddlmZddlmZddl m Z ddl m Z dgZ Gd dZy) N)Optional) deprecated)Tensor) constraints) lazy_property)_size Distributionc <eZdZdZdZdZdZededdfdZ e je jdfde jd e jd e eddffd Z d'defd Zede jfd Zede jfdZedeeej,ffdZede ej,fdZedefdZedefdZedefdZedefdZe jfdedefdZe jfdedefdZe de!de"defdZ#dedefdZ$dedefdZ%dedefdZ&d(dedefd Z'defd!Z(defd"Z)e jfdede jfd#Z*deddfd$Z+d'd%Z,defd&Z-xZ.S))r aS Distribution is the abstract base class for probability distributions. Args: batch_shape (torch.Size): The shape over which parameters are batched. event_shape (torch.Size): The shape of a single sample (without batching). validate_args (bool, optional): Whether to validate arguments. Default: None. FTvaluereturnNc.|dvrt|t_y)a Sets whether validation is enabled or disabled. The default behavior mimics Python's ``assert`` statement: validation is on by default, but is disabled if Python is run in optimized mode (via ``python -O``). Validation may be expensive, so you may want to disable it once a model is working. Args: value (bool): Whether to enable validation. )TFN) ValueErrorr _validate_args)r s f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/distribution.pyset_default_validate_argsz&Distribution.set_default_validate_argss  % &+ # batch_shape event_shape validate_argsc||_||_|||_|jr |j}|jD]\}}tj|r||jvr$ttt||trNt||}|j!|}t#j$|rt'd|dt|j(dt+|j,dt/|dt/|d | t0 |ey#t$r.i}t j |jddzdzY,wxYw) Nz$ does not define `arg_constraints`. zAPlease set `arg_constraints = {}` or initialize the distribution z2with `validate_args=False` to turn off validation.zExpected parameter z ( of shape z) of distribution z to satisfy the constraint , but found invalid values: ) _batch_shape _event_shaperarg_constraintsNotImplementedErrorwarningswarn __class__itemsr is_dependent__dict__ isinstancegetattrtyperchecktorch _is_all_truer__name__tupleshapereprsuper__init__) selfrrrrparam constraintr validrs rr.zDistribution.__init__/sw ('  $"/D     "&"6"6&5%:%:%< !z++J7 -*DJ. 3e,"((/))%0$-eW5 K001E%++ " 1' "$ ~~&&JKYZJK s D((3EEct)a/ Returns a new distribution instance (or populates an existing instance provided by a derived class) with batch dimensions expanded to `batch_shape`. This method calls :class:`~torch.Tensor.expand` on the distribution's parameters. As such, this does not allocate new memory for the expanded distribution instance. Additionally, this does not repeat any args checking or parameter broadcasting in `__init__.py`, when an instance is first created. Args: batch_shape (torch.Size): the desired expanded size. _instance: new instance provided by subclasses that need to override `.expand`. Returns: New distribution instance with batch dimensions expanded to `batch_size`. r)r/r _instances rexpandzDistribution.expandVs &"!rc|jS)zF Returns the shape over which parameters are batched. )rr/s rrzDistribution.batch_shapek    rc|jS)zJ Returns the shape of a single sample (without batching). )rr8s rrzDistribution.event_shaperr9rct)a  Returns a dictionary from argument names to :class:`~torch.distributions.constraints.Constraint` objects that should be satisfied by each argument of this distribution. Args that are not tensors need not appear in this dict. r4r8s rrzDistribution.arg_constraintsy "!rct)z Returns a :class:`~torch.distributions.constraints.Constraint` object representing this distribution's support. r4r8s rsupportzDistribution.support "!rct)z7 Returns the mean of the distribution. r4r8s rmeanzDistribution.mean "!rc2t|jd)z7 Returns the mode of the distribution. z does not implement mode)rrr8s rmodezDistribution.modes "T^^$44L"MNNrct)z; Returns the variance of the distribution. r4r8s rvariancezDistribution.variancerBrc6|jjS)zE Returns the standard deviation of the distribution. )rFsqrtr8s rstddevzDistribution.stddevs }}!!##r sample_shapecxtj5|j|cdddS#1swYyxYw)z Generates a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched. N)r'no_gradrsampler/rJs rsamplezDistribution.samples. ]]_ .<< - . . .s09ct)z Generates a sample_shape shaped reparameterized sample or sample_shape shaped batch of reparameterized samples if the distribution parameters are batched. r4rNs rrMzDistribution.rsampler?rz=`sample_n(n)` will be deprecated. Use `sample((n,))` instead.)categoryncL|jtj|fS)zq Generates n samples or n batches of samples if the distribution parameters are batched. )rOr'Size)r/rRs rsample_nzDistribution.sample_ns{{5::qd+,,rct)z Returns the log of the probability density/mass function evaluated at `value`. Args: value (Tensor): r4r/r s rlog_probzDistribution.log_probr<rct)z Returns the cumulative density/mass function evaluated at `value`. Args: value (Tensor): r4rWs rcdfzDistribution.cdfr<rct)z Returns the inverse cumulative density/mass function evaluated at `value`. Args: value (Tensor): r4rWs ricdfzDistribution.icdfr<rr6ct)ar Returns tensor containing all values supported by a discrete distribution. The result will enumerate over dimension 0, so the shape of the result will be `(cardinality,) + batch_shape + event_shape` (where `event_shape = ()` for univariate distributions). Note that this enumerates over all batched tensors in lock-step `[[0, 0], [1, 1], ...]`. With `expand=False`, enumeration happens along dim 0, but with the remaining batch dimensions being singleton dimensions, `[[0], [1], ..`. To iterate over the full Cartesian product use `itertools.product(m.enumerate_support())`. Args: expand (bool): whether to expand the support over the batch dims to match the distribution's `batch_shape`. Returns: Tensor iterating over dimension 0. r4)r/r6s renumerate_supportzDistribution.enumerate_supports ,"!rct)z Returns entropy of distribution, batched over batch_shape. Returns: Tensor of shape batch_shape. r4r8s rentropyzDistribution.entropys "!rcHtj|jS)z Returns perplexity of distribution, batched over batch_shape. Returns: Tensor of shape batch_shape. )r'expr`r8s r perplexityzDistribution.perplexitysyy((rct|tjstj|}tj||jz|jzS)ax Returns the size of the sample returned by the distribution, given a `sample_shape`. Note, that the batch and event shapes of a distribution instance are fixed at the time of construction. If this is empty, the returned shape is upcast to (1,). Args: sample_shape (torch.Size): the size of the sample to be drawn. )r#r'rTrrrNs r_extended_shapezDistribution._extended_shape sE, 3 ::l3Lzz,):)::T=N=NNOOrc t|tjs tdt |j t |j z }|j |d|j k7r*td|j d|j d|j }|j|j z}tt|t|D](\}}|dk7s |dk7s||k7std|d|d |j}|J|j|}tj |sRtd t#|j$d t'|j(d t+|dt+|d| y#t$r+tj|jdd zd zYywxYw)a Argument validation for distribution methods such as `log_prob`, `cdf` and `icdf`. The rightmost dimensions of a value to be scored via these methods must agree with the distribution's batch and event shapes. Args: value (Tensor): the tensor whose log probability is to be computed by the `log_prob` method. Raises ValueError: when the rightmost dimensions of `value` do not match the distribution's batch and event shapes. z/The value argument to log_prob must be a TensorNz5The right-most size of value must match event_shape: z vs .z9Value is not broadcastable with batch_shape+event_shape: z% does not define `support` to enable z;sample validation. Please initialize the distribution with z-`validate_args=False` to turn off validation.zExpected value argument (rz) to be within the support (z) of the distribution r)r#r'rrlensizerrzipreversedr>rrrrr&r(r%r)r*r+r,) r/r event_dim_start actual_shapeexpected_shapeijr>r2s r_validate_samplezDistribution._validate_samples%.NO Oejjl+c$2C2C.DD ::<( )T->-> >G ~UYZ^ZkZkYllmn zz| **T->->>.0HI DAqAv!q&Q!V OP\~]abpaqqrs   llG""" e$!!%(K(()E%++4F3GH--1']O<''+Dzl3..3W 6 )#  MM>>""GHOPAB    s F1GGc|Rt|j|jk7r0td|jjd|jd||j t|S|S)Nz Subclass z of zR that defines a custom __init__ method must also define a custom .expand() method.)r%r.rrr)__new__)r/clsr5s r_get_checked_instancez"Distribution._get_checked_instanceJsp  d!4!4 !D%DNN334DG>> ,5+rrArDrFrIrOrMr FutureWarningintrUrXrZr\r^r`rcrerrrvr __classcell__)rs@rr r sK!N,,$,,$#-%**,",%**,(, %ZZ%ZZ% ~ %  %N"%"*!UZZ!! !UZZ!! "c;+A+A&A!B"""+"8"89"""f"" OfOO "&"" $$$ ,65::<.5.F.-7EJJL"E"V"G-#-&- -"f""""F""&"V""""0"")F)5?EJJL PE PUZZ P0f00dLA#Ar)rtypingrtyping_extensionsrr'rtorch.distributionsrtorch.distributions.utilsr torch.typesr__all__r rrrs5( +3  JAJAr