AddReshapeLayer
Put a reshape inside the model instead of praying the shapes line up
- model
- TORCH_MODEL
The eternal middle step of building a CNN classifier: you've got a conv stack outputting (batch, channels, height, width) and your AddLinearLayerNode wants a flat (batch, features). Normally you'd squeeze in a FlattenTensor node before the model and hope the shapes match when you hand them over. AddReshapeLayer moves that reshape inside the model itself - it appends both a nn.Flatten and an nn.Unflatten to your nn.Sequential, so the model handles its own reshaping and the layer dimensions stay consistent.
Why the pair? The node flattens everything first, then unflattens dimension 0 into the shape you gave it. So a (batch, 3, 32, 32) tensor becomes (batch*3*32*32,) and then gets unflattened into, say, (batch, -1) where the -1 auto-computes the feature count. That second half is what makes the connection to a following linear layer stable - the linear sees exactly (batch, features).
Inputs
- model (
TORCH_MODEL) - the Sequential to extend. Start fromSequentialModelProvider. - shape - a comma-separated string, default
1, -1.-1means "whatever's left." For(batch, features)semantics you typically want the batch dim first, so(1, -1)gives you a leading 1 that preserves the batch when unflattening. Note it's parsed with a plainsplit(","), so1,-1and1, -1both work, but don't leave stray characters.
Output: one TORCH_MODEL - now with reshape built in.
Install
ComfyUI Manager, search "EternalKernel PyTorch Nodes", or:
cd ComfyUI/custom_nodes
git clone https://github.com/TashaSkyUp/EternalKernelPytorchNodes
cd EternalKernelPytorchNodes
pip install -r requirements.txt
Restart ComfyUI; node under ETK/pytorch. No model files; requirements are the standard ComfyUI stack plus scipy, scikit-learn, transformers, einops.
Common issues
- The
-1must appear exactly once. Two-1s in the shape string is ambiguous and errors. Count your dims:1, -1is the safe default. - Total size must match. Unflatten needs the product of the new dims to equal the flattened size. If
shapeimplies a different total, the error shows up at training/inference time, not at node-build time. - "Not an nn.Sequential model." The pack-wide rule for every builder node - start from
SequentialModelProvider.
Realistically, for the small models this pack builds, FlattenTensor outside the model does the same job with less mystery - this node is for when you want the reshape encapsulated so the model is self-contained (which matters if you save it and reload it elsewhere). It's a niche utility, and that's fine. No community tutorials exist for it, but it's two stock torch layers, so any Flatten/Unflatten documentation applies. And as with everything in this pack: it patches ComfyUI's validator to ignore return_type_mismatch, so a subtly-wrong wire may not raise the error you're expecting.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| model | TORCH_MODEL | — | |
| shape | STRING | 1, -1 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| TORCH_MODEL | TORCH_MODEL | — |