ComfyUI Node

Ptn Linear

The atom of every MLP you'll build

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Linear
    • PTMODEL
    in_features1
    out_features1
    biastrue

    This is the smallest useful building block in the whole pack: one dense layer, y = xWᵀ + b, exposed as a node. You reach for PtnLinear when you're assembling a model by hand - it's the "linear" brick in the pack's build-a-transformer-from-scratch workflow, and it's also how you cap most architectures with a classification head. It's the node equivalent of nn.Linear, and it does exactly what that function does.

    How it works

    Nothing fancy under the hood: the node constructs torch.nn.Linear(in_features, out_features, bias). Every input feature connects to every output feature with a learned weight, plus an optional learned bias. Weights are initialized with PyTorch's default scheme, which is fine for a single layer. Because it's a bare layer - no activation, no flattening - it's usually chained between other nodes via the pack's model-chaining nodes rather than used standalone.

    The inputs that matter

    Just three, and they map straight onto nn.Linear:

    • in_features - the size of each input vector. This has to match whatever the previous node outputs.
    • out_features - the size you want to go to. Set this to the number of classes for a final classification layer, or to an intermediate width for a hidden layer.
    • bias (default True) - keep the learned bias term unless you have a reason to drop it.

    It emits a single PTMODEL, which you can wire into another PtnLinear, a normalization layer, an activation, or a trainer. The pack's PtnLinearModel node builds the whole multi-layer MLP for you if you'd rather not chain these one at a time.

    How you'd use it

    In the transformer-from-scratch guide, linear layers form the feedforward block and the final classification head. The standard pattern: put PtnPreFlatten (or PtnPreAddChannelAxis) before a PtnLinear, since images and sequence data don't arrive in the nice 2D shape a dense layer wants. Chain with Ptn Chained Model, and remember there's no activation built in - add a Ptf ReLU (or whatever) explicitly if you want the layer to be non-linear.

    Installing

    Same for every node in the pack. ComfyUI Manager → search "Pt-Wrapper", or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
    

    Restart ComfyUI. The pack installs a chunky requirements.txt on first launch; a single linear layer doesn't need any of it, but the rest of the pack does.

    Where people get burned

    Shape mismatch is the whole failure mode here. Feed it a 4D image tensor when in_features expects a vector and it errors. That's not this node's fault - it's why the pre-processing wrapper nodes (PtnPreFlatten, PtnPreAddChannelAxis) exist. And because IS_CHANGED returns NaN, the node rebuilds on every queue; harmless for one layer, but it's why heavy models in this pack shouldn't sit in the graph during light eval runs.

    CategoryTraining

    Inputs (3)

    NameTypeDefaultDescription
    in_featuresINT11–1000000
    out_featuresINT11–1000000
    biasBOOLEANtrue

    Outputs (1)

    NameTypeDescription
    PTMODELPTMODEL