Ë
    M²j"  ã                   ó\   — d dl Z d dl mZ d dlmZmZ d dlmZ d dlmZ dgZ	 G d„ de«      Z
y)é    N)ÚTensor)ÚCategoricalÚconstraints)ÚMixtureSameFamilyConstraint)ÚDistributionÚMixtureSameFamilyc            	       ó>  ‡ — e Zd ZU dZi Zeeej                  f   e	d<   dZ
	 ddedededz  ddfˆ fd	„Zdˆ fd
„	Zej                   d„ «       Zedefd„«       Zedefd„«       Zedefd„«       Zedefd„«       Zd„ Zd„ Z ej6                  «       fd„Zd„ Zd„ Zd„ Zˆ xZ S )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                 óÒ  •— || _         || _        t        | j                   t        «      st	        d«      ‚t        | j                  t
        «      st	        d«      ‚| j                   j                  }| j                  j                  d d }t        t        |«      t        |«      «      D ](  \  }}|dk7  sŒ|dk7  sŒ||k7  sŒt	        d|› d|› d�«      ‚ | j                   j                  j                  d   }| j                  j                  d   }	|�|	�||	k7  rt	        d|› d	|	› d�«      ‚|| _        | j                  j                  }
t        |
«      | _        t        ‰| �A  ||
|¬
«       y )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              €úf/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/distributions/mixture_same_family.pyr"   zMixtureSameFamily.__init__;   sŒ  ø€ ð &:ˆÔ"Ø'=ˆÔ$ä˜$×4Ñ4´kÔBÜð?óð ô
 ˜$×6Ñ6¼ÔEÜð?óð ð ×)Ñ)×5Ñ5ˆØ×+Ñ+×7Ñ7¸¸Ð<ˆÜ¤¨£´¸³Ó?ò 	‰LˆE�5Ø˜‹z˜e q›j¨U°e«^Ü Ø:¸4¸&ð A$à$( 6¨ð,óð ð	ð ×'Ñ'×.Ñ.×4Ñ4°RÑ8ˆØ×)Ñ)×5Ñ5°bÑ9ˆØˆ>˜b˜n°°r²ÜØ4°R°Dð 9à�D˜ðóð ð
 !ˆÔà×2Ñ2×>Ñ>ˆÜ Ó,ˆÔÜ‰ÑàØ#Ø'ð	 	õ 	
ó    c                 ó¼  •— t        j                  |«      }|| j                  fz   }| j                  t        |«      }| j
                  j                  |«      |_        | j                  j                  |«      |_        | j                  |_        | j                  |_        |j
                  j                  }t        t        |�/  ||d¬«       | j                  |_        |S )NFr   )ÚtorchÚSizer   Ú_get_checked_instancer   r   Úexpandr   r    r   r!   r"   Ú_validate_args)r#   r   Ú	_instanceÚbatch_shape_compÚnewr   r*   s         €r+   r1   zMixtureSameFamily.expando   sÍ   ø€ Ü—j‘j Ó-ˆØ&¨$×*=Ñ*=Ð)?Ñ?ÐØ×(Ñ(Ô):¸IÓFˆØ&*×&BÑ&B×&IÑ&IØó'
ˆÔ#ð %)×$>Ñ$>×$EÑ$EÀkÓ$RˆÔ!Ø!×0Ñ0ˆÔØ×,Ñ,ˆÔØ×1Ñ1×=Ñ=ˆÜÔ Ñ.Ø#°ÈEð 	/ô 	
ð "×0Ñ0ˆÔØˆ
r,   c                 ó@   — t        | j                  j                  «      S ©N)r   r   Úsupport©r#   s    r+   r8   zMixtureSameFamily.support€   s   € ô +¨4×+GÑ+G×+OÑ+OÓPÐPr,   c                 ó   — | j                   S r7   )r   r9   s    r+   r   z&MixtureSameFamily.mixture_distribution…   s   € à×)Ñ)Ð)r,   c                 ó   — | j                   S r7   )r   r9   s    r+   r   z(MixtureSameFamily.component_distribution‰   s   € à×+Ñ+Ð+r,   c                 óÂ   — | j                  | j                  j                  «      }t        j                  || j
                  j                  z  d| j                  z
  ¬«      S ©Nr   ©Údim)Ú_pad_mixture_dimensionsr   Úprobsr.   Úsumr   Úmeanr    )r#   rA   s     r+   rC   zMixtureSameFamily.mean�   sP   € à×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜ�y‰yØ�D×/Ñ/×4Ñ4Ñ4¸"¸t×?PÑ?PÑ:Pô
ð 	
r,   c                 ó˜  — | j                  | j                  j                  «      }t        j                  || j
                  j                  z  d| j                  z
  ¬«      }t        j                  || j
                  j                  | j                  | j                  «      z
  j                  d«      z  d| j                  z
  ¬«      }||z   S )Nr   r>   g       @)r@   r   rA   r.   rB   r   Úvariancer    rC   Ú_padÚpow)r#   rA   Úmean_cond_varÚvar_cond_means       r+   rE   zMixtureSameFamily.variance”   s¬   € ð ×,Ñ,¨T×-FÑ-F×-LÑ-LÓMˆÜŸ	™	Ø�D×/Ñ/×8Ñ8Ñ8¸bÀ4×CTÑCTÑ>Tô
