Nodes/ComfyUI-Pt-Wrapper/Ptd Categorical
ComfyUI Node

Ptd Categorical

Pick one outcome out of many

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptd Categorical
    • PTDISTRIBUTION
    probs
    logits

    The Categorical distribution is the generalization of the coin flip: instead of two outcomes, you get k outcomes, each with its own probability, and a sample picks exactly one of them. Ptd Categorical builds that as a PyTorch Distribution object. If you're modeling multi-class decisions - which class did this image belong to, which token comes next, which option was chosen - this is the distribution you want.

    How it works

    You type in either probs or logits (exactly one, hard-enforced), the node parses it and constructs torch.distributions.Categorical. Probabilities get normalized internally, so [0.2, 0.3, 0.5] and [2, 3, 5] describe the same categorical - the logits version just skips the explicit normalization step, which is useful when you have raw scores from a model layer. Unlike Bernoulli, Binomial, and a few others in the pack, Categorical stays stock PyTorch - no scipy subclass here, since PyTorch already gives it the methods the pack needs.

    The output PTDISTRIBUTION feeds the usual family: Ptdm Sample returns an integer index (0 to k-1) for each draw, Ptdm LogProb scores a given index, Ptdm Pmf gives the probability mass at an index. A classic use: sample repeatedly from a categorical over classes to simulate what a classifier predicts, or wire logits straight from a model tensor into this node and let Ptdm Sample do the argmax-ish work - with the bonus that sampling, not argmax, gives you stochasticity when you want it.

    The inputs that matter

    • probs - a Python literal, e.g. [0.2, 0.3, 0.5] or a tuple.
    • logits - the raw-score form, same literal style.

    The rule is the pack's standard: one or the other, and leaving both blank (the default) throws "You have to specify either probabilities or logits". Note the code uses is not None checks rather than truthiness, precisely so that a literal 0 in your values doesn't get mistaken for "empty".

    Where people get burned

    • Blank defaults - both fields default to "", so typing only one is the fix for the "specify either" error.
    • Literal syntax: 0.2, 0.3 without brackets fails ast.literal_eval. Use [0.2, 0.3, 0.5].
    • Samples are indices, not probabilities - a common "why is it outputting 2??" moment. That's the point of a categorical sample.

    Installing it

    It's part of ComfyUI-Pt-Wrapper:

    • ComfyUI Manager → search "ComfyUI-Pt-Wrapper" → Install → restart.
    • Or cd ComfyUI/custom_nodes && git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper and restart.

    Heavy dependency list (transformers, datasets, peft, accelerate, scikit-learn, scipy, gensim, sentencepiece), so the first startup is slow. No model downloads needed. It's a niche educational pack by HowToSD with little community footprint; the repo's docs/reference/ is the authoritative reference.

    CategoryDistribution

    Inputs (2)

    NameTypeDefaultDescription
    probsSTRING
    logitsSTRING

    Outputs (1)

    NameTypeDescription
    PTDISTRIBUTIONPTDISTRIBUTION