§
    Ÿ²Åj"  ã                   óX   — d dl mZ d dlZd dlmZmZ d dlmZ dgZ G d„ de¦  «        Z	dS )é    )ÚDictN)ÚCategoricalÚconstraints)ÚDistributionÚMixtureSameFamilyc                   ó  ‡ — e Zd ZU dZi Zeeej        f         e	d<   dZ
	 dˆ fd„	Zdˆ fd„	Zej        d„ ¦   «         Zed„ ¦   «         Zed	„ ¦   «         Zed
„ ¦   «         Ze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 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_constraintsFNc                 óà  •— || _         || _        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.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_shapeÚvalidate_args)Ú_mixture_distributionÚ_component_distributionÚ
isinstancer   Ú
ValueErrorr   r   ÚzipÚreversedÚlogitsÚshapeÚ_num_componentr   ÚlenÚ_event_ndimsÚsuperÚ__init__)ÚselfÚmixture_distributionÚcomponent_distributionr   ÚmdbsÚcdbsÚsize1Úsize2ÚkmÚkcr   Ú	__class__s              €úXC:\Projects\Historical\venv\Lib\site-packages\torch/distributions/mixture_same_family.pyr   zMixtureSameFamily.__init__9   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)   r/   zMixtureSameFamily.expandg   sÔ   ø€ Ý”j Ñ-Ô-ˆØ&¨$Ô*=Ð)?Ñ?ÐØ×(Ò(Õ):¸IÑFÔFˆØ&*Ô&B×&IÒ&IØñ'
ô '
ˆÔ#ð %)Ô$>×$EÒ$EÀkÑ$RÔ$RˆÔ!Ø!Ô0ˆÔØÔ,ˆÔØÔ1Ô=ˆÝÕ Ñ%Ô%×.Ò.Ø#°ÈEð 	/ñ 	
ô 	
ð 	
ð "Ô0ˆÔØˆ
r*   c                 ó   — | j         j        S ©N)r   Úsupport©r   s    r)   r6   zMixtureSameFamily.supportx   s   € ð Ô+Ô3Ð3r*   c                 ó   — | j         S r5   )r   r7   s    r)   r    z&MixtureSameFamily.mixture_distribution~   s   € àÔ)Ð)r*   c                 ó   — | j         S r5   )r   r7   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   r?   s     r)   rA   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    r?   r,   r@   r!   Úvariancer   rA   Ú_padÚpow)r   r?   Úmean_cond_varÚvar_cond_means       r)   rC   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;   )rD   r!   Úcdfr    r?   r,   r@   )r   ÚxÚcdf_xÚmix_probs       r)   rI   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;   )
r0   Ú_validate_samplerD   r!   Úlog_probr,   Úlog_softmaxr    r   Ú	logsumexp)r   rJ   Ú
log_prob_xÚlog_mix_probs       r)   rO   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)   rV   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   rJ   s     r)   rD   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   rW   r,   r-   r   )r   rJ   Ú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*   r5   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   Ústrr   Ú
ConstraintÚ__annotations__Úhas_rsampler   r/   Údependent_propertyr6   Úpropertyr    r!   rA   rC   rI   rO   r,   r-   rV   rD   r>   rn   Ú__classcell__)r(   s   @r)   r   r      sŽ  ø€ € € € € € ð(ð (ðR :<€O�T˜#˜{Ô5Ð5Ô6Ð;Ð;Ñ;Ø€Kð KOð,
ð ,
ð ,
ð ,
ð ,
ð ,
ð\ð ð ð ð ð ð" Ô#ð4ð 4ñ $Ô#ð4ð
 ð*ð *ñ „Xð*ð ð,ð ,ñ „Xð,ð ð
ð 
ñ „Xð
ð ð
-ð 
-ñ „Xð
-ð3ð 3ð 3ðBð Bð Bð #- %¤*¡,¤,ð /ð /ð /ð /ð23ð 3ð 3ðð ð ð=ð =ð =ð =ð =ð =ð =r*   )
Útypingr   r,   Útorch.distributionsr   r   Ú torch.distributions.distributionr   Ú__all__r   © r*   r)   ú<module>r      s’   ðà Ð Ð Ð Ð Ð à €€€Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9ð Ð
€ðL=ð L=ð L=ð L=ð L=˜ñ L=ô L=ð L=ð L=ð L=r*   