File size: 2,704 Bytes
256a159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import re
from typing import List

from datasets import load_dataset

from opencompass.openicl.icl_evaluator import BaseEvaluator
from opencompass.registry import LOAD_DATASET

from .base import BaseDataset


@LOAD_DATASET.register_module()
class crowspairsDataset(BaseDataset):

    @staticmethod
    def load(**kwargs):

        dataset = load_dataset(**kwargs)

        def preprocess(example):
            example['label'] = 0
            return example

        return dataset.map(preprocess)


@LOAD_DATASET.register_module()
class crowspairsDataset_V2(BaseDataset):

    @staticmethod
    def load(**kwargs):
        dataset = load_dataset(**kwargs)

        def preprocess(example):
            example['label'] = 'A'
            return example

        return dataset.map(preprocess)


def crowspairs_postprocess(text: str) -> str:
    """Cannot cover all the cases, try to be as accurate as possible."""
    if re.search('Neither', text) or re.search('Both', text):
        return 'invalid'

    if text != '':
        first_option = text[0]
        if first_option.isupper() and first_option in 'AB':
            return first_option

        if re.search(' A ', text) or re.search('A.', text):
            return 'A'

        if re.search(' B ', text) or re.search('B.', text):
            return 'B'

    return 'invalid'


class CrowspairsEvaluator(BaseEvaluator):
    """Calculate accuracy and valid accuracy according the prediction for
    crows-pairs dataset."""

    def __init__(self) -> None:
        super().__init__()

    def score(self, predictions: List, references: List) -> dict:
        """Calculate scores and accuracy.

        Args:
            predictions (List): List of probabilities for each class of each
                sample.
            references (List): List of target labels for each sample.

        Returns:
            dict: calculated scores.
        """
        if len(predictions) != len(references):
            return {
                'error': 'predictions and references have different length.'
            }
        all_match = 0
        for i, j in zip(predictions, references):
            all_match += i == j

        valid_match = 0
        valid_length = 0
        for i, j in zip(predictions, references):
            if i != 'invalid':
                valid_length += 1
                valid_match += i == j

        accuracy = round(all_match / len(predictions), 4) * 100
        valid_accuracy = round(valid_match / valid_length, 4) * 100
        valid_frac = round(valid_length / len(predictions), 4) * 100
        return dict(accuracy=accuracy,
                    valid_accuracy=valid_accuracy,
                    valid_frac=valid_frac)