/usr/local/lib64/python3.6/site-packages/torch/distributions/__pycache__
Edit: /usr/local/lib64/python3.6/site-packages/torch/distributions/__pycache__/categorical.cpython-36.pyc (5813B)
3
Egp @ sT d dl Z d dlmZ d dlmZ d dlmZ d dlmZm Z m
Z
G dd deZdS ) N)nan)constraints)Distribution)probs_to_logitslogits_to_probs
lazy_propertyc s e Zd ZdZejejdZdZd! fdd Z d" fdd Z
d d
Zejdddd
d Z
edd Zedd Zedd Zedd Zedd Zej fddZdd Zdd Zd#dd Z ZS )$Categoricala
Creates a categorical distribution parameterized by either :attr:`probs` or
:attr:`logits` (but not both).
.. note::
It is equivalent to the distribution that :func:`torch.multinomial`
samples from.
Samples are integers from :math:`\{0, \ldots, K-1\}` where `K` is ``probs.size(-1)``.
If `probs` is 1-dimensional with length-`K`, each element is the relative probability
of sampling the class at that index.
If `probs` is N-dimensional, the first N-1 dimensions are treated as a batch of
relative probability vectors.
.. 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.multinomial`
Example::
>>> m = Categorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
>>> m.sample() # equal probability of 0, 1, 2, 3
tensor(3)
Args:
probs (Tensor): event probabilities
logits (Tensor): event log probabilities (unnormalized)
)probslogitsTNc s |d k|d kkrt d|d k rJ|j dk r4t d||jd dd | _n(|j dk r^t d||jd
dd | _|d k r| jn| j| _| jj d | _| jj dkr| jj d d nt
j }tt
| j||d d S )
Nz;Either `probs` or `logits` must be specified, but not both. z3`probs` parameter must be at least one-dimensional.T)keepdimz4`logits` parameter must be at least one-dimensional.)dimr )
validate_argsr r r )
ValueErrorr
sumr Z logsumexpr
_paramsize_num_eventsZ
ndimensiontorchSizesuperr __init__)selfr r
r batch_shape) __class__ K/usr/local/lib64/python3.6/site-packages/torch/distributions/categorical.pyr 1 s (zCategorical.__init__c s | j t|}tj|}|tj| jf }d| jkrH| jj||_|j|_d| jkrh| j j||_ |j |_| j|_t
t|j|dd | j|_|S )Nr r
F)r )
Z_get_checked_instancer r r r __dict__r expandr r
r r _validate_args)r r Z _instancenewparam_shape)r r r r B s
zCategorical.expandc O s | j j||S )N)r r! )r argskwargsr r r _newQ s zCategorical._newr )Zis_discreteZ event_dimc C s t jd| jd S )Nr r )r Zinteger_intervalr )r r r r supportT s zCategorical.supportc C s
t | jS )N)r r )r r r r r
X s zCategorical.logitsc C s
t | jS )N)r r
)r r r r r \ s zCategorical.probsc C s
| j j S )N)r r )r r r r r" ` s zCategorical.param_shapec C s t j| j t| jj| jjdS )N)dtypedevice)r full_extended_shaper r r' r( )r r r r meand s zCategorical.meanc C s t j| j t| jj| jjdS )N)r' r( )r r) r* r r r' r( )r r r r varianceh s zCategorical.variancec C sJ t |tjstj|}| jjd| j}tj||j dj}|j| j |S )Nr Tr )
isinstancer r r Zreshaper ZmultinomialZnumelTr* )r Zsample_shapeZprobs_2dZ
samples_2dr r r samplel s
zCategorical.samplec C sR | j r| j| |j jd}tj|| j\}}|dd df }|jd|jdS )Nr .r r r ) r Z_validate_samplelongZ unsqueezer Zbroadcast_tensorsr
gatherZsqueeze)r valueZlog_pmfr r r log_probs s
zCategorical.log_probc C s6 t j| jjj}t j| j|d}|| j }|jd S )N)minr r )r Zfinfor
r' r4 clampr r )r Zmin_realr
Zp_log_pr r r entropy{ s
zCategorical.entropyc C sL | j }tj|tj| jjd}|jddt| j }|rH|j d| j }|S )N)r' r( r r )r )r r )r )
r r Zaranger0 r r( viewlenZ_batch_shaper )r r Z
num_eventsvaluesr r r enumerate_support s zCategorical.enumerate_support)NNN)N)T)__name__
__module____qualname____doc__r ZsimplexZreal_vectorZarg_constraintsZhas_enumerate_supportr r r% Zdependent_propertyr&