
    9i!                     h    S SK Jr  S SKrS SKJr  S SKJrJr  S SKJr  S SK	J
r
  S/r " S S\
5      rg)	    )OptionalN)Tensor)Categoricalconstraints)MixtureSameFamilyConstraint)DistributionMixtureSameFamilyc            	       Z  ^  \ rS rSr% Sr0 r\\\R                  4   \
S'   Sr SS\S\S\\   S	S4U 4S
 jjjrSU 4S jjr\R$                  S 5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       r\S	\4S j5       rS rS r\R:                  " 5       4S jrS rS r S r!Sr"U =r#$ )r	      a;  
The `MixtureSameFamily` distribution implements a (batch of) mixture
distribution where all component are from different parameterizations of
the same distribution type. It is parameterized by a `Categorical`
"selecting distribution" (over `k` component) and a component
distribution, i.e., a `Distribution` with a rightmost batch shape
(equal to `[k]`) which indexes each (batch of) component.

Examples::

    >>> # xdoctest: +SKIP("undefined vars")
    >>> # Construct Gaussian Mixture Model in 1D consisting of 5 equally
    >>> # weighted normal distributions
    >>> mix = D.Categorical(torch.ones(5,))
    >>> comp = D.Normal(torch.randn(5,), torch.rand(5,))
    >>> gmm = MixtureSameFamily(mix, comp)

    >>> # Construct Gaussian Mixture Model in 2D consisting of 5 equally
    >>> # weighted bivariate normal distributions
    >>> mix = D.Categorical(torch.ones(5,))
    >>> comp = D.Independent(D.Normal(
    ...          torch.randn(5,2), torch.rand(5,2)), 1)
    >>> gmm = MixtureSameFamily(mix, comp)

    >>> # Construct a batch of 3 Gaussian Mixture Models in 2D each
    >>> # consisting of 5 random weighted bivariate normal distributions
    >>> mix = D.Categorical(torch.rand(3,5))
    >>> comp = D.Independent(D.Normal(
    ...         torch.randn(3,5,2), torch.rand(3,5,2)), 1)
    >>> gmm = MixtureSameFamily(mix, comp)

Args:
    mixture_distribution: `torch.distributions.Categorical`-like
        instance. Manages the probability of selecting component.
        The number of categories must match the rightmost batch
        dimension of the `component_distribution`. Must have either
        scalar `batch_shape` or `batch_shape` matching
        `component_distribution.batch_shape[:-1]`
    component_distribution: `torch.distributions.Distribution`-like
        instance. Right-most batch dimension indexes component.
