Pt Flatten
The bridge between conv layers and your classifier head
- tens
- TENSOR
Pt Flatten is the ComfyUI-Pt-Wrapper node that crushes a tensor down to a single 1D vector. One input, one output, no options - it calls tens.flatten(), which collapses every dimension into one long row of numbers.
Why would you want that? It's the classic handoff inside a convolutional classifier. Your conv layers and pooling chew an image down to a stack of small feature maps - say [batch, 64, 4, 4]. Linear layers (which the pack calls Pt Linear / Ptn Linear) can't eat a 4D tensor; they expect a 2D [batch, features]. Flatten is the glue that turns those feature maps into the "features" column a classifier head can work with. In the pack's dog-vs-cat ResNet example, flatten sits right between the feature extractor and the final linear classification layer.
ComfyUI-Pt-Wrapper is Hide Inada's (HowToSD.com) no-code PyTorch pack for the node graph - a spin-off of ComfyUI-Data-Analysis with roughly 200 nodes for tensor math, model building, and training. This is one of the transform nodes that make the "build your own network" workflow possible.
Inputs and outputs
From info_schema:
tens- the tensor to flatten.- Output:
TENSOR- the 1D result.
No start/end dimension controls, no keepdim. Just full flatten.
Installing it
It's a Data Analysis category node from ComfyUI-Pt-Wrapper. Install once via ComfyUI Manager (search "ComfyUI-Pt-Wrapper") or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
# then restart ComfyUI
The pack's requirements.txt carries a heavy ML stack (transformers, datasets, gensim, peft, accelerate, pandas, seaborn) for the training/text side, but flattening needs nothing beyond the torch ComfyUI already ships.
Where people get burned
The big trap is that this flattens everything, including the batch dimension. If you feed a [16, 3, 32, 32] batch of images in, you get one giant [49152] vector - not sixteen separate [3072] samples. For most model wiring you want the batch kept intact, with only the per-sample dims merged, and this node can't do that. PyTorch's own flatten(start_dim, end_dim) can; this wrapper only exposes the all-the-way-to-1D version. If you need a partial flatten, look at Pt Reshape or Pt View in the same pack, which take explicit target shapes.
The good news: once you understand that, Pt Flatten is about as predictable as nodes get. Same shape in means the same total element count out, just rearranged. And since the count is preserved, it plays nicely with training nodes that validate shapes.
A lighter footgun: if you flatten before the batch stays batched, your classifier head silently gets one "sample" per run and training behavior gets weird without throwing an obvious error. Watch the shape - that's what the pack's Pt Show Size node is for.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| tens | TENSOR | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| TENSOR | TENSOR | — |