L ivddlmZmZmZddlZddlmZmZddlmZddl m Z ddl m Z ddl mZdgZed e ZGd de eeZy) )GenericOptionalTypeVarN)SizeTensor) constraints) Distribution)_sum_rightmost)_size IndependentD)boundc eZdZUdZiZeeejfe d<e e d< dde de de e ddffd Zdfd Zede fd Zede fd Zej&d ZedefdZedefdZedefdZej4fdefdZej4fdedefdZdZdZddZ dZ!xZ"S)r a Reinterprets some of the batch dims of a distribution as event dims. This is mainly useful for changing the shape of the result of :meth:`log_prob`. For example to create a diagonal Normal distribution with the same shape as a Multivariate Normal distribution (so they are interchangeable), you can:: >>> from torch.distributions.multivariate_normal import MultivariateNormal >>> from torch.distributions.normal import Normal >>> loc = torch.zeros(3) >>> scale = torch.ones(3) >>> mvn = MultivariateNormal(loc, scale_tril=torch.diag(scale)) >>> [mvn.batch_shape, mvn.event_shape] [torch.Size([]), torch.Size([3])] >>> normal = Normal(loc, scale) >>> [normal.batch_shape, normal.event_shape] [torch.Size([3]), torch.Size([])] >>> diagn = Independent(normal, 1) >>> [diagn.batch_shape, diagn.event_shape] [torch.Size([]), torch.Size([3])] Args: base_distribution (torch.distributions.distribution.Distribution): a base distribution reinterpreted_batch_ndims (int): the number of batch dims to reinterpret as event dims arg_constraints base_distNbase_distributionreinterpreted_batch_ndims validate_argsreturncd|t|jkDr$td|dt|j|j|jz}|t|jz}|dt||z }|t||z d}||_||_t ||||y)NzQExpected reinterpreted_batch_ndims <= len(base_distribution.batch_shape), actual z vs r)len batch_shape ValueError event_shaperrsuper__init__) selfrrrshape event_dimrr __class__s e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/independent.pyrzIndependent.__init__3s %s+<+H+H'I I34D=N=Z=Z9[8\^ (336G6S6SS2S9J9V9V5WW 4c%j945 CJ245 *)B& kOcV|jt|}tj|}|jj ||j d|jz|_|j|_tt|'||j d|j|_ |S)NFr) _get_checked_instancer torchrrexpandrrrr_validate_args)rr _instancenewr!s r"r'zIndependent.expandFs((i@jj- -- $**+KT-K-KL L  )-(F(F% k3( )) ) "00 r#c.|jjSN)r has_rsamplers r"r-zIndependent.has_rsampleSs~~)))r#cN|jdkDry|jjS)NrF)rrhas_enumerate_supportr.s r"r0z!Independent.has_enumerate_supportWs#  ) )A -~~333r#c|jj}|jr tj||j}|Sr,)rsupportrr independent)rresults r"r2zIndependent.support]s7''  ) ) ,,VT5S5STF r#c.|jjSr,)rmeanr.s r"r6zIndependent.meand~~"""r#c.|jjSr,)rmoder.s r"r9zIndependent.modehr7r#c.|jjSr,)rvariancer.s r"r;zIndependent.variancels~~&&&r#c8|jj|Sr,)rsampler sample_shapes r"r=zIndependent.sampleps~~$$\22r#r?c8|jj|Sr,)rrsampler>s r"rAzIndependent.rsampless~~%%l33r#cd|jj|}t||jSr,)rlog_probr r)rvaluerCs r"rCzIndependent.log_probvs)>>**51h(F(FGGr#cb|jj}t||jSr,)rentropyr r)rrFs r"rFzIndependent.entropyzs'..((*gt'E'EFFr#cn|jdkDr td|jj|S)Nrz5Enumeration over cartesian product is not implemented)r')rNotImplementedErrorrenumerate_support)rr's r"rIzIndependent.enumerate_support~s:  ) )A -%G ~~//v/>>r#cj|jjd|jd|jdzS)N(z, ))r!__name__rrr.s r"__repr__zIndependent.__repr__s8 NN # #$..!D$B$B#C1E F r#r,)T)#rM __module__ __qualname____doc__rdictstrr Constraint__annotations__r intrboolrr'propertyr-r0dependent_propertyr2rr6r9r;r&rr=r rArCrFrIrN __classcell__)r!s@r"r r sW::rbsH-- +94 / C|$w , w r#