NNT Dataset To Target Tensor
NNT Dataset To Target Tensor — turn string labels into what the loss function actually eats
- dataset
- target_tensor
- info
- label_info
- collated_outputs
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, ormulti_label. This tells the node what kind of encoding you need. - encoding -
sparse(default: integer class indices - exactly whatCrossEntropyLosswants),one_hot, orlabel_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/id2labelfrom 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_mapsbuilds 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_hotbut kept the defaultCrossEntropyLoss, 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_typeis 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.
Inputs (15)
| Name | Type | Default | Description |
|---|---|---|---|
| dataset | DATASET | — | |
| target_column | STRING | label | — |
| target_type | COMBO | classification | 3 options: classification, regression, multi_label |
| num_classes | INT | 102–1000 | — |
| use_data_collator | COMBO | True | 2 options: True, False |
| padding | COMBO | max_length | 3 options: max_length, longest, do_not_pad |
| pad_to_multiple_of | INT | 81–128 | — |
| return_tensors | COMBO | pt | 2 options: pt, tf |
| encoding | COMBO | sparse | 3 options: sparse, one_hot, label_smooth |
| label_smoothing | FLOAT | 0.100–0.5 | — |
| create_label_maps | COMBO | True | 2 options: True, False |
| custom_label_map | STRING | {} | — |
| detach_tensor | COMBO | True | 2 options: True, False |
| requires_grad | COMBO | False | 2 options: True, False |
| make_clone | COMBO | True | 2 options: True, False |
Outputs (4)
| Name | Type | Description |
|---|---|---|
| target_tensor | TENSOR | — |
| info | STRING | — |
| label_info | DICT | — |
| collated_outputs | DICT | — |