Suppose I have myNetwork class defined as:
class myNetwork(nn.Module):
    def __init__(self, a, b, embedding_dim):
        super(modeler, self).__init__()
        self.embed1 = nn.Embedding(a, embedding_dim)
        self.embed2 = nn.Embedding(b, embedding_dim)

        
    def forward(self, idx1, idx2, context_idxs1, context_idxs2):
        
        embeds1 = self.embed1(idx1)
        embeds2 = self.embed2(idx2)

        embeds1_context = self.embed1(context_idxs1)
        embeds2_context = self.embed2(context_idxs2)

        embeds1_context  = embeds1_context.sum(1) / embeds1_context.data.nelement()
        embeds2_context  = embeds2_context.sum(1) / embeds2_context.data.nelement()
        
        loss1 = torch.sum((embeds1 - embeds1_context)**2) / embeds1.data.nelement()
        loss2 = torch.sum((embeds2 - embeds2_context)**2) / embeds2.data.nelement()

        output = torch.sum((embeds1- embeds2)**2,1)

        return output, loss1, loss2

In the training loop, I am going call the forward function twice (one for positive example and the other for  negative example) and I am going to minimize the margin loss:
def training(self):
    model = myNetwork()
        
    criterion = nn.MarginRankingLoss(margin=self.margin))

    optimizer = optim.Adam(model.parameters(), lr = self.lRate)
        
    for epoch in range(numEpoch):
        for batch_idx, batch in enumerate(train_loader):
            pos, loss1_pos, loss_2_pos = model(xxx)
            neg, loss1_neg, loss_2_neg = model(xxx)

            loss = criterion_trans(pos, neg, Variable(torch.FloatTensor([-1])))

            loss = loss + (loss1_pos + loss_2_pos + loss1_neg + loss_2_neg)
                
            loss.backward()
            optimizer.step()

From the above code, I wanted to 1) compute margin based loss between positive sample and negative sample, 2) Minimizing distance between a latent vector and its context latent vectors. (e.g., (embeds1 - embeds1_context)^2, where I also want to update indices of embed1 that generated embeds1_context.
I am sure that loss is computed correctly. But I wonder if loss1_pos, loss_2_pos, loss1_neg, loss_2_neg are computed as my intention.
Can anyone help me?