# DiscriminatorLoss

### *class* deepinv.loss.adversarial.DiscriminatorLoss(weight_adv=1.0, D=None, device='cpu', \*\*kwargs)

Bases: [`Loss`](https://deepinv.org/api/stubs/deepinv.loss.Loss.html.md#deepinv.loss.Loss)

Base discriminator adversarial loss.

Override the forward function to
call `adversarial_loss` with quantities depending on your specific GAN model.

For examples, see [`deepinv.loss.adversarial.SupAdversarialDiscriminatorLoss`](https://deepinv.org/api/stubs/deepinv.loss.adversarial.SupAdversarialDiscriminatorLoss.html.md#deepinv.loss.adversarial.SupAdversarialDiscriminatorLoss),
[`deepinv.loss.adversarial.UnsupAdversarialDiscriminatorLoss`](https://deepinv.org/api/stubs/deepinv.loss.adversarial.UnsupAdversarialDiscriminatorLoss.html.md#deepinv.loss.adversarial.UnsupAdversarialDiscriminatorLoss).

See [Imaging inverse problems with adversarial networks](https://deepinv.org/auto_examples/adversarial-learning/demo_gan_imaging.html.md#sphx-glr-auto-examples-adversarial-learning-demo-gan-imaging-py) for formula.

* **Parameters:**
  * **weight_adv** ([*float*](https://docs.python.org/3.12/library/functions.html#float)) – weight for adversarial loss, defaults to 1.
  * **D** ([*torch.nn.Module*](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module)) – discriminator network.
    If not specified, `D` must be provided in `forward`, defaults to `None`.
  * **device** ([*str*](https://docs.python.org/3.12/library/stdtypes.html#str)) – torch device, defaults to `"cpu"`.

#### adversarial_loss(real, fake, D=None)

Typical adversarial loss in GAN discriminators.

* **Parameters:**
  * **real** ([*torch.Tensor*](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) – image labelled as real, typically one originating from training set
  * **fake** ([*torch.Tensor*](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) – image labelled as fake, typically a reconstructed image
  * **D** ([*torch.nn.Module*](https://docs.pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module)) – discriminator/critic/classifier model. If None, then D passed from \_\_init_\_ used. Defaults to None.
* **Returns:**
  ([`torch.Tensor`](https://docs.pytorch.org/docs/stable/tensors.html#torch.Tensor)) discriminator adversarial loss

<a id="sphx-glr-backref-deepinv-loss-adversarial-discriminatorloss"></a>

## Examples using `DiscriminatorLoss`:

<div class="sphx-glr-thumbnails">
<!-- thumbnail-parent-div-open --><div class="sphx-glr-thumbcontainer" tooltip="This example shows you how to train various networks using adversarial training for deblurring problems. We demonstrate running training and inference using a conditional GAN (i.e. DeblurGAN), CSGM, AmbientGAN and UAIR implemented in the library, and how to simply train your own GAN by using deepinv.training.AdversarialTrainer. These examples can also be easily extended to train more complicated GANs such as CycleGAN.">![](auto_examples/adversarial-learning/images/thumb/sphx_glr_demo_gan_imaging_thumb.png)

[Imaging inverse problems with adversarial networks](https://deepinv.org/auto_examples/adversarial-learning/demo_gan_imaging.html.md)

  <div class="sphx-glr-thumbnail-title">Imaging inverse problems with adversarial networks</div>
</div>
<!-- thumbnail-parent-div-close --></div>
