
    Ph                     d    d dl Z d dlmZ d dlmZ d dlmZ ddgZ G d de      Z G d de      Z	y)	    N)constraints)Categorical)DistributionOneHotCategorical OneHotCategoricalStraightThroughc                   0    e Zd ZdZej
                  ej                  dZej                  Z	dZ
d fd	Zd fd	Zd Zed        Zed        Zed	        Zed
        Zed        Zed        Zed        Z ej.                         fdZd Zd ZddZ xZS )r   a  
    Creates a one-hot categorical distribution parameterized by :attr:`probs` or
    :attr:`logits`.

    Samples are one-hot coded vectors of size ``probs.size(-1)``.

    .. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
              and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
              will return this normalized value.
              The `logits` argument will be interpreted as unnormalized log probabilities
              and can therefore be any real number. It will likewise be normalized so that
              the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
              will return this normalized value.

    See also: :func:`torch.distributions.Categorical` for specifications of
    :attr:`probs` and :attr:`logits`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
        >>> m.sample()  # equal probability of 0, 1, 2, 3
        tensor([ 0.,  0.,  0.,  1.])

    Args:
        probs (Tensor): event probabilities
        logits (Tensor): event log probabilities (unnormalized)
    )probslogitsTc                     t        ||      | _        | j                  j                  }| j                  j                  dd  }t        |   |||       y )Nvalidate_args)r   _categoricalbatch_shapeparam_shapesuper__init__)selfr	   r
   r   r   event_shape	__class__s         rC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributions/one_hot_categorical.pyr   zOneHotCategorical.__init__*   sO    'v6''33''33BC8kO    c                     | j                  t        |      }t        j                  |      }| j                  j                  |      |_        t        t        |  || j                  d       | j                  |_	        |S )NFr   )
_get_checked_instancer   torchSizer   expandr   r   r   _validate_args)r   r   	_instancenewr   s       r   r   zOneHotCategorical.expand0   st    (():IFjj-,,33K@.)) 	/ 	
 "00
r   c                 :     | j                   j                  |i |S N)r   _new)r   argskwargss      r   r#   zOneHotCategorical._new:   s     %t  %%t6v66r   c                 .    | j                   j                  S r"   )r   _paramr   s    r   r'   zOneHotCategorical._param=         '''r   c                 .    | j                   j                  S r"   r   r	   r(   s    r   r	   zOneHotCategorical.probsA         &&&r   c                 .    | j                   j                  S r"   )r   r
   r(   s    r   r
   zOneHotCategorical.logitsE   r)   r   c                 .    | j                   j                  S r"   r+   r(   s    r   meanzOneHotCategorical.meanI   r,   r   c                     | j                   j                  }|j                  d      }t        j                  j
                  j                  ||j                  d         j                  |      S )Nr   )axis)num_classes)	r   r	   argmaxr   nn
functionalone_hotshapeto)r   r	   modes      r   r9   zOneHotCategorical.modeM   sW    !!''|||$xx""**4U[[_*MPPQVWWr   c                 b    | j                   j                  d| j                   j                  z
  z  S )N   r+   r(   s    r   variancezOneHotCategorical.varianceS   s*      &&!d.?.?.E.E*EFFr   c                 .    | j                   j                  S r"   )r   r   r(   s    r   r   zOneHotCategorical.param_shapeW   s      ,,,r   c                 ,   t        j                  |      }| j                  j                  }| j                  j                  }| j                  j                  |      }t         j                  j                  j                  ||      j                  |      S r"   )
r   r   r   r	   _num_eventssampler4   r5   r6   r8   )r   sample_shaper	   
num_eventsindicess        r   r@   zOneHotCategorical.sample[   sp    zz,/!!''&&22
##**<8xx""**7J?BB5IIr   c                     | j                   r| j                  |       |j                  d      d   }| j                  j	                  |      S )Nr   r;   )r   _validate_samplemaxr   log_prob)r   valuerC   s      r   rG   zOneHotCategorical.log_probb   sB    !!%())B-"  ))'22r   c                 6    | j                   j                         S r"   )r   entropyr(   s    r   rJ   zOneHotCategorical.entropyh   s      ((**r   c                 L   | j                   d   }t        j                  || j                  j                  | j                  j
                        }|j                  |fdt        | j                        z  z   |fz         }|r#|j                  |f| j                  z   |fz         }|S )Nr   )dtypedevice)r;   )
r   r   eyer'   rL   rM   viewlenr   r   )r   r   nvaluess       r   enumerate_supportz#OneHotCategorical.enumerate_supportk   s    Q1DKK$5$5dkk>P>PQaTD3t/?/?+@$@@A4GH]]A4$*:*:#:aT#ABFr   )NNNr"   )T)__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintsr6   supporthas_enumerate_supportr   r   r#   propertyr'   r	   r
   r/   r9   r<   r   r   r   r@   rG   rJ   rS   __classcell__)r   s   @r   r   r   	   s    8 !, 3 3{?V?VWO!!G P7 ( ( ' ' ( ( ' ' X X
 G G - - #-%**, J3+r   c                   <    e Zd ZdZdZ ej                         fdZy)r   a
  
    Creates a reparameterizable :class:`OneHotCategorical` distribution based on the straight-
    through gradient estimator from [1].

    [1] Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation
    (Bengio et al, 2013)
    Tc                 |    | j                  |      }| j                  j                  }|||j                         z
  z   S r"   )r@   r   r	   detach)r   rA   samplesr	   s       r   rsamplez(OneHotCategoricalStraightThrough.rsample~   s7    ++l+!!''%%,,.011r   N)rT   rU   rV   rW   has_rsampler   r   rc    r   r   r   r   t   s     K#-5::< 2r   )
r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   __all__r   r   re   r   r   <module>rj      s8     + 7 9 B
Ch hV2'8 2r   