ComfyUI Node

NNT Text To Tensor

Paste a list literal, get a torch tensor

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Text To Tensor
    • TENSOR
    dtypefloat32
    requires_gradfalse
    device
    text_content
    input_text

    Sometimes the fastest way to get data into a graph is to just type it. NNT Text To Tensor parses a block of text as a Python list literal and turns it into a torch tensor - no CSV, no loader, no download. Type [[1, 2, 3], [4, 5, 6]] into the node, run it, and out comes a 2×3 tensor you can wire anywhere a TENSOR is expected.

    It's the mirror image of NntTensorToText, and together they're how you move data between "human-readable" and "torch-readable" without leaving the node graph. If you're testing a tiny network and don't want to set up a full dataset, this is the quickest path to a working tensor.

    How it works

    The input is text_content, a multiline string. The node parses it with ast.literal_eval - which means it must be a valid Python literal: [1, 2, 3], [[1, 2, 3], [4, 5, 6]], nested lists of numbers, that sort of thing. Anything else throws a helpful error explaining the expected format.

    The controls:

    • dtype - float32 (default), float64, int32, int64, uint8, bool, or auto. auto is a nice touch: it checks whether every value is a whole number, and picks int64 if so, else float32.
    • requires_grad - whether the tensor tracks gradients, off by default.
    • device - cpu or cuda.

    There are two text inputs, which is the subtle part: text_content is the widget you type into, and input_text is a connector input meant to be fed from another node. If you wire something into input_text, it wins over the widget - the code prioritizes the connector. That's handy for composing, and it's also a classic "why is it ignoring what I typed" trap if you have a stray wire connected.

    The single output is TENSOR.

    Where it fits

    Three obvious uses. Debugging: you need a known tensor to poke at a NntTensorOperations gradient exercise - hand-type it here. Data entry for toy problems: a tiny regression dataset like [[0, 1], [1, 2], [2, 4]] pasted straight in beats building a CSV. And teaching: when you want to show exactly what a tensor is, typing one into existence and reading it back out with Tensor To Text is the most direct demonstration in the pack.

    The limitations are the flip side of its simplicity. literal_eval means no expressions, no variables, no comments - pure data. And there's no validation that your nested lists are rectangular; [[1, 2], [3]] will fail at tensor creation with a numpy shape error. Read the error message, fix the brackets.

    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
    

    Restart ComfyUI, or use Manager and search "ComfyUI Neural Network Toolkit NNT". As always with this pack, the requirements list is long (torch, numpy, pandas, matplotlib, sklearn, transformers, statsmodels, onnx, shap 0.41.0, …) and the first install is slow. The node itself has essentially no extra moving parts - just torch and a parser.

    CategoryNNT Neural Network Toolkit/Tensors

    Inputs (5)

    NameTypeDefaultDescription
    dtypeCOMBOfloat327 options: float32, float64, int32, int64, uint8, bool, +1
    requires_gradBOOLEANfalse
    deviceCOMBO2 options: cpu, cuda
    text_contentSTRING
    input_textoptSTRING

    Outputs (1)

    NameTypeDescription
    TENSORTENSOR