for epoch in tqdm(range(epochs)):
        model.train()
        epoch_loss = 0
        for batch in tqdm(train_iterator):
            src_sentence = Variable(batch['src_sentence']).cuda()
            src_speaker = Variable(batch['src_speaker']).cuda()
            trg_sentence = Variable(batch['trg_sentence']).cuda()
            trg_speaker = Variable(batch['trg_speaker']).cuda()

            optimizer.zero_grad()
            output, predicted_speaker = model(src_sentence,src_speaker, trg_sentence, trg_speaker)
            
            output = output[1:].view(-1, output.shape[-1])
            trg = trg_sentence.permute(1,0)
            trg = trg[1:].reshape(-1)

            predicted_speaker = log_softmax(predicted_speaker)
            
            loss1 = criterion1(output, trg)
            loss2 = criterion2(predicted_speaker, trg_speaker)
            loss = loss1 + loss2 
            loss.backward()

            torch.nn.utils.clip_grad_norm_(model.parameters(), 1)

            optimizer.step()
            epoch_loss += loss.item()

This is my code for training and computing loss