code: def test_melgan_generator_and_discriminator(dict_g, dict_d):
      # setup
      batch_size = 2
      batch_length = 512
      args_g = make_melgan_generator_args(**dict_g)
      args_d = make_melgan_discriminator_args(**dict_d)
      y = torch.randn(batch_size, 1, batch_length)
      c = torch.randn(
          batch_size,
          args_g["in_channels"],
          batch_length // np.prod(args_g["upsample_scales"]),
      )
      model_g = MelGANGenerator(**args_g)
      model_d = MelGANMultiScaleDiscriminator(**args_d)
      gen_adv_criterion = GeneratorAdversarialLoss()
      dis_adv_criterion = DiscriminatorAdversarialLoss()
      feat_match_criterion = FeatureMatchLoss()
      optimizer_g = torch.optim.Adam(model_g.parameters())
      optimizer_d = torch.optim.Adam(model_d.parameters())
  
      # check generator trainable
      y_hat = model_g(c)
      p_hat = model_d(y_hat)
      adv_loss = gen_adv_criterion(p_hat)
      with torch.no_grad():
          p = model_d(y)
      fm_loss = feat_match_criterion(p_hat, p)
      loss_g = adv_loss + fm_loss
      optimizer_g.zero_grad()
      loss_g.backward()
      optimizer_g.step()
  
      # check discriminator trainable
      p = model_d(y)
      p_hat = model_d(y_hat.detach())
      real_loss, fake_loss = dis_adv_criterion(p_hat, p)
      loss_d = real_loss + fake_loss
      optimizer_d.zero_grad()
      loss_d.backward()
      optimizer_d.step()
  
file_path: espnet/test/espnet2/gan_tts/melgan/test_melgan.py
imports: import numpy
  import pytest
  import torch
  from espnet2.gan_tts.hifigan.loss import DiscriminatorAdversarialLoss, FeatureMatchLoss, GeneratorAdversarialLoss
  from espnet2.gan_tts.melgan import MelGANGenerator, MelGANMultiScaleDiscriminator
label_desc: Use image node_id 3 for calling a global function with example usage: test_melgan_generator_and_discriminator(dict_g, dict_d) without return types