This should answer your question:

class WGANLoss(torch.nn.Module):

    def __init__(self):
        super(WGANLoss, self).__init__()

    def forward(self,predicted,true):
        WGAN = (-2*true+1) * torch.mean(predicted)
        return WGAN
       
def forward_D(latent_code,Img_real):
    Img_fake = G(latent_code)
    d_real = D(Img_real)
    d_fake = D(Img_fake.detach())
    
    return d_real,d_fake
    
def forward_G(latent_code):
    Img_fake = G(latent_code)
    d_fake = D(Img_fake)
    
    return d_fake


def backward_G(d_fake,optim_G):
    g_loss = compute_G_loss(d_fake)
    g_loss.backward()
    optim_G.step()


def backward_D(d_real,d_fake,optim_D):
    d_loss = compute_D_loss(d_real,d_fake)
    d_loss.backward(retain_graph=False)
    optim_D.step()


def compute_G_loss(d_fake):
    g_adv_loss = compute_adv_loss(d_fake, True)
    
    return g_adv_loss

def compute_D_loss(d_real,d_fake):
    d_adv_loss_real = compute_adv_loss(d_real, True)
    d_adv_loss_fake = compute_adv_loss(d_fake, False)
    
    d_adv_loss = (d_adv_loss_real + d_adv_loss_fake)

    return d_adv_loss
    

'''
	Define here latent_codes, Imgs, G (Generator) and D (Discriminator)
'''
compute_adv_loss = WGAN()

optim_G = optim.Adam(G.parameters())
optim_D = optim.Adam(D.parameters())

for latent_code,Img_real in zip(latent_codes,Imgs):

	# ===update D===
	optim_D.zero_grad()
	d_real,d_fake = forward_D(latent_code,Img_real)
	backward_D(d_real,d_fake,optim_D)

	# ===update G===
	optim_G.zero_grad()
	d_fake = forward_G(latent_code)
	backward_G(d_fake,optim_G)