concept confidence sampling in category machine learning

appears as: confidence sampling
Human-in-the-Loop Machine Learning

This is an excerpt from Manning's book Human-in-the-Loop Machine Learning.

Here’s an implementation of margin of confidence sampling using PyTorch:

def margin_confidence(self, prob_dist, sorted=False):
        """
        Returns the uncertainty score of a probability distribution using
        margin of confidence sampling in a 0-1 range where 1 is the most uncertain
       
        Assumes probability distribution is a pytorch tensor, like:
            tensor([0.0321, 0.6439, 0.0871, 0.2369])
           
        Keyword arguments:
            prob_dist -- a pytorch tensor of real numbers between 0 and 1 that total to 1.0
            sorted -- if the probability distribution is pre-sorted from largest to smallest
        """
        if not sorted:
            prob_dist, _ = torch.sort(prob_dist, descending=True)
       
        difference = (prob_dist.data[0] - prob_dist.data[1])
        margin_conf = 1 - difference
       
        return margin_conf.item()