Exemplo n.º 1
0
    def predicted_class_index(self) -> int:
        "Returns predicted class index (int) for model with last calculated `input_ids`"
        if len(self.input_ids) > 0:
            # we call this before _forward() so it has to be calculated twice
            preds = self.model(self.input_ids)[0]
            self.pred_class = torch.argmax(torch.softmax(preds, dim=0)[0])
            return torch.argmax(torch.softmax(preds, dim=1)[0]).cpu().detach().numpy()

        else:
            raise InputIdsNotCalculatedError("input_ids have not been created yet.`")
Exemplo n.º 2
0
    def predicted_class_index(self):
        if self.input_ids is not None:
            preds = self.model(self.input_ids)[0]
            self.pred_class = torch.argmax(torch.softmax(preds, dim=0)[0])
            return torch.argmax(torch.softmax(preds,
                                              dim=1)[0]).detach().numpy()

        else:
            raise InputIdsNotCalculatedError(
                "input_ids have not been created yet. Please call `get_attributions()`"
            )
Exemplo n.º 3
0
    def predicted_class_index(self):
        if len(self.input_ids) > 0:
            # we call this before _forward() so it has to be calculated twice
            preds = self.model(self.input_ids)[0]
            self.pred_class = torch.argmax(torch.softmax(preds, dim=0)[0])
            return torch.argmax(torch.softmax(
                preds, dim=1)[0]).cpu().detach().numpy()

        else:
            raise InputIdsNotCalculatedError(
                "input_ids have not been created yet. Please call `get_attributions()`"
            )
    def end_pos(self):
        if len(self.input_ids) > 0:
            preds = self._get_preds(
                self.input_ids,
                self.token_type_ids,
                self.position_ids,
                self.attention_mask,
            )

            preds = preds[1]
            return int(preds.argmax())
        else:
            raise InputIdsNotCalculatedError("input_ids have not been created yet.`")
    def predicted_answer(self):
        if len(self.input_ids) > 0:
            preds = self._get_preds(
                self.input_ids,
                self.token_type_ids,
                self.position_ids,
                self.attention_mask,
            )

            start = preds[0].argmax()
            end = preds[1].argmax()
            return " ".join(self.decode(self.input_ids)[start : end + 1])
        else:
            raise InputIdsNotCalculatedError("input_ids have not been created yet.`")
Exemplo n.º 6
0
    def predicted_class_indexes(self) -> List[int]:
        "Returns the predicted class indexes (int) for model with last calculated `input_ids`"
        if len(self.input_ids) > 0:

            preds = self.model(self.input_ids)
            preds = preds[0]
            self.pred_class = torch.softmax(preds, dim=2)[0]

            return (torch.argmax(torch.softmax(preds, dim=2),
                                 dim=2)[0].cpu().detach().numpy())

        else:
            raise InputIdsNotCalculatedError(
                "input_ids have not been created yet.`")
    def start_pos(self):
        "Returns predicted start position for answer"
        if len(self.input_ids) > 0:
            preds = self._get_preds(
                self.input_ids,
                self.token_type_ids,
                self.position_ids,
                self.attention_mask,
            )

            preds = preds[0]
            return int(preds.argmax())
        else:
            raise InputIdsNotCalculatedError(
                "input_ids have not been created yet.`")