Prepin
Log in

Adjust this PyTorch pseudo-code to implement negative sampling when the pos_ids are available. Here are the shapes for the variables: label has a shape of [batch_s, pos_ids], hidden has a shape of [batch_s, ch], and logits has a shape of [ch, C]. The output is calculated as out = np.dot(logits, hidden(X)), resulting in a shape of [batch_s, C]. The loss is then computed using cross_entropy(out, label).

Approach / Explanation

This question requires familiarity with PyTorch programming and an understanding of negative sampling. The scenario presented involves modifying existing PyTorch pseudo-code to implement negative sampling, given that pos_ids (identifiers for positive samples) are available. In machine learning, negative sampling is a technique commonly utilized in scenarios where the overwhelming majority of potential samples are negative. By deliberately including a greater number of negative samples during training, the model's performance on negative samples can be enhanced. First, let's analyze the current implementation: label, hidden, and logits are tensors. The operation np.dot(logits, hidden(X)) represents a matrix multiplication between the logits and the hidden representations to produce the output. The cross_entropy(out, label) function calculates the cross-entropy loss between the generated output and the actual labels. Here are the steps to consider for implementing negative sampling: Identify the negative samples: Typically, negatives are those samples that are not included in the positive sample set. You'll need to determine how to select these samples. Modify the code to include these negative samples in the training process. Consider how to adjust the loss computation process to account for the inclusion of these negative samples.

Suggested Answer

Take a look at the following example where we perform random negative sampling and modify the loss function to binary cross-entropy. This could be a viable method to fulfill the requirement. import torch from torch import nn def negative_sampling ( pos_ids , ch , C , num_negative_samples ) : neg_ids = torch . randint ( 0 , C , ( label . size ( 0 ) , num_negative_samples ) ) label = torch . ones ( ( label . size ( 0 ) , 1 ) ) id = torch . cat ( ( pos_ids , neg_ids ) , 1 ) label = torch . cat ( ( label , torch . zeros ( ( label . size ( 0 ) , num_negative_samples ) ) ) , 1 ) hidden = hidden [ id ] logits = logits [ id ] out = torch . bmm ( hidden , logits . unsqueeze ( - 1 ) ) . squeeze ( - 1 ) loss = nn . BCEWithLogitsLoss ( ) ( out , label ) return loss

Alternative Answer

Alternatively, imagine a scenario where you have chosen a specific method for selecting negative samples (as opposed to the random choice in Example 1) and have saved these samples in neg_ids . This method applies a similar adjustment to the pseudo-code. def negative_samplingSpecific ( pos_ids , neg_ids , hidden , logits ) : all_ids = torch . cat ( ( pos_ids , neg_ids ) , dim = - 1 ) all_hidden = hidden [ all_ids ] all_logits = logits [ all_ids ] # Positive samples are labeled as 1, while negative samples are labeled as 0 labels = torch . cat ( ( torch . ones_like ( pos_ids ) , torch . zeros_like ( neg_ids ) ) , dim = - 1 ) out = torch . bmm ( all_logits , all_hidden . unsqueeze ( - 1 ) ) . squeeze ( - 1 ) loss = nn . BCEWithLogitsLoss ( ) ( out , labels ) return loss Note: Both examples utilize PyTorch instead of the numpy library ( np used in your original code) to allow for automatic differentiation and GPU acceleration.

Question Details

Difficulty & Category

Medium
program sense

Companies

SAP

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.