o
    Men                     @   s"   d dl mZ G dd dejZdS )    )distributionc                       sb   e Zd ZdZ fddZedd Zedd Zdd	d
Zdd Z	dd Z
dd Zdd Z  ZS )Independenta  
    Reinterprets some of the batch dimensions of a distribution as event dimensions.

    This is mainly useful for changing the shape of the result of
    :meth:`log_prob`. 

    Args:
        base (Distribution): The base distribution.
        reinterpreted_batch_rank (int): The number of batch dimensions to 
            reinterpret as event dimensions.

    Examples:

        .. code-block:: python
        
            import paddle
            from paddle.distribution import independent

            beta = paddle.distribution.Beta(paddle.to_tensor([0.5, 0.5]), paddle.to_tensor([0.5, 0.5]))
            print(beta.batch_shape, beta.event_shape)
            # (2,) ()
            print(beta.log_prob(paddle.to_tensor(0.2)))
            # Tensor(shape=[2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [-0.22843921, -0.22843921])
            reinterpreted_beta = independent.Independent(beta, 1)
            print(reinterpreted_beta.batch_shape, reinterpreted_beta.event_shape)
            # () (2,)
            print(reinterpreted_beta.log_prob(paddle.to_tensor([0.2,  0.2])))
            # Tensor(shape=[1], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            #        [-0.45687842])
    c                    s   t |tjstdt| d|  k rt|jks*n tdt|j d| || _|| _	|j|j
 }tt| j|d t|j|  |t|j| d  d d S )Nz1Expected type of 'base' is Distribution, but got r   z)Expected 0 < reinterpreted_batch_rank <= z
, but got )batch_shapeevent_shape)
isinstancer   Distribution	TypeErrortypelenr   
ValueError_base_reinterpreted_batch_rankr   superr   __init__)selfbaseZreinterpreted_batch_rankshape	__class__ OD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distribution/independent.pyr   3   s*   

zIndependent.__init__c                 C      | j jS N)r   meanr   r   r   r   r   F      zIndependent.meanc                 C   r   r   )r   variancer   r   r   r   r   J   r   zIndependent.variancer   c                 C   s   | j |S r   )r   sample)r   r   r   r   r   r   N   s   zIndependent.samplec                 C   s   |  | j|| jS r   )_sum_rightmostr   log_probr   r   valuer   r   r   r   Q   s   zIndependent.log_probc                 C   s   |  | S r   )r   expr    r   r   r   probU   s   zIndependent.probc                 C   s   |  | j | jS r   )r   r   entropyr   r   r   r   r   r$   X   s   zIndependent.entropyc                 C   s"   |dkr| tt| dS |S )Nr   )sumlistrange)r   r!   nr   r   r   r   \   s   "zIndependent._sum_rightmost)r   )__name__
__module____qualname____doc__r   propertyr   r   r   r   r#   r$   r   __classcell__r   r   r   r   r      s     


r   N)Zpaddle.distributionr   r   r   r   r   r   r   <module>   s   