Ejemplo n.º 1
0
    def evaluate(self, predict, truth):
        """Compute tags.
        :param predict: Tensor, [batch_size, max_len, tag_size]
        :param truth: Tensor, [batch_size, max_len]
        :return:
        """

        prediction = self._model.prediction(predict, self.mask)
        prediction = prediction.cpu().numpy()
        truth = truth.cpu().numpy()
        return [
            Action.cut_pad(prediction, self.seq_len),
            Action.cut_pad(truth, self.seq_len)
        ]
Ejemplo n.º 2
0
 def test_case_1(self):
     x = [1, 2, 3, 4, 5, 6, 7, 8]
     y = [1, 1, 1, 1, 2, 2, 2, 2]
     data = []
     for i in range(len(x)):
         data.append([[x[i]], [y[i]]])
     data = Batchifier(SequentialSampler(data),
                       batch_size=2,
                       drop_last=False)
     action = Action()
     for batch_x in action.make_batch(data,
                                      use_cuda=False,
                                      output_length=True,
                                      max_len=None):
         print(batch_x)
Ejemplo n.º 3
0
    def make_eval_output(self, ):
        """
        Customize Tester outputs. you can overload to change the Evaluation standard
        """
        predictions = self.result
        y_label = self.lable
        if self.all_mask:
            mask = self.all_mask
        else:
            mask = [None] * len(predictions)
        Acc_num, All_num, Pred_num, Label_num, Pred_right_num = 0, 0, 0, 0, 0  #Acc_num is the number of exact match right preds
        #All_num is the sum number of samples
        #Pred_num is the number of predicted nonzero labels
        #Label_num is the number of nonzero labels
        #Pred_right_num is the number of predicted right nonzero labels
        for p, y, m in zip(predictions, y_label, mask):
            acc_num, all_num, pred_num, label_num, pred_right_num = Action.get_real(
                p, y, m
            )  #if only care about acc, ignore pred_num, label_num, pred_right_num.
            # if lable 0 is negtive sample, pred_num, label_num, pred_right_num work too.
            Acc_num += acc_num
            All_num += all_num
            Pred_num += pred_num
            Label_num += label_num
            Pred_right_num += pred_right_num

        accuracy = Acc_num / All_num
        precision = Pred_right_num / Pred_num
        recall = Pred_right_num / Label_num
        f1_score = 2 * precision * recall / (precision + recall)
        self.print_output(accuracy, precision, recall, f1_score)
Ejemplo n.º 4
0
 def make_batch(self, iterator):
     return Action.make_batch(iterator,
                              output_length=False,
                              use_cuda=self.use_cuda)
Ejemplo n.º 5
0
 def make_batch(self, iterator):
     return Action.adv_make_batch(iterator,
                                  output_length=True,
                                  use_cuda=self.use_cuda)
Ejemplo n.º 6
0
 def mode(self, network, test):
     Action.mode(network, test)
Ejemplo n.º 7
0
 def make_batch(self, iterator, max_len=None):
     return Action.make_batch(iterator,
                              use_cuda=self.use_cuda,
                              max_len=max_len)
Ejemplo n.º 8
0
 def mode(self, model, test):
     """Train mode or Test mode. This is for PyTorch currently.
     :param model: a PyTorch model
     :param test: bool, whether in test mode.
     """
     Action.mode(model, test)