ComfyUI Node

Pt Mean

The average along a dimension, for pooling and stats

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

Pt Mean is torch.mean in the graph: average a tensor along one or more dimensions and hand back the result. It's the most-used summary statistic in machine learning - global average pooling, normalization stats, loss reporting - so if you're doing anything with the pack's training side, you'll meet this node early.

How it works

Three inputs, same shape as its reduction siblings (Pt Max, Pt Min):

  • tens - the TENSOR to average.
  • dim - a STRING field (multiline) accepting 0, [0], or (1, 2). Multi-axis reduction works; leave it blank for the mean of everything.
  • keepdim - boolean, default off. Keep the reduced axis as size 1 (broadcast-friendly) or drop it.

Implementation is torch.mean(tens, dim=dim, keepdim=keepdim). Straightforward, but with one hard constraint from torch: the input must be floating-point (or complex). Mean over an int tensor throws - you can't average integers without choosing a result type, and torch refuses to guess.

The two jobs it does

Global average pooling. Reduce a (b, c, h, w) feature map over (2, 3) and you get one value per channel - (b, c, 1, 1) with keepdim, or (b, c) without. That's the standard way to collapse spatial info before a classifier, and it's exactly what the pack's ResNet and Transformer examples build on.

Normalization stats. Mean and standard deviation are the backbone of feature normalization. Compute the mean, then wire it (with keepdim on) into a subtract node against the original tensor - that's mean-centering, the first step of most preprocessing.

Gotchas

The float-only rule is the big one - if you feed it ints, the node goes red. And remember mean averages, so outliers drag it: if your data has extreme values and you want something robust to them, that's Pt Median's job instead. Same dim parsing rules as its siblings - (1, 2) is a tuple, [0] a list, and malformed input throws.

Installing

Pt Mean ships in the HowToSD/ComfyUI-Pt-Wrapper pack under the "Data Analysis" menu - install once, ~200 nodes. ComfyUI Manager: search ComfyUI-Pt-Wrapper, install, restart. Manual:

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

The heavy requirements.txt (transformers, peft, accelerate…) serves training; mean needs only PyTorch, already installed. Skip the pip line for math-only work. No models to download.

Pack-wide: everything uses the custom TENSOR type, not ComfyUI's IMAGE/LATENT - convert with Pt From Image (Pt From Image Transpose for (b, c, h, w)) and Pt To Image.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
dimSTRING
keepdimBOOLEANfalse

Outputs (1)

NameTypeDescription
TENSORTENSOR