ComfyUI Node

NNT Define Dense Layer

NNT Define Dense Layer — the workhorse layer, with more init options than you'll ever use

By inventorado·Created 2 years ago·Updated 2 years ago· 69
NNT Define Dense Layer
  • LAYER_STACK
  • LAYER_STACK
  • num_nodes
num_nodes64
activation_functionReLU
use_biasTrue
weight_initkaiming_normal
weight_init_gain1.00
weight_init_modefan_in
weight_init_nonlinearityrelu
bias_initzeros
bias_init_value0.00
normalizationNone
norm_eps0.0000
norm_momentum0.100
norm_affineTrue
dropout_rate0.0
alpha0.01
num_copies1

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 None for 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 set weight_init to zeros and 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 Linear layer - same thing in PyTorch; the compile path handles it as Linear. Don't go looking for a Dense class 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.
  • alpha input - 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.

CategoryNNT Neural Network Toolkit/Layers

Inputs (17)

NameTypeDefaultDescription
num_nodesINT641–8192
activation_functionCOMBOReLU28 options: None, ELU, GELU, GLU, Hardshrink, Hardsigmoid, +22
use_biasCOMBOTrue2 options: True, False
weight_initCOMBOkaiming_normal12 options: default, normal, uniform, xavier_normal, xavier_uniform, kaiming_normal, +6
weight_init_gainFLOAT1.000.01–10
weight_init_modeCOMBOfan_in2 options: fan_in, fan_out
weight_init_nonlinearityCOMBOrelu6 options: relu, leaky_relu, selu, tanh, linear, sigmoid
bias_initCOMBOzeros5 options: default, zeros, ones, normal, uniform
bias_init_valueFLOAT0.00-1–1
normalizationCOMBONone6 options: None, BatchNorm, LayerNorm, InstanceNorm, GroupNorm, LocalResponseNorm
norm_epsFLOAT0.00001e-12–0.001
norm_momentumFLOAT0.1000.001–0.999
norm_affineCOMBOTrue2 options: True, False
dropout_rateFLOAT0.00–0.9
alphaFLOAT0.010–1
num_copiesINT11–100
LAYER_STACKoptLIST

Outputs (2)

NameTypeDescription
LAYER_STACKLIST
num_nodesINT