示例#1
0
    def infer(self, data, predict_proba=False, *args):
        """
        Infer on the given data
        Args:
            data: single sentence or [list of sentences, list of labels] or
                    [list of sentences] or generator of sentences
            predict_proba: whether to return probabilities distribution or only labels-predictions
            *args:

        Returns:
            for each sentence:
                vector of probabilities to belong with each class
                or list of labels sentence belongs with
        """
        if type(data) is str:
            preds = self.infer_on_batch([data])[0]
            preds = np.array(preds)
            if predict_proba:
                return preds
            else:
                return proba2labels([preds], confident_threshold=self.confident_threshold, classes=self.classes)[0]

        elif inspect.isgeneratorfunction(data):
            preds = []
            for step, batch in enumerate(data):
                preds.extend(self.infer_on_batch(batch))
            preds = np.array(preds)
        elif type(data) is list:
            preds = self.infer_on_batch(data)
            preds = np.array(preds)
        else:
            raise ConfigError("Not understand data type for inference")

        if predict_proba:
            return preds
        else:
            return proba2labels(preds, confident_threshold=self.confident_threshold, classes=self.classes)
示例#2
0
    def __call__(self, data, *args):
        """
        Infer on the given data
        Args:
            data: [list of sentences]
            *args:

        Returns:
            for each sentence:
                vector of probabilities to belong with each class
                or list of labels sentence belongs with
        """
        preds = np.array(self.infer_on_batch(data))

        labels = proba2labels(preds, confident_threshold=self.opt['confident_threshold'], classes=self.classes)
        return labels, [dict(zip(self.classes, preds[i])) for i in range(preds.shape[0])]
示例#3
0
    def __call__(self, data, predict_proba=False, *args):
        """
        Infer on the given data
        Args:
            data: [list of sentences]
            predict_proba: whether to return probabilities distribution or only labels-predictions
            *args:

        Returns:
            for each sentence:
                vector of probabilities to belong with each class
                or list of labels sentence belongs with
        """
        preds = np.array(self.infer_on_batch(data))

        if predict_proba:
            return preds
        else:
            return proba2labels(preds, confident_threshold=self.confident_threshold, classes=self.classes)