mtp/machamp/models/multiseq_decoder.py
def make_output_human_readable(
        self, predictions: torch.Tensor
    ) -> Dict[str, torch.Tensor]:
        """
        Does a simple position-wise argmax over each token, converts indices to string labels, and
        adds a `"tags"` key to the dictionary with the result.
        """
        all_predictions = predictions.cpu().data.numpy()
        if all_predictions.ndim == 3:
            predictions_list = [all_predictions[i] for i in range(all_predictions.shape[0])]
        else:
            predictions_list = [all_predictions]
        all_tags = []
        for predictions in predictions_list:
            outside_index = self.vocab.get_token_index("O", namespace=self.task)

            # @AR: Get the thresholded matrix and prepare the prediction sequence
            pred_over_thresh = (predictions >= self.threshold) * predictions
            #print(pred_over_thresh)
            sequence_token_labels = []
            maxxx = numpy.argmax(predictions, axis=-1).tolist()

            # @AR: For each label set, check if to apply argmax or sigmoid thresh
            j=0
            for pred in pred_over_thresh:
                num_pred_over_thresh = numpy.count_nonzero(pred)
                if (num_pred_over_thresh == 0) or (num_pred_over_thresh == 1):
                    pred_idx_list = [maxxx[j]]

                else:
                    try:
                        outside_position = pred_idx_list.index(outside_index)
                    except ValueError:
                        outside_position = -1
                    # get ranked list
                    tuples = [[score, idx] for idx, score in enumerate(pred) if score > self.threshold and idx != outside_position]
                    # check for max_heads
                    if self.max_heads != 0 and len(tuples) > self.max_heads:
                        tuples = tuples[:self.max_heads]
                    if len(tuples) == 0:
                        tuples = [1.0, outside_position]
                    pred_idx_list = [x[1] for x in tuples]
                    

                sequence_token_labels.append(pred_idx_list)
                j += 1

            # @AR: Create the list of tags to append for the output
            tags = []
            for token_labels in sequence_token_labels:
                curr_labels = []
                for token_label in token_labels:
                    curr_labels.append(
                        self.vocab.get_token_from_index(token_label, namespace=self.task))
                tags.append(curr_labels)

            all_tags.append(tags)
        return all_tags

        Does a simple position-wise argmax over each token, converts indices to string labels, and
        adds a `"tags"` key to the dictionary with the result.
        
Does a simple position-wise argmax over each token, converts indices to string labels, and
adds a `"tags"` key to the dictionary with the result.
Does a simple position - wise argmax over each token converts indices to string labels and adds a ` " tags " ` key to the dictionary with the result .
def make_output_human_readable(
        self, predictions: torch.Tensor
    ) -> Dict[str, torch.Tensor]:
        all_predictions = predictions.cpu().data.numpy()
        if all_predictions.ndim == 3:
            predictions_list = [all_predictions[i] for i in range(all_predictions.shape[0])]
        else:
            predictions_list = [all_predictions]
        all_tags = []
        for predictions in predictions_list:
            outside_index = self.vocab.get_token_index("O", namespace=self.task)
            pred_over_thresh = (predictions >= self.threshold) * predictions
            sequence_token_labels = []
            maxxx = numpy.argmax(predictions, axis=-1).tolist()
            j=0
            for pred in pred_over_thresh:
                num_pred_over_thresh = numpy.count_nonzero(pred)
                if (num_pred_over_thresh == 0) or (num_pred_over_thresh == 1):
                    pred_idx_list = [maxxx[j]]
                else:
                    try:
                        outside_position = pred_idx_list.index(outside_index)
                    except ValueError:
                        outside_position = -1
                    tuples = [[score, idx] for idx, score in enumerate(pred) if score > self.threshold and idx != outside_position]
                    if self.max_heads != 0 and len(tuples) > self.max_heads:
                        tuples = tuples[:self.max_heads]
                    if len(tuples) == 0:
                        tuples = [1.0, outside_position]
                    pred_idx_list = [x[1] for x in tuples]
                sequence_token_labels.append(pred_idx_list)
                j += 1
            tags = []
            for token_labels in sequence_token_labels:
                curr_labels = []
                for token_label in token_labels:
                    curr_labels.append(
                        self.vocab.get_token_from_index(token_label, namespace=self.task))
                tags.append(curr_labels)
            all_tags.append(tags)
        return all_tags
def make_output_human_readable ( self , predictions : torch . Tensor ) -> Dict [ str , torch . Tensor ] : all_predictions = predictions . cpu ( ) . data . numpy ( ) if all_predictions . ndim == 3 : predictions_list = [ all_predictions [ i ] for i in range ( all_predictions . shape [ 0 ] ) ] else : predictions_list = [ all_predictions ] all_tags = [ ] for predictions in predictions_list : outside_index = self . vocab . get_token_index ( "O" , namespace = self . task ) pred_over_thresh = ( predictions >= self . threshold ) * predictions sequence_token_labels = [ ] maxxx = numpy . argmax ( predictions , axis = - 1 ) . tolist ( ) j = 0 for pred in pred_over_thresh : num_pred_over_thresh = numpy . count_nonzero ( pred ) if ( num_pred_over_thresh == 0 ) or ( num_pred_over_thresh == 1 ) : pred_idx_list = [ maxxx [ j ] ] else : try : outside_position = pred_idx_list . index ( outside_index ) except ValueError : outside_position = - 1 tuples = [ [ score , idx ] for idx , score in enumerate ( pred ) if score > self . threshold and idx != outside_position ] if self . max_heads != 0 and len ( tuples ) > self . max_heads : tuples = tuples [ : self . max_heads ] if len ( tuples ) == 0 : tuples = [ 1.0 , outside_position ] pred_idx_list = [ x [ 1 ] for x in tuples ] sequence_token_labels . append ( pred_idx_list ) j += 1 tags = [ ] for token_labels in sequence_token_labels : curr_labels = [ ] for token_label in token_labels : curr_labels . append ( self . vocab . get_token_from_index ( token_label , namespace = self . task ) ) tags . append ( curr_labels ) all_tags . append ( tags ) return all_tags
Does a simple position-wise argmax over each token, converts indices to string labels, and
adds a `"tags"` key to the dictionary with the result.
Does a simple position - wise argmax over each token converts indices to string labels and adds a ` " tags " ` key to the dictionary with the result .
"""
        Does a simple position-wise argmax over each token, converts indices to string labels, and
        adds a `"tags"` key to the dictionary with the result.
        """ # @AR: Get the thresholded matrix and prepare the prediction sequence #print(pred_over_thresh) # @AR: For each label set, check if to apply argmax or sigmoid thresh # get ranked list # check for max_heads # @AR: Create the list of tags to append for the output
self
predictions
torch.Tensor