L ipddlmZddlZddlmZmZddlmZddlmZddl m Z m Z m Z dgZ GddeZy) )OptionalN)nanTensor) constraints) Distribution) lazy_propertylogits_to_probsprobs_to_logits Categoricalc eZdZdZej ej dZdZ dde e de e de e ddffd Z dfd Z d Zejdd dZede fdZede fdZedej,fdZede fdZede fdZede fdZej,fdZdZdZddZxZS)r a 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:: >>> # xdoctest: +IGNORE_WANT("non-deterministic") >>> 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) )probslogitsTNr r validate_argsreturnc`|du|duk(r td|:|jdkr td||jddz |_n=|J|jdkr td||j ddz |_| |jn |j |_|j jd|_|j jdkDr|j jddntj}t|5|| y) 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.)dimrr) ValueErrorrsumr logsumexpr_paramsize _num_events ndimensiontorchSizesuper__init__)selfr rr batch_shape __class__s e/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/distributions/categorical.pyr!zCategorical.__init__8s TMv~ .M   yy{Q !VWW2t!<rBr8s r%variancezCategorical.variancerFr&cJt|tjstj|}|jj d|j }tj ||jdj}|j |j|S)NrT) isinstancerrr reshaper multinomialnumelTrD)r" sample_shapeprobs_2d samples_2ds r%samplezCategorical.samplesy, 3 ::l3L::%%b$*:*:;&&x1C1C1EtLNN !!$"6"6|"DEEr&c|jr|j||jjd}t j ||j \}}|dddf}|jd|jdS)Nr.r) r+_validate_samplelong unsqueezerbroadcast_tensorsrgathersqueeze)r"valuelog_pmfs r%log_probzCategorical.log_probsv     ! !% ( &&r*00 Dwc2A2g~~b%(0044r&ctj|jjj}tj |j|}||j z}|jd S)N)minr)rfinforr@raclampr r)r"min_realrp_log_ps r%entropyzCategorical.entropysS;;t{{00155T[[h74::% Br&c&|j}tj|tj|jj }|j ddt|jzz}|r|jd|jz}|S)Nr?)r)r) rrarangerXrrAviewlen _batch_shaper*)r"r* num_eventsvaluess r%enumerate_supportzCategorical.enumerate_supportsq%% j 4;;CUCUVUTC0A0A,B%BBC ]]54+<+<#<=F r&)NNNr0)T) __name__ __module__ __qualname____doc__rsimplex real_vectorarg_constraintshas_enumerate_supportrrboolr!r*r3dependent_propertyr9rrr propertyrrr.rErIrKrUr_rfrn __classcell__)r$s@r%r r sh$L!, 3 3{?V?VWO #'#'(, CC C ~ C  C6 0$[##BECE+++,v,,"UZZ"" f  )f)) &  #-%**,F5 r&)typingrrrrtorch.distributionsr torch.distributions.distributionrtorch.distributions.utilsrr r __all__r r&r%rs1 +9UU /X,Xr&