ComfyUI Node

Pt Argmin

The other arg-extreme, for distances and minima

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Argmin
  • tens
  • TENSOR
dim
keepdimfalse

Pt Argmin is the mirror of Pt Argmax: instead of "which class scored highest," it answers "which value is smallest" - returning the indices of the minima. Classifiers use argmax because you want the best guess. Anything that involves distance, cost, or error wants argmin, because there "best" means smallest. Same shape, same quirks, opposite instinct.

It's a reduction op in ComfyUI-Pt-Wrapper (HowToSD's 200-node PyTorch pack, the spin-off of ComfyUI-Data-Analysis). Where does the minimum side earn its keep in tensor workflows? Nearest-neighbor thinking: if you compute the distance between a query and every candidate, the closest one is argmin over the distance axis. Cost/minimization schedules, early-stopping bookkeeping, and "which timestep/element had the smallest error" all reduce to argmin. In a from-scratch model you might use it for "which token position is most confident-by-opposite" or for selecting the best of several candidates by a loss score.

How it works. Identical contract to Pt Argmax:

  • tens - the TENSOR to reduce.
  • dim - a string field (multiline), type a bare integer like 1 or -1. Empty string flattens the whole tensor into one global minimum index - usually not what you want, so fill it in.
  • keepdim - boolean, default False. True keeps the reduced axis as size 1 ((8, 10)(8, 1)); False drops it ((8, 10)(8,)).

Output is a TENSOR of integer indices, via torch.argmin(tens, dim=dim, keepdim=keepdim).

The traps mirror argmax, with one extra twist. Yes, dim is a string - leave it blank and you get a single global index, or type 1.0 and the parser refuses. Yes, output is positions, not values - you get "which one," not "how small." And the extra one: because argmin points at the smallest, it's sensitive to the sign of your data. If your "distance" values are actually similarity scores where bigger is better (cosine similarity, correlation), argmin hands you the worst match - flip your thinking or use Argmax. Getting nearest-neighbor logic silently inverted is the classic argmin embarrassment, and it never throws an error.

Install: ComfyUI Manager → "ComfyUI-Pt-Wrapper", or:

cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper

then restart. No model downloads; the pack's heavy requirements.txt (transformers, sklearn, sentencepiece, pinned gensim) is the only install cost.

Troubleshooting: single number out when you expected a batch - dim was empty. Parser error - bare integer, no decimals. Results look backwards - you fed it similarity where bigger is better; check your data's polarity before assuming a bug. If the indices seem to "pick the wrong thing," print your input's range mentally: argmin always picks the smallest, and if that's not your intent, you grabbed the wrong node or the wrong sign.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
dimSTRING
keepdimBOOLEANfalse

Outputs (1)

NameTypeDescription
TENSORTENSOR