ˆô Ÿ	™	Ø�T×0Ñ0×5Ñ5¸¿	¹	À$Ç)Á)Ó8LÑL×QÑQÐRUÓVÑVØ�T×&Ñ&Ñ&ô
ˆð ˜}Ñ,Ð,r,   c                 óº   — | j                  |«      }| j                  j                  |«      }| j                  j                  }t        j                  ||z  d¬«      S r=   )rF   r   Úcdfr   rA   r.   rB   )r#   ÚxÚcdf_xÚmix_probs       r+   rK   zMixtureSameFamily.cdf¡   sL   € Ø�I‰I�a‹LˆØ×+Ñ+×/Ñ/°Ó2ˆØ×,Ñ,×2Ñ2ˆä�y‰y˜ Ñ)¨rÔ2Ð2r,   c                 ó  — | j                   r| j                  |«       | j                  |«      }| j                  j	                  |«      }t        j                  | j                  j                  d¬«      }t        j                  ||z   d¬«      S r=   )
r2   Ú_validate_samplerF   r   Úlog_probr.   Úlog_softmaxr   r   Ú	logsumexp)r#   rL   Ú
log_prob_xÚlog_mix_probs       r+   rQ   zMixtureSameFamily.log_prob¨   su   € Ø×ÒØ×!Ñ! !Ô$Ø�I‰I�a‹LˆØ×0Ñ0×9Ñ9¸!Ó<ˆ
Ü×(Ñ(Ø×%Ñ%×,Ñ,°"ô
ˆô �‰˜z¨LÑ8¸bÔAÐAr,   c           
      ó�  — t        j                  «       5  t        |«      }t        | j                  «      }||z   }| j                  }| j
                  j                  |«      }|j                  }| j                  j                  |«      }|j                  |t        j                  dgt        |«      dz   z  «      z   «      }	|	j                  t        j                  dgt        |«      z  «      t        j                  dg«      z   |z   «      }	t        j                  |||	«      }
|
j                  |«      cd d d «       S # 1 sw Y   y xY w)Nr   )r.   Úno_gradr   r   r   r   Úsampler   r   Úreshaper/   ÚrepeatÚgatherÚsqueeze)r#   Úsample_shapeÚ
sample_lenÚ	batch_lenÚ
gather_dimÚesÚ
mix_sampleÚ	mix_shapeÚcomp_samplesÚmix_sample_rÚsampless              r+   rX   zMixtureSameFamily.sample²   s  € Ü�]‰]‹_ñ 	/Ü˜\Ó*ˆJÜ˜D×,Ñ,Ó-ˆIØ# iÑ/ˆJØ×!Ñ!ˆBð ×2Ñ2×9Ñ9¸,ÓGˆJØ"×(Ñ(ˆIð  ×6Ñ6×=Ñ=¸lÓKˆLð &×-Ñ-ØœEŸJ™J¨ s¬c°"«g¸©kÑ':Ó;Ñ;óˆLð (×.Ñ.Ü—
‘
˜A˜3¤ Y£Ñ/Ó0´5·:±:¸q¸c³?ÑBÀRÑGóˆLô —l‘l <°¸\ÓJˆGØ—?‘? :Ó.÷-	/÷ 	/ò 	/ús   •DD<Ä<Ec                 ó>   — |j                  d| j                  z
  «      S )Nr   )Ú	unsqueezer    )r#   rL   s     r+   rF   zMixtureSameFamily._padË   s   € Ø�{‰{˜2 × 1Ñ 1Ñ1Ó2Ð2r,   c                 óN  — t        | j                  «      }t        | j                  j                  «      }|dk(  rdn||z
  }|j                  }|j	                  |d d t        j                  |dgz  «      z   |dd  z   t        j                  | j                  dgz  «      z   «      }|S )Nr   r   r   )r   r   r   r   rY   r.   r/   r    )r#   rL   Údist_batch_ndimsÚcat_batch_ndimsÚ	pad_ndimsÚxss         r+   r@   z)MixtureSameFamily._pad_mixture_dimensionsÎ   s©   € Ü˜t×/Ñ/Ó0ÐÜ˜d×7Ñ7×CÑCÓDˆØ(¨AÒ-‘AÐ3CÀoÑ3Uˆ	Ø�W‰WˆØ�I‰IØˆs�ˆGÜ�j‰j˜ a S™Ó)ñ*à��ˆgñô �j‰j˜×*Ñ*¨a¨SÑ0Ó1ñ2ó
ˆð ˆr,   c                 óJ   — d| j                   › d| j                  › �}d|z   dz   S )Nz
  z,
  zMixtureSameFamily(r   )r   r   )r#   Úargs_strings     r+   Ú__repr__zMixtureSameFamily.__repr__Û   s7   € à�4×,Ñ,Ð-¨U°4×3NÑ3NÐ2OÐPð 	ð )¨;Ñ6¸Ñ<Ð<r,   r7   )!Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   ÚdictÚstrr   Ú
ConstraintÚ__annotations__Úhas_rsampler   r   Úboolr"   r1   Údependent_propertyr8   Úpropertyr   r   r   rC   rE   rK   rQ   r.   r/   rX   rF   r@   rp   Ú__classcell__)r*   s   @r+   r   r      s  ø… ñ(ðT :<€O�T˜#˜{×5Ñ5Ð5Ñ6Ó;Ø€Kð &*ñ	2
à)ð2
ð !-ð2
ð ˜d‘{ð	2
ð
 
õ2
õhð" ×#Ñ#ñQó $ðQð ð* kò *ó ð*ð ð,¨ò ,ó ð,ð ð
�fò 
ó ð
ð ð
-˜&ò 
-ó ð
-ò3òBð #- %§*¡*£,ó /ò23òö=r,   )r.   r   Útorch.distributionsr   r   Útorch.distributions.constraintsr   Ú torch.distributions.distributionr   Ú__all__r   © r,   r+   ú<module>rƒ      s.   ðó Ý ß 8Ý GÝ 9ð Ð
€ôR=˜õ R=r,   