§
    ŠŠtj""  ã                   ód   — 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
dS )é    N)ÚTensor)ÚCategoricalÚconstraints)ÚMixtureSameFamilyConstraint)ÚDistributionÚMixtureSameFamilyc            	       óJ  ‡ — 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j        ¦   «         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 components are from different parameterizations of
    the same distribution type. It is parameterized by a `Categorical`
    "selecting distribution" (over `k` components) 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 components.
            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k    r"|dk    r||k    rt	          d|› d|› d�¦  «        ‚Œ.| j         j	        j
        d         }| j        j        d         }	|�|	�||	k    rt	          d|› d	|	› d�¦  «        ‚|| _        | j        j        }
t          |
¦  «        | _        t          ¦   «                              ||
|¬
¦  «         d S )NzU The Mixture distribution needs to be an  instance of torch.distributions.CategoricalzVThe Component distribution needs 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              €úe/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/distributions/mixture_same_family.pyr"   zMixtureSameFamily.__init__;   sÌ  ø€ ð &:ˆÔ"Ø'=ˆÔ$å˜$Ô4µkÑBÔBð 	Ýð?ñô ð õ
 ˜$Ô6½ÑEÔEð 	Ýð?ñô ð ð Ô)Ô5ˆØÔ+Ô7¸¸¸Ô<ˆÝ¥¨¡¤µ¸±´Ñ?Ô?ð 	ð 	‰LˆE�5Ø˜Šzˆz˜e qšj˜j¨U°eª^¨^Ý ð,¸4ð ,ð ,à$(ð,ð ,ð ,ñô ð øð Ô'Ô.Ô4°RÔ8ˆØÔ)Ô5°bÔ9ˆØˆ>˜b˜n°°r²°Ýð°Rð ð àðð ð ñô ð ð
 !ˆÔàÔ2Ô>ˆÝ Ñ,Ô,ˆÔÝ‰Œ×ÒàØ#Ø'ð	 	ñ 	
ô 	
ð 	
ð 	
ð 	
ó    c                 ó®  •— t          j        |¦  «        }|| j        fz   }|                      t          |¦  «        }| 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 Ñ-Ô-ˆØ&¨$Ô*=Ð)?Ñ?ÐØ×(Ò(Õ):¸IÑFÔFˆØ&*Ô&B×&IÒ&IØñ'
ô '
ˆÔ#ð %)Ô$>×$EÒ$EÀkÑ$RÔ$RˆÔ!Ø!Ô0ˆÔØÔ,ˆÔØÔ1Ô=ˆÝÕ Ñ%Ô%×.Ò.Ø#°ÈEð 	/ñ 	
ô 	
ð 	
ð "Ô0ˆÔØˆ
r,   c                 ó4   — t          | j        j        ¦  «        S ©N)r   r   Úsupport©r#   s    r+   r8   zMixtureSameFamily.support€   s   € õ +¨4Ô+GÔ+OÑPÔ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        ¦  «        }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�   sL   € à×,Ò,¨TÔ-FÔ-LÑMÔMˆÝŒyØ�DÔ/Ô4Ñ4¸"¸tÔ?PÑ:Pð
ñ 
ô 
ð 	
r,   c                 óR  — |                       | j        j        ¦  «        }t          j        || j        j        z  d| j        z
  ¬¦  «        }t          j        || j        j        |  	                    | j        ¦  «        z
   
                    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Ô-LÑMÔMˆÝœ	Ø�DÔ/Ô8Ñ8¸bÀ4ÔCTÑ>Tð
ñ 
ô 
ˆõ œ	Ø�TÔ0Ô5¸¿	º	À$Ä)Ñ8LÔ8LÑL×QÒQÐRUÑVÔVÑVØ�TÔ&Ñ&ð
ñ 
ô 
ˆð ˜}Ñ,Ð,r,   c                 óª   — |                       |¦  «        }| 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ŒLˆØÔ+×/Ò/°Ñ2Ô2ˆØÔ,Ô2ˆåŒy˜ Ñ)¨rÐ2Ñ2Ô2Ð2r,   c                 ó
  — | j         r|                      |¦  «         |                      |¦  «        }| 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¨   s„   € ØÔð 	%Ø×!Ò! !Ñ$Ô$Ð$Ø�IŠI�a‰LŒLˆØÔ0×9Ò9¸!Ñ<Ô<ˆ
