Update Generator
The generator fights back — one step at fooling the critic
- Z
- net_D
- net_G
- loss_G
- fake_X
Update Generator is the other half of the GAN loop, and it's the more interesting one to watch. Where the discriminator learns to tell real from fake, this node teaches the generator to fake better: it takes noise Z, runs it through net_G, pushes the result through net_D, and treats "the discriminator believed it" as the loss - the generator is scored on how convincingly it fooled the critic. Then it backpropagates and applies an SGD step to the generator only. The d2l update_G function, as a node, one step at a time.
The trick that makes GANs work is in the loss: the generator's target is 1 - the same target as real data. The discriminator is supposed to output 1 for real images, so a generator that produces fakes the critic rates near 1 is winning. When loss_G goes down, the discriminator is getting fooled more often. The confusion for beginners is that a low generator loss is the goal here, whereas in supervised learning you always want loss low - and the interplay with loss_D (which climbs as the generator improves) is the seesaw that defines the whole game. Put Update D and Update G side by side and you can literally watch them trade blows.
Inputs
All optional, mirroring Update D:
Z- latent noise (cdlTensor) the generator transforms.net_D- the discriminator (cdlModel), which is frozen here - the gradient flows through it only to reach the generator, not into its own weights.net_G- the generator (cdlModel), the one getting updated.
Outputs
Two, and this is where it beats its sibling:
loss_G- a FLOAT, the generator's BCE loss against target 1.fake_X- acdlTensorof the actual generated samples, detached from the graph. This is the payoff - you can wire it to a display or image node and see what the generator is producing after each step. Watching a blob of noise gradually sharpen into something recognizable is the most satisfying moment in GAN learning.
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. Only matplotlib for the pack; no downloads.
Gotchas
Same honesty note as its sibling: every call spins up a fresh SGD with hardcoded lr=0.01, and there's no optimizer state persisting across nodes. That's fine for a teaching run - chain a few Update D / Update G pairs in a queue and read loss_G and fake_X at the end - but it's not a real training loop, and GANs are famously finicky about optimizer details, so don't expect ImageNet quality out of this. One more gotcha: fake_X is a tensor in the pack's custom cdlTensor format, so wiring it to native ComfyUI image nodes won't work directly - you'll want the pack's own visualization or a tensor-to-image path if that's your goal. Niche pack, no community threads to lean on yet; the math here is straight from the D2L textbook, so trust the mechanism and expect to fiddle with the workflow, not the node.
Inputs (3)
| Name | Type | Default | Description |
|---|---|---|---|
| Zopt | TENSOR | — | |
| net_Dopt | cdlModel | — | |
| net_Gopt | cdlModel | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| loss_G | FLOAT | — |
| fake_X | TENSOR | — |