ComfyUI Node

NNT Tensor Slice

NNT Tensor Slice — grab a few rows out of a tensor (or image) without leaving the graph

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Tensor Slice
  • tensor
  • image
  • TENSOR
  • IMAGE
start_element0
num_elements1
flattenFalse
reshapeFalse
shape[1,1,1]
convert_maskFalse

Sometimes you don't want the whole batch. NNT Tensor Slice is the toolkit's way of cutting a chunk out of a tensor (or an image) so you can inspect it, plot it, or feed a single sample somewhere. It's a debug-and-explore node, and for a teaching pack that's a core job.

The inputs

  • start_element (default 0) - where to start cutting, along the first (batch) dimension.
  • num_elements (default 1) - how many rows to keep. Slice of 1 = one sample.
  • flatten (False) - squash the tensor to 1D first, then slice. Useful when you want a flat vector of values.
  • reshape (False) + shape (default [1,1,1]) - reshape the sliced chunk. shape is a Python-list string like [1, 28, 28] that gets evaluated. Only applies after the slice.
  • convert_mask (False) - permute the result to [N, 3, H, W] layout. Handy if you're slicing image-ish tensors that arrived in channels-last form and need them back in image format.

The node takes tensor or image as an optional input - wire whichever you're cutting. Outputs: TENSOR and IMAGE (both get the same sliced data, so pick the type that matches your downstream node).

How it works

Simple and honest: it picks whichever input you gave it, converts to float32 if needed, optionally flattens, then does tensor[start : start + num_elements]. If your range runs past the end, it silently clamps the count instead of erroring - nice behavior. Then the optional reshape (via eval(shape)) and the mask-permute apply. If anything throws, it returns the input unchanged rather than crashing the workflow, so a bad shape string won't kill your run - it'll just quietly do nothing.

When you'd actually use it

  • Spot-check your data - after a dataset-to-tensor conversion, slice off one sample and inspect it.
  • Single-sample inference - pull one row out of a test set and feed it to the pack's inference node.
  • Debugging shapes - confirm your slice has the shape you expect before it enters a model.

It's a tiny node, and its one impression with a 100% CTR suggests the couple of people who landed on it knew what they wanted. For the toolkit's "see what's inside your tensors" philosophy it's the right companion to the plot and text-display nodes.

Common issues

  • Nothing happens - you probably fed a malformed shape string (eval fails silently), or didn't connect either input.
  • Start index out of range - the node raises a clear error for that one case and returns the input as-is.
  • Slicing images - remember IMAGE tensors in ComfyUI are [N, H, W, C] (channels-last). flatten will destroy that layout; use convert_mask if you need channels-first back.

Install

Part of the pack:

cd ComfyUI/custom_nodes
git clone https://github.com/inventorado/ComfyUI_NNT.git
cd ComfyUI_NNT
pip install -r requirements.txt

or ComfyUI Manager → "ComfyUI Neural Network Toolkit NNT", restart, and it's under NNT Neural Network Toolkit/Tensors.

CategoryNNT Neural Network Toolkit/Tensors

Inputs (8)

NameTypeDefaultDescription
start_elementINT00–2147483647
num_elementsINT11–2147483647
flattenCOMBOFalse2 options: True, False
reshapeCOMBOFalse2 options: True, False
shapeSTRING[1,1,1]
convert_maskCOMBOFalse2 options: True, False
tensoroptTENSOR
imageoptIMAGE

Outputs (2)

NameTypeDescription
TENSORTENSOR
IMAGEIMAGE