Tensor Reshape
Tensor Reshape — same data, different dimensions
- tensor
- *
Tensor Reshape changes the dimensions of a tensor without touching its data. Same numbers, same order, different box to put them in. If a node downstream is demanding a specific layout and your tensor doesn't have it, reshape is how you get from here to there - flattening a 4D image into a 1D list for a classifier, or grouping a flat list into a proper 2D grid.
It's part of Basic data handling by StableLlama, a dependency-free utility pack that wraps everyday Python and PyTorch into ComfyUI nodes, sitting alongside the other tensor shape tools (slice, permute, join).
How it works
You type a target shape as a comma-separated string, and the node runs tensor.reshape(...) with it. Two things matter to know upfront:
- The total number of elements must match. A
[1, 4, 64, 64]tensor has 16,384 elements, so your new shape has to multiply out to 16,384 too. Otherwise PyTorch refuses. - You can use
-1for exactly one dimension and let PyTorch figure out the rest. This is the shape string you'll use most:-1alone flattens everything into one long vector.
The inputs that matter
Two inputs:
tensor(wildcard*) - what you're reshaping.shape(STRING, default-1) - comma-separated target dims, e.g.1, 16, 64, 64or just-1.
One wildcard output - the reshaped tensor.
Where you'd actually use it
Two patterns dominate. First: flattening. A custom node that wants a flat feature vector gets -1 from you, and a [1, 1024, 1024, 3] image becomes [1, 3145728]. Second: re-boxing for a specific interface - if a model wrapper expects [batch, channels, height, width] and you have a [batch, height, width, channels] tensor, that's actually permute's job (reordering), but when a shape is genuinely wrong, reshape fixes it.
The shape string is evaluated left-to-right and just needs the element count to check out. -1 is your friend; put it last and let PyTorch do the arithmetic.
Installing it
# ComfyUI Manager (recommended): search "Basic data handling", install, restart.
# or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
# restart ComfyUI
No dependencies beyond torch, no models to download - the whole pack is intentionally lightweight.
Where people get burned
Forgetting that reshape isn't permute. Reshape reinterprets the flat data in row-major order; it does not move dimensions around. If your data is channel-last and you need channel-first, reshaping instead of permuting will silently scramble your image into garbage that looks plausible at a glance. Reordering is Tensor Permute's job; reshaping is repackaging.
Element count mismatches. The #1 error is trying 1, 4, 64, 64 on a tensor that has a different number of elements, or typing -1 twice (which PyTorch won't accept - only one unknown is allowed). The error message tells you both shapes; the fix is usually "multiply out the dims you know and check the total."
It's a view, sometimes. reshape returns a view when it can and a copy when it can't, so you can't rely on it being free or being a copy - for the purposes of this node, just know it always gives you back a valid tensor or a clear error. If you need to guarantee a contiguous copy, look elsewhere; for everyday shaping, this is fine.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| tensor | * | — | |
| shape | STRING | -1 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| * | * | — |