NNT Define Dense Layer
NNT Define Dense Layer — the workhorse layer, with more init options than you'll ever use
- LAYER_STACK
- LAYER_STACK
- num_nodes
The fully-connected layer is the brick every neural network tutorial starts with, and this is the brick-layer node of the Neural Network Toolkit. NntDefineDenseLayer appends a Linear layer to your LAYER_STACK - plus, if you want, a normalization, an activation, and a dropout, all rolled into one node. It's the node you'll reach for first, and the one the pack's linear-regression example is built around.
The inputs that actually matter
Twenty-ish inputs sounds scary; most are optional sugar. What you'll set:
- num_nodes (default 64, up to 8192) - how wide this layer is. This is the only input that truly defines the layer; everything else is decoration.
- activation_function (default ReLU) - applied right after the linear transform. Leave as ReLU for hidden layers, set to
Nonefor the final output layer of a regression model. - normalization (default
None) - BatchNorm, LayerNorm, InstanceNorm, GroupNorm, or LocalResponseNorm on the output. BatchNorm before a ReLU is a classic combo, but on small toy datasets it often makes things worse, not better. - dropout_rate (default 0) - dropout applied after activation. Set it low (0.2–0.5) if you're overfitting; leave it at 0 for tiny teaching datasets.
- use_bias (default True), weight_init (default
kaiming_normal), bias_init - the weight-initialization playground. Kaiming is the right default for ReLU nets. The fun here is pedagogical: you can setweight_inittozerosand watch the network refuse to learn, then flip it back.
Outputs: LAYER_STACK (the growing list - chain it into the next define node) and num_nodes as an INT, which is handy if you want to wire the width of one layer into a later node.
How it works
The node doesn't create a PyTorch layer - not yet. It appends a plain dict like {'type': 'Linear', 'num_nodes': 64, ...} to the stack list. The actual nn.Linear gets built later by NntCompileModel, which reads the dict, auto-flattens multi-dimensional input when needed, applies your chosen init, then the norm and activation. So the define node is really a config recorder, and the compile node is the factory.
Gotchas
- It's called "Dense" but emits a
Linearlayer - same thing in PyTorch; the compile path handles it asLinear. Don't go looking for aDenseclass in the report. - Too many nodes, too fast - a 1024-node dense layer on a 28×28 MNIST input is a million+ parameters you'll train on a CPU for no reason. The toolkit will happily let you build it; the training loop will just take forever.
alphainput - that's the LeakyReLU negative slope (default 0.01), used when you pick LeakyReLU or similar. Ignore it unless you're using one of those.
Install
Part of the pack - no separate install:
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 after installing, then start a stack with NntInputLayer → this node → NntCompileModel and you've got a network. The pack's linear-regression and MNIST workflows are built exactly this way.
Inputs (17)
| Name | Type | Default | Description |
|---|---|---|---|
| num_nodes | INT | 641–8192 | — |
| activation_function | COMBO | ReLU | 28 options: None, ELU, GELU, GLU, Hardshrink, Hardsigmoid, +22 |
| use_bias | COMBO | True | 2 options: True, False |
| weight_init | COMBO | kaiming_normal | 12 options: default, normal, uniform, xavier_normal, xavier_uniform, kaiming_normal, +6 |
| weight_init_gain | FLOAT | 1.000.01–10 | — |
| weight_init_mode | COMBO | fan_in | 2 options: fan_in, fan_out |
| weight_init_nonlinearity | COMBO | relu | 6 options: relu, leaky_relu, selu, tanh, linear, sigmoid |
| bias_init | COMBO | zeros | 5 options: default, zeros, ones, normal, uniform |
| bias_init_value | FLOAT | 0.00-1–1 | — |
| normalization | COMBO | None | 6 options: None, BatchNorm, LayerNorm, InstanceNorm, GroupNorm, LocalResponseNorm |
| norm_eps | FLOAT | 0.00001e-12–0.001 | — |
| norm_momentum | FLOAT | 0.1000.001–0.999 | — |
| norm_affine | COMBO | True | 2 options: True, False |
| dropout_rate | FLOAT | 0.00–0.9 | — |
| alpha | FLOAT | 0.010–1 | — |
| num_copies | INT | 11–100 | — |
| LAYER_STACKopt | LIST | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| LAYER_STACK | LIST | — |
| num_nodes | INT | — |