arg_constraintsFNmixture_distributioncomponent_distributionvalidate_argsreturnc                   > Xl         X l        [        U R                   [        5      (       d  [	        S5      e[        U R                  [
        5      (       d  [	        S5      eU R                   R                  nU R                  R                  S S n[        [        U5      [        U5      5       H,  u  pgUS:w  d  M  US:w  d  M  Xg:w  d  M  [	        SU SU S35      e   U R                   R                  R                  S   nU R                  R                  S   n	Ub  U	b  X:w  a  [	        SU S	U	 S35      eXl        U R                  R                  n
[        U
5      U l        [        TU ]A  XZUS
9  g )NzU The Mixture distribution needs to be an  instance of torch.distributions.CategoricalzUThe Component distribution need to be an instance of torch.distributions.Distribution   z$`mixture_distribution.batch_shape` (z>) is not compatible with `component_distribution.batch_shape`()z"`mixture_distribution component` (z;) does not equal `component_distribution.batch_shape[-1]` (batch_shapeevent_shaper   )_mixture_distribution_component_distribution
isinstancer   
ValueErrorr   r   zipreversedlogitsshape_num_componentr   len_event_ndimssuper__init__)selfr   r   r   mdbscdbssize1size2kmkcr   	__class__s              g/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/mixture_same_family.pyr$   MixtureSameFamily.__init__<   s~    &:"'=$$44kBB? 
 $66EE?  ))55++77<?LEzeqjU^ :4& A$$(6,  @ ''..44R8))55b9>bn4RD 9D 
 !22>>,] 	 	
    c                   > [         R                  " U5      nXR                  4-   nU R                  [        U5      nU R
                  R                  U5      Ul        U R                  R                  U5      Ul        U R                  Ul        U R                  Ul        UR
                  R                  n[        [        U]/  XSS9  U R                  Ul        U$ )NFr   )torchSizer    _get_checked_instancer	   r   expandr   r"   r   r#   r$   _validate_args)r%   r   	_instancebatch_shape_compnewr   r,   s         r-   r4   MixtureSameFamily.expandm   s    jj-&*=*=)??(():IF&*&B&B&I&I'
# %)$>$>$E$Ek$R!!00,,11==.#E 	/ 	
 "00
r/   c                 @    [        U R                  R                  5      $ N)r   r   supportr%   s    r-   r<   MixtureSameFamily.support~   s    *4+G+G+O+OPPr/   c                     U R                   $ r;   )r   r=   s    r-   r   &MixtureSameFamily.mixture_distribution   s    )))r/   c                     U R                   $ r;   )r   r=   s    r-   r   (MixtureSameFamily.component_distribution   s    +++r/   c                     U R                  U R                  R                  5      n[        R                  " XR
                  R                  -  SU R                  -
  S9$ Nr   dim)_pad_mixture_dimensionsr   probsr1   sumr   meanr"   )r%   rH   s     r-   rJ   MixtureSameFamily.mean   sN    ,,T-F-F-L-LMyy//444"t?P?P:P
 	
r/   c                    U R                  U R                  R                  5      n[        R                  " XR
                  R                  -  SU R                  -
  S9n[        R                  " XR
                  R                  U R                  U R                  5      -
  R                  S5      -  SU R                  -
  S9nX#-   $ )Nr   rE   g       @)rG   r   rH   r1   rI   r   variancer"   rJ   _padpow)r%   rH   mean_cond_varvar_cond_means       r-   rM   MixtureSameFamily.variance   s     ,,T-F-F-L-LM		//888b4CTCT>T
 		0055		$))8LLQQRUVVT&&&
 ,,r/   c                     U R                  U5      nU R                  R                  U5      nU R                  R                  n[
        R                  " X#-  SS9$ rD   )rN   r   cdfr   rH   r1   rI   )r%   xcdf_xmix_probs       r-   rT   MixtureSameFamily.cdf   sJ    IIaL++//2,,22yy)r22r/   c                    U R                   (       a  U R                  U5        U R                  U5      nU R                  R	                  U5      n[
        R                  " U R                  R                  SS9n[
        R                  " X#-   SS9$ rD   )
r5   _validate_samplerN   r   log_probr1   log_softmaxr   r   	logsumexp)r%   rU   
log_prob_xlog_mix_probs       r-   r[   MixtureSameFamily.log_prob   ss    !!!$IIaL0099!<
((%%,,"
 z8bAAr/   c           
         [         R                  " 5          [        U5      n[        U R                  5      nX#-   nU R                  nU R
                  R                  U5      nUR                  nU R                  R                  U5      nUR                  U[         R                  " S/[        U5      S-   -  5      -   5      n	U	R                  [         R                  " S/[        U5      -  5      [         R                  " S/5      -   U-   5      n	[         R                  " XU	5      n
U
R                  U5      sS S S 5        $ ! , (       d  f       g = f)Nr   )r1   no_gradr!   r   r   r   sampler   r   reshaper2   repeatgathersqueeze)r%   sample_shape
sample_len	batch_len
gather_dimes
mix_sample	mix_shapecomp_samplesmix_sample_rsampless              r-   rc   MixtureSameFamily.sample   s
   ]]_\*JD,,-I#/J!!B 2299,GJ"((I  66==lKL &--EJJsc"gk':;;L (..

A3Y/05::qc?BRGL ll<\JG??:.- __s   DD??
Ec                 >    UR                  SU R                  -
  5      $ )Nr   )	unsqueezer"   )r%   rU   s     r-   rN   MixtureSameFamily._pad   s    {{2 1 1122r/   c                 R   [        U R                  5      n[        U R                  R                  5      nUS:X  a  SOX#-
  nUR                  nUR	                  US S [
        R                  " US/-  5      -   USS  -   [
        R                  " U R                  S/-  5      -   5      nU$ )Nr   r   r   )r!   r   r   r   rd   r1   r2   r"   )r%   rU   dist_batch_ndimscat_batch_ndims	pad_ndimsxss         r-   rG   )MixtureSameFamily._pad_mixture_dimensions   s    t//0d77CCD(A-A3C3U	WWIIsGjjaS)*g jj**aS012
 r/   c                 J    SU R                    SU R                   3nSU-   S-   $ )Nz
  z,
  zMixtureSameFamily(r   )r   r   )r%   args_strings     r-   __repr__MixtureSameFamily.__repr__   s7    4,,-U43N3N2OP 	 );6<<r/   )r   r"   r   r    r;   )$__name__
__module____qualname____firstlineno____doc__r   dictstrr   
Constraint__annotations__has_rsampler   r   r   boolr$   r4   dependent_propertyr<   propertyr   r   r   rJ   rM   rT   r[   r1   r2   rc   rN   rG   r~   __static_attributes____classcell__)r,   s   @r-   r	   r	      s    (T :<OT#{5556;K )-	/
)/
 !-/
  ~	/

 
/
 /
b" ##Q $Q *k * * , , , 
f 
 
 
-& 
- 
-3B #(**, /23= =r/   )typingr   r1   r   torch.distributionsr   r   torch.distributions.constraintsr    torch.distributions.distributionr   __all__r	    r/   r-   <module>r      s/       8 G 9 
N= N=r/   