Пример #1
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.vstack([self.infer_on_batch(d) for d in data])

        if predict_proba:
            return preds
        else:
            pl = proba2labels(preds,
                              confident_threshold=self.confident_threshold,
                              classes=self.classes)
            tl = proba2labels(
                preds, confident_threshold=0, classes=self.classes
            )  # [self.classes[np.argmax(preds[i,:])-1] for i in range(len(self.classes))]
            return [(pl[i], dict(zip(tl[i], preds[i])))
                    for i in range(len(pl))]  # [[y[0]] for y in pl]
 def __predict(self, model, input_text):
     rp = model.pipe[0][-1]([input_text])
     for i in range(1, len(model.pipe) - 1):
         rp = model.pipe[i][-1](rp)
     res = model.pipe[-1][-1](rp, predict_proba=True)
     dec = proba2labels(
         res,
         confident_threshold=model.pipe[-1][-1].confident_threshold,
         classes=model.pipe[-1][-1].classes)[0]
     return {
         'decision': dec,
         'confidence': np.sort(res)[0, -len(dec):].tolist()[::-1]
     }
    def __call__(self, data: List[List[str]], *args) -> Tuple[List[list], List[dict]]:
        """
        Infer on the given data

        Args:
            data: list of tokenized text samples
            *args: additional arguments

        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), dtype="float64")

        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])]
Пример #4
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])
        ]
Пример #5
0
    def __call__(self, data: List[List[str]],
                 *args) -> Tuple[List[list], List[dict]]:
        """
        Infer on the given data

        Args:
            data: list of tokenized text samples
            *args: additional arguments

        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), dtype="float64")

        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])
        ]