Update Discriminator
One training step for the critic — where GAN loss actually comes from
- X
- Z
- net_D
- net_G
- loss_D
If you've ever stared at a GAN training log and wondered where loss_D comes from, this node is the answer in one box. Update Discriminator performs exactly one training step for the discriminator in a GAN: it scores real data X, generates fake data from noise Z using the generator, scores that too, computes the binary-cross-entropy loss on both, backpropagates, and applies an SGD update - then hands you back the discriminator loss as a number. It's the d2l update_D function as a node, and it's the half of a GAN you usually only hear about in passing.
The mechanism is the classic minimax dance, compressed. Real samples get target 1, fake samples (with gradients detached so the generator doesn't get updated through the discriminator's backward pass) get target 0, and the loss is the average of the two BCE terms. The output, loss_D, is that average after the update - a FLOAT you can watch tick down as the discriminator gets better at telling real from fake. The adversarial tension - the number going up when the generator gets good - is one of the most confusing things about GAN training, and this node exists to make you see it happen.
Inputs
Everything is optional, which is ComfyDL's way of letting you stub the node. Wire in:
X- real data (cdlTensor).Z- latent noise (cdlTensor) that the generator turns into fakes.net_D- the discriminator model (cdlModel).net_G- the generator model (cdlModel).
Leave any of them unwired and the node returns a harmless 0.0 rather than crashing. It's a debug-friendly choice, but it means a "working" graph can silently be doing nothing if you forgot a wire.
Output
One output: loss_D, a FLOAT. Feed it to a plot node or a text display and you've got a live GAN training curve.
Installing it
Part of ComfyDL. ComfyUI Manager, search "ComfyDL". Or:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt
Restart ComfyUI. Pack depends only on matplotlib; no models to fetch.
Gotchas
The big honesty note: this node creates a fresh SGD optimizer with a hardcoded lr=0.01 on every call, and it only updates the discriminator's parameters. The generator update is your other node (Update Generator). In a real GAN you'd alternate them - one Update D, one Update G, repeat - and you'd typically have a smarter optimizer with momentum. This is a teaching simplification, and it's fine for learning, but don't mistake it for a production training loop. Because ComfyUI caches aggressively, chaining a handful of Update D / Update G nodes in a queue gives you a short "training run," but each step starts a new optimizer state - that's the deliberate tradeoff of doing this without a real training node. The pack is young and has essentially no community signal, so expect to debug from first principles; the good news is the loss math is visible and small enough to reason about.
Inputs (4)
| Name | Type | Default | Description |
|---|---|---|---|
| Xopt | TENSOR | — | |
| Zopt | TENSOR | — | |
| net_Dopt | cdlModel | — | |
| net_Gopt | cdlModel | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| loss_D | FLOAT | — |