concept uncertainty sampling in category machine learning

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

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

From our introduction in Chapter 1, recall this graphic showing how uncertainty sampling should find items close to the decision boundary, like in Figure 3.6:

def get_high_uncertainty_cluster(self, model, unlabeled_data, method, feature_method,
                                      number=10, num_clusters=20, max_epochs=10, limit=10000):
        """Gets items from the cluster with the highest average uncertainty
        
        Keyword arguments:
            model -- machine learning model to get predictions from to determine uncertainty
            unlabeled_data -- data that does not yet have a label
            method -- method for uncertainty sampling (eg: least_confidence())
            feature_method -- the method for extracting features from your data
            number -- number of items to sample
            num_clusters -- the number of clusters to create
            max_epochs -- maximum number of epochs to create clusters
            limit -- sample from only this many items for faster sampling (-1 = no limit)      
        """
               
        if limit > 0:
            shuffle(unlabeled_data)
            unlabeled_data = unlabeled_data[:limit]            
 
        unlabeled_clusters = CosineClusters(num_clusters)   
        unlabeled_clusters.add_random_training_items(unlabeled_data)
       
        for i in range(0, max_epochs):    #A
            print("Epoch "+str(i))
            added = unlabeled_clusters.add_items_to_best_cluster(unlabeled_data)
            if added == 0:
                break
   
        # get scores
       
        most_uncertain_cluster = None
        highest_average_uncertainty = 0.0
       
        # for each cluster of unlabeled data
        for cluster in unlabeled_clusters.clusters:
            total_uncertainty = 0.0
            count = 0
 
            item_keys = list(cluster.members.keys())
            
            for key in item_keys:
                item = cluster.members[key]
                text = item[1] # the text for the message
               
                feature_vector = feature_method(text)
                hidden, logits, log_probs = model(feature_vector, return_all_layers=True) 
   
                prob_dist = torch.exp(log_probs) # the probability distribution of our prediction
               
                score = method(prob_dist.data[0]) # get the specific type of uncertainty sampling
               
                total_uncertainty += 1.0
                count += 1
               
            average_uncertainty = total_uncertainty / count    #B
            if average_uncertainty > highest_average_uncertainty:
                highest_average_uncertainty = average_uncertainty
                most_uncertain_cluster = cluster
           
        samples = most_uncertain_cluster.get_random_members(number)
           
        return samples