ÝÔ(ØÔ%Ô,°"ð
ñ 
ô 
ˆõ Œ˜z¨LÑ8¸bÐAÑAÔAÐAr,   c           
      ó¤  — t          j        ¦   «         5  t          |¦  «        }t          | j        ¦  «        }||z   }| j        }| j                             |¦  «        }|j        }| j                             |¦  «        }| 	                    |t          j
        dgt          |¦  «        dz   z  ¦  «        z   ¦  «        }	|	                     t          j
        dgt          |¦  «        z  ¦  «        t          j
        dg¦  «        z   |z   ¦  «        }	t          j        |||	¦  «        }
|
                     |¦  «        cd d d ¦  «         S # 1 swxY w Y   d S )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×9Ò9¸,ÑGÔGˆJØ"Ô(ˆIð  Ô6×=Ò=¸lÑKÔKˆLð &×-Ò-Ø�EœJ¨ s­c°"©g¬g¸©kÑ':Ñ;Ô;Ñ;ñô ˆLð (×.Ò.Ý”
˜A˜3¥ Y¡¤Ñ/Ñ0Ô0µ5´:¸q¸c±?´?ÑBÀRÑGñô ˆLõ ”l <°¸\ÑJÔJˆGØ—?’? :Ñ.Ô.ð-	/ð 	/ð 	/ð 	/ñ 	/ô 	/ð 	/ð 	/ð 	/ð 	/ð 	/ð 	/øøøð 	/ð 	/ð 	/ð 	/ð 	/ð 	/s   ”D$EÅE	ÅE	c                 ó<   — |                      d| j        z
  ¦  «        S )Nr   )Ú	unsqueezer    )r#   rL   s     r+   rF   zMixtureSameFamily._padË   s   € Ø�{Š{˜2 Ô 1Ñ1Ñ2Ô2Ð2r,   c                 óF  — t          | j        ¦  «        }t          | j        j        ¦  «        }|dk    rdn||z
  }|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Ô0ÐÝ˜dÔ7ÔCÑDÔDˆØ(¨AÒ-Ð-�A�AÐ3CÀoÑ3Uˆ	ØŒWˆØ�IŠIØˆs�ˆsŒGÝŒj˜ a S™Ñ)Ô)ñ*à���Œgñõ Œj˜Ô*¨a¨SÑ0Ñ1Ô1ñ2ñ
ô 
ˆð ˆr,   c                 ó6   — d| j         › d| j        › �}d|z   dz   S )Nz
  z,
  zMixtureSameFamily(r   )r   r   )r#   Úargs_strings     r+   Ú__repr__zMixtureSameFamily.__repr__Û   s2   € àP�4Ô,ÐPÐP°4Ô3NÐPÐ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Ô6Ð;Ð;Ñ;Ø€Kð &*ð	2
ð 2
à)ð2
ð !-ð2
ð ˜d‘{ð	2
ð
 
ð2
ð 2
ð 2
ð 2
ð 2
ð 2
ðhð ð ð ð ð ð" Ô#ðQð Qñ $Ô#ðQð ð* kð *ð *ð *ñ „Xð*ð ð,¨ð ,ð ,ð ,ñ „Xð,ð ð
�fð 
ð 
ð 
ñ „Xð
ð ð
-˜&ð 
-ð 
-ð 
-ñ „Xð
-ð3ð 3ð 3ðBð Bð Bð #- %¤*¡,¤,ð /ð /ð /ð /ð23ð 3ð 3ðð ð ð=ð =ð =ð =ð =ð =ð =r,   )r.   r   Útorch.distributionsr   r   Útorch.distributions.constraintsr   Ú torch.distributions.distributionr   Ú__all__r   © r,   r+   ú<module>rƒ      s¦   ðð €€€Ø Ð Ð Ð Ð Ð Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø GÐ GÐ GÐ GÐ GÐ GØ 9Ð 9Ð 9Ð 9Ð 9Ð 9ð Ð
€ðR=ð R=ð R=ð R=ð R=˜ñ R=ô R=ð R=ð R=ð R=r,   