Skip to main content

A collection of GAN loss functions

Project description

Provided are GAN losses from the R3GAN and Seaweed papers.

Relativistic GAN loss referenced in the R3GAN paper:

${L}(\theta, \psi) = \mathbb{E}_{z \sim p_z, \, x \sim p_D} \left[ f \left( D_\psi(G_\theta(z)) - D_\psi(x) \right) \right]$

1. Default logistic GAN loss

$f(t) = -\log(1 + e^{-t})$

# If f is omitted, logistic_f is used automatically
d_loss = -gan_loss(discriminator, generator, real_images, z)

# You have to freeze the other model when doing a backward pass for generator or discriminator, otherwise you will combine the negative and positive gradients which will cancel out.
g_loss = gan_loss(discriminator, generator, real_images, z)

2. Custom function 𝑓 If you have a different function (e.g. hinge), you can pass it in:

def hinge_f(t):
    return torch.nn.functional.relu(1 - t)

d_loss = -gan_loss(
    discriminator, generator, real_images, z,
    f=hinge_f
)

3. Extra arguments to discriminator or generator, if your models need additional inputs:

d_loss = -gan_loss(
    discriminator, generator,
    real_images, z,
    generator_args=(some_label, ),          # positional
    generator_kwargs={'some_flag': True},   # keyword
    disc_args=(some_label, ),
    disc_kwargs={'cond_flag': True}
)

R1 and R2 losses referenced in the R3GAN paper:

$R_1(\psi) = \frac{\gamma}{2} \mathbb{E}_{x \sim p_D} \left[ \| \nabla_x D_\psi \|^2 \right]$

$R_2(\theta, \psi) = \frac{\gamma}{2} \mathbb{E}_{x \sim p_\theta} \left[ \| \nabla_x D_\psi \|^2 \right]$

Use the 0 centred gradient penalties to stabilize training:

d_loss += r1_penalty(discriminator, real_images, gamma=1.0, disc_args=args, disc_kwargs=kwargs) 
d_loss += r2_penalty(discriminator, fake_images, gamma=1.0, disc_args=args, disc_kwargs=kwargs)

Approximate R1 loss referenced in the Seaweed paper, and a extrapolated version of the R2 loss:

${L}_{aR1} = \lambda \mathbb{E}_{x \sim p_D} \left[ \left\| D(x, c) - D\big(\mathcal{N}(x, \sigma I), c\big) \right\|_2^2 \right]$

${L}_{aR2} = \lambda \mathbb{E}_{x \sim p_\theta} \left[ \left\| D(x, c) - D\big(\mathcal{N}(x, \sigma I), c\big) \right\|_2^2 \right]$

The idea is that, the gradient penalty exists to disencourage large changes in the logits from a small change in the input. The approximation just adds a bit of noise to the input, which works similarly to taking the gradient in this case. This approximation is very helpful as FlashAttention cannot take a second derivative, so the true penalties are much slower to compute.

Use the 0 centred approximate gradient penalties to stabilize training:

d_loss += approximate_r1_loss(discriminator, real_images, sigma=0.01, Lambda=100.0, disc_args=disc_args, disc_kwargs=disc_kwargs) 
d_loss += approximate_r2_loss(discriminator, fake_images, sigma=0.01, Lambda=100.0, disc_args=disc_args, disc_kwargs=disc_kwargs)

Example Outputs

Generated by running python test.py

Relativistic GAN loss + Real R1 + R2 gradient penalty

Real Samples

Relativistic GAN loss + Approximate R1 + R2 gradient penalty

Approximate Samples

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

relativistic_loss-0.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

relativistic_loss-0.1.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file relativistic_loss-0.1.0.tar.gz.

File metadata

  • Download URL: relativistic_loss-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.4

File hashes

Hashes for relativistic_loss-0.1.0.tar.gz
Algorithm Hash digest
SHA256 00d1d1ce3b4261d6514fdd97f34a56e222e7fce6271154ccc41a508bd14ad1e5
MD5 8eb424e308de615f76a544f77f0023e2
BLAKE2b-256 03150bc1a3c023ffee844628a48ccf7f601b07f5dd7b7eb371bc0cfb23c9e837

See more details on using hashes here.

File details

Details for the file relativistic_loss-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for relativistic_loss-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e604d4062d9fa3fd9a7bbcf892b24af0e8925361bfe1675fe75b319573d311cb
MD5 8c8afcd573bd9c9052141173c2e0deec
BLAKE2b-256 b157c953dfb2e37efe0d7942199be12a35d301b6e6c934353214cf422f754b15

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page