class_docstr: FactorVAE model.
  
  Args:
      model_config (FactorVAEConfig): The Variational Autoencoder configuration setting the main
          parameters of the model.
  
      encoder (BaseEncoder): An instance of BaseEncoder (inheriting from `torch.nn.Module` which
          plays the role of encoder. This argument allows you to use your own neural networks
          architectures if desired. If None is provided, a simple Multi Layer Preception
          (https://en.wikipedia.org/wiki/Multilayer_perceptron) is used. Default: None.
  
      decoder (BaseDecoder): An instance of BaseDecoder (inheriting from `torch.nn.Module` which
          plays the role of decoder. This argument allows you to use your own neural networks
          architectures if desired. If None is provided, a simple Multi Layer Preception
          (https://en.wikipedia.org/wiki/Multilayer_perceptron) is used. Default: None.
  
  
  .. note::
      For high dimensional data we advice you to provide you own network architectures. With the
      provided MLP you may end up with a ``MemoryError``.
class_docstr_tok: ["FactorVAE","model",".","Args",":","model_config","(","FactorVAEConfig",")",":","The","Variational","Autoencoder","configuration","setting","the","main","parameters","of","the","model",".","encoder","(","BaseEncoder",")",":","An","instance","of","BaseEncoder","(","inheriting","from","`","torch.nn.Module","`","which","plays","the","role","of","encoder",".","This","argument","allows","you","to","use","your","own","neural","networks","architectures","if","desired",".","If","None","is","provided",",","a","simple","Multi","Layer","Preception","(","https",":","\/\/en.wikipedia.org\/wiki\/Multilayer_perceptron",")","is","used",".","Default",":","None",".","decoder","(","BaseDecoder",")",":","An","instance","of","BaseDecoder","(","inheriting","from","`","torch.nn.Module","`","which","plays","the","role","of","decoder",".","This","argument","allows","you","to","use","your","own","neural","networks","architectures","if","desired",".","If","None","is","provided",",","a","simple","Multi","Layer","Preception","(","https",":","\/\/en.wikipedia.org\/wiki\/Multilayer_perceptron",")","is","used",".","Default",":","None",".","..","note",":",":","For","high","dimensional","data","we","advice","you","to","provide","you","own","network","architectures",".","With","the","provided","MLP","you","may","end","up","with","a","``","MemoryError","``","."]
code: def loss_function(self, recon_x, x, mu, log_var, z, z_bis_permuted):
      N = z.shape[0]  # batch size
  
      if self.model_config.reconstruction_loss == "mse":
          recon_loss = 0.5 * F.mse_loss(
              recon_x.reshape(x.shape[0], -1),
              x.reshape(x.shape[0], -1),
              reduction="none",
          ).sum(dim=-1)
  
      elif self.model_config.reconstruction_loss == "bce":
          recon_loss = F.binary_cross_entropy(
              recon_x.reshape(x.shape[0], -1),
              x.reshape(x.shape[0], -1),
              reduction="none",
          ).sum(dim=-1)
  
      KLD = -0.5 * torch.sum(
          1 + log_var - mu.pow(2) - log_var.exp(), dim=-1
      )
  
      latent_adversarial_score = self.discriminator(z)
  
      TC = (
          latent_adversarial_score[:, 0]
          - latent_adversarial_score[:, 1]
      ).mean()
      autoencoder_loss = recon_loss + KLD + self.gamma * TC
  
      # discriminator loss
      permuted_latent_adversarial_score = self.discriminator(
          z_bis_permuted
      )
  
      true_labels = (
          torch.ones(z_bis_permuted.shape[0], requires_grad=False)
          .type(torch.LongTensor)
          .to(z.device)
      )
      fake_labels = (
          torch.zeros(z.shape[0], requires_grad=False)
          .type(torch.LongTensor)
          .to(z.device)
      )
  
      TC_permuted = F.cross_entropy(
          latent_adversarial_score, fake_labels
      ) + F.cross_entropy(
          permuted_latent_adversarial_score, true_labels
      )
  
      discriminator_loss = 0.5 * TC_permuted
  
      return (
          (recon_loss).mean(dim=0),
          (autoencoder_loss).mean(dim=0),
          (discriminator_loss).mean(dim=0),
      )
  
imports: import os
  from typing import Optional
  import torch
  import torch.nn.functional
  from ...customexception import BadInheritanceError
  from ...data.datasets import BaseDataset
  from ..base.base_utils import ModelOutput
  from ..nn import BaseDecoder, BaseDiscriminator, BaseEncoder
  from ..vae import VAE
  from .factor_vae_config import FactorVAEConfig
  from .factor_vae_utils import FactorVAEDiscriminator
label_desc: Use image node_id 4 for calling the FactorVAE obj's underlying member method code with example usage: obj.loss_function(recon_x, x, mu, log_var, z, z_bis_permuted) and returns: recon_loss,  autoencoder_loss,  discriminator_loss