# Buggy code
def _get_nearest_labels_for(self, labels):
    already_sampled_negative_labels = set()

    for label in labels:
        plausible_labels = []
        plausible_label_probabilities = []
        for plausible_label in self.label_nearest_map[label]:
            if plausible_label in already_sampled_negative_labels:
                continue
            else:
                plausible_labels.append(plausible_label)
                plausible_label_probabilities.append(
                    self.label_nearest_map[label][plausible_label]
                )
        plausible_label_probabilities /= np.sum(plausible_label_probabilities) + 1e-08
        if len(plausible_labels) > 0:
            num_samples = min(self.num_negative_labels_to_sample, len(plausible_labels))
            sampled_negative_labels = np.random.choice(
                plausible_labels,
                num_samples,
                replace=False,
                p=plausible_label_probabilities,
            )
            already_sampled_negative_labels.update(sampled_negative_labels)

    return already_sampled_negative_labels


# Traceback (ValueError)
Traceback (most recent call last):
File "/home/khalder/prod_stuff/latest_flair/flair/flair/trainers/trainer.py", line 371, in train
loss = self.model.forward_loss(batch_step)
File "/home/khalder/prod_stuff/latest_flair/flair/flair/models/text_classification_model.py", line 793, in forward_loss
sentences = self._get_tars_formatted_sentences(data_points)
File "/home/khalder/prod_stuff/latest_flair/flair/flair/models/text_classification_model.py", line 735, in _get_tars_formatted_sentences
sampled_negative_labels = self._get_nearest_labels_for(positive_labels)
File "/home/khalder/prod_stuff/latest_flair/flair/flair/models/text_classification_model.py", line 708, in _get_nearest_labels_for
p=plausible_label_probabilities)
File "mtrand.pyx", line 924, in numpy.random.mtrand.RandomState.choice
ValueError: probabilities do not sum to 1

# Fixed code
def _get_nearest_labels_for(self, labels):
    already_sampled_negative_labels = set()

    for label in labels:
        plausible_labels = []
        plausible_label_probabilities = []
        for plausible_label in self.label_nearest_map[label]:
            if plausible_label in already_sampled_negative_labels:
                continue
            else:
                plausible_labels.append(plausible_label)
                plausible_label_probabilities.append(
                    self.label_nearest_map[label][plausible_label]
                )

        # make sure the probabilities always sum up to 1
        plausible_label_probabilities = np.array(
            plausible_label_probabilities, dtype="float64"
        )
        plausible_label_probabilities += 1e-08
        plausible_label_probabilities /= np.sum(plausible_label_probabilities)

        if len(plausible_labels) > 0:
            num_samples = min(self.num_negative_labels_to_sample, len(plausible_labels))
            sampled_negative_labels = np.random.choice(
                plausible_labels,
                num_samples,
                replace=False,
                p=plausible_label_probabilities,
            )
            already_sampled_negative_labels.update(sampled_negative_labels)

    return already_sampled_negative_labels