Nodes/ComfyUI Neural Network Toolkit NNT /NNT Dataset To Target Tensor
ComfyUI Node

NNT Dataset To Target Tensor

NNT Dataset To Target Tensor — turn string labels into what the loss function actually eats

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Dataset To Target Tensor
  • dataset
  • target_tensor
  • info
  • label_info
  • collated_outputs
target_columnlabel
target_typeclassification
num_classes10
use_data_collatorTrue
paddingmax_length
pad_to_multiple_of8
return_tensorspt
encodingsparse
label_smoothing0.10
create_label_mapsTrue
custom_label_map{}
detach_tensorTrue
requires_gradFalse
make_cloneTrue

A dataset's labels are often strings ("cat", "dog", "NEGATIVE") and your loss function wants numbers. NntDatasetToTargetTensor is the node that makes the conversion: it pulls the label column, builds a string→integer mapping, encodes the labels, and hands you a target tensor ready for NntTrainModel or NntFineTuneModel. If you're doing classification in the Neural Network Toolkit, this is the "labels" half of the data pipeline.

The inputs that matter

  • dataset - the DATASET from the pack's loader nodes.
  • target_column (default label) - which column holds the ground truth.
  • target_type - classification (default), regression, or multi_label. This tells the node what kind of encoding you need.
  • encoding - sparse (default: integer class indices - exactly what CrossEntropyLoss wants), one_hot, or label_smooth. Sparse is the right call for the default loss; reach for one-hot or label smoothing if you're using a loss that expects it.
  • label_smoothing (0.1) - the smoothing factor when you pick label_smooth.
  • create_label_maps (True) - auto-build label2id/id2label from the unique labels. If you'd rather pin the mapping, set this False and provide custom_label_map as a JSON-ish string like {"NEGATIVE": 0, "POSITIVE": 1}.
  • num_classes (default 10) - used for one-hot; auto-overridden by the number of labels when create_label_maps builds from the data.
  • requires_grad (default False) - targets shouldn't need gradients; leave it off.

Outputs: target_tensor (TENSOR), info (STRING), label_info (DICT - your label2id/id2label maps), and collated_outputs (DICT - the data-collator result).

How it works

It grabs the column, and if the values are strings it builds the label map (your custom_label_map if given, otherwise sorted-unique), converts strings to indices, then applies the chosen encoding. There's also a use_data_collator path that runs HuggingFace's DataCollatorWithPadding - that's the branch that gives you collated_outputs and matters mostly for variable-length text-style targets. The defaults (sparse, requires_grad=False) are set up so that the common case - integer targets feeding CrossEntropyLoss - just works.

Common issues

  • Loss shape mismatch - the #1 complaint in any training setup. If you picked one_hot but kept the default CrossEntropyLoss, they'll fight: CrossEntropy wants class indices, not one-hot vectors. Match the encoding to the loss.
  • Labels all get clumped together - check label_info; if the mapping collapsed your classes, your target column probably had typos or mixed formats (e.g., "0" and 0).
  • Regression labels - if target_type is regression, don't let it build a classification-style map; you want raw numeric targets.

Install

Pack-level:

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, under NNT Neural Network Toolkit/Data Processing. Pair it with NntDatasetToImageTensor for the standard image-classification chain.

CategoryNNT Neural Network Toolkit/Data Processing

Inputs (15)

NameTypeDefaultDescription
datasetDATASET
target_columnSTRINGlabel
target_typeCOMBOclassification3 options: classification, regression, multi_label
num_classesINT102–1000
use_data_collatorCOMBOTrue2 options: True, False
paddingCOMBOmax_length3 options: max_length, longest, do_not_pad
pad_to_multiple_ofINT81–128
return_tensorsCOMBOpt2 options: pt, tf
encodingCOMBOsparse3 options: sparse, one_hot, label_smooth
label_smoothingFLOAT0.100–0.5
create_label_mapsCOMBOTrue2 options: True, False
custom_label_mapSTRING{}
detach_tensorCOMBOTrue2 options: True, False
requires_gradCOMBOFalse2 options: True, False
make_cloneCOMBOTrue2 options: True, False

Outputs (4)

NameTypeDescription
target_tensorTENSOR
infoSTRING
label_infoDICT
collated_outputsDICT