Project: s41467-024-54375-y | Function: ContrastiveLoss.forward (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Implements the contrastive loss function for twin neural networks, computing the distance-based loss between pairs of embeddings to learn whether they represent the same drug or different drugs

**KEY TERMS:** {
  "euclidean_distance": "distance between embeddings",
  "label": "whether pair is matched (0) or mismatched (1)",
  "margin_neg": "margin for negative pairs",
  "output1": "first embedding",
  "output2": "second embedding"
}

**CONTEXT CODE:** import torch
import torch.nn as nn
import torch.nn.functional as F

class ContrastiveLoss(torch.nn.Module):
    
    def __init__(self, margin_pos, margin_neg):
        super(ContrastiveLoss, self).__init__()
        self.margin_pos = margin_pos
        self.margin_neg = margin_neg
        
    def forward(self, output1, output2, label):
        """Compute contrastive loss between pairs of embeddings"""
        raise NotImplementedError("Function implementation has been masked")

class TwinNN(nn.Module):
    def __init__(self,feat_length):
        super(TwinNN, self).__init__()
        
        self.feat_length = feat_length
        
        self.fc2 = nn.Sequential(
            
            nn.Linear(self.feat_length, 4000),
            nn.BatchNorm1d(4000),
            nn.ReLU(inplace=True),
            nn.Linear(4000, 500),
            nn.BatchNorm1d(500),
            nn.ReLU(inplace=True),
            nn.Linear(500, 250),
            nn.BatchNorm1d(250),
            nn.ReLU(inplace=True),
            nn.Linear(250, 100),
            nn.BatchNorm1d(100),
            nn.ReLU(inplace=True),
            nn.Linear(100, 10))
        
    def forward_once(self, x):

        output = self.fc2(x)
        #output = self.fc2(x)
        return output
        
    def forward(self, input1, input2):
        #print(input1.shape)
        output1 = self.forward_once(input1)
        output2 = self.forward_once(input2)
        return output1, output2



Complete the function implementation replacing "raise NotImplementedError".