WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

PyTorch Β· Basic Β· question 19 of 100

What are the common techniques for handling class imbalance in PyTorch?

πŸ“• Buy this interview preparation book: 100 PyTorch questions & answers β€” PDF + EPUB for $5

Class imbalance refers to the training data being skewed towards one or more classes, leading to suboptimal performance of a machine learning model. In PyTorch, there are several techniques that can be used to handle class imbalance:

1. Weighted loss function: The simplest method to handle class imbalance is to use a weighted loss function. This involves assigning higher weights to the minority class during training. In PyTorch, the CrossEntropyLoss function allows us to pass a weight tensor that determines the weight of each class. For example, if we have a binary classification problem with class 0 and class 1, and we want to give the minority class (class 1) a weight of 2, we can use the following code:

  weights = torch.tensor([1, 2])
  criterion = nn.CrossEntropyLoss(weight=weights)

2. Oversampling: This involves increasing the number of samples in the minority class to balance the distribution of classes. PyTorch provides the torch.utils.data.sampler.WeightedRandomSampler class, which can be used to sample from the dataset according to the inverse of the class frequencies. This ensures that the minority class is oversampled during training. For example, the following code shows how to use WeightedRandomSampler to oversample the minority class:

  from torch.utils.data import WeightedRandomSampler
  
  # assuming train_dataset is a PyTorch dataset object
  class_count = [10, 100] # assuming 10 samples in class 0 and 100 samples in class 1
  weights = 1 / torch.Tensor(class_count)
  samples_weight = [weights[t] for t in train_dataset.targets]
  sampler = WeightedRandomSampler(samples_weight, len(samples_weight))
  train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=sampler)

3. Undersampling: This involves reducing the number of samples in the majority class to balance the distribution of classes. PyTorch provides the torch.utils.data.sampler.RandomSampler class, which can be used to sample from the dataset randomly. We can use this sampler to select a subset of samples from the majority class. For example, the following code shows how to use RandomSampler to undersample the majority class:

  from torch.utils.data import RandomSampler
  
  # assuming train_dataset is a PyTorch dataset object
  class_count = [10, 100] # assuming 10 samples in class 0 and 100 samples in class 1
  minority_class = 0
  minority_count = class_count[minority_class]
  majority_class = 1
  majority_count = class_count[majority_class]
  majority_idx = [i for i in range(len(train_dataset.targets)) if train_dataset.targets[i] == majority_class]
  majority_idx_undersampled = random.sample(majority_idx, minority_count)
  sampler = RandomSampler(majority_idx_undersampled + [i for i in range(len(train_dataset.targets)) if i not in majority_idx])
  train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, sampler=sampler)

4. Combination of oversampling and undersampling: This involves using both oversampling and undersampling to balance the distribution of classes. This can be done by oversampling the minority class and then undersampling the majority class to the same number of samples as the minority class. For example, the following code shows how to use WeightedRandomSampler and RandomSampler to balance the data:

  from torch.utils.data import ConcatDataset
  
  # assuming train_dataset is a PyTorch dataset object
  class_count = [10, 100] # assuming 10 samples in class 0 and 100 samples in class 1
  minority_class = 0
  minority_count = class_count[minority_class]
  majority_class = 1
  majority_count = class_count[majority_class]
  minority_dataset = [train_dataset[i] for i in range(len(train_dataset)) if train_dataset.targets[i] == minority_class]
  majority_dataset = [train_dataset[i] for i in range(len(train_dataset)) if train_dataset.targets[i] == majority_class]
  
  oversampled_minority_dataset = ConcatDataset([minority_dataset] * (majority_count // minority_count))
  majority_idx = [i for i in range(len(majority_dataset))]
  majority_idx_undersampled = random.sample(majority_idx, minority_count)
  undersampled_majority_dataset = torch.utils.data.Subset(majority_dataset, majority_idx_undersampled)
  
  balanced_dataset = torch.utils.data.ConcatDataset([undersampled_majority_dataset, oversampled_minority_dataset])
  sampler = RandomSampler(balanced_dataset, replacement=True, num_samples=len(balanced_dataset))
  train_loader = torch.utils.data.DataLoader(balanced_dataset, batch_size=batch_size, sampler=sampler)

All of these techniques can be used to handle class imbalance in PyTorch. The choice of technique depends on the specific data and problem, and it is common to experiment with multiple techniques to find the one that works best for a particular problem.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic PyTorch interview β€” then scores it.
πŸ“ž Practice PyTorch β€” free 15 min
πŸ“• Buy this interview preparation book: 100 PyTorch questions & answers β€” PDF + EPUB for $5

All 100 PyTorch questions Β· All topics