File size: 1,157 Bytes
3257c6c
 
b81628e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3257c6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np

test_cases = [
    {
        "predictions": [0, 0],
        "references": [1, 1],
        "result": {"metric_score": 0}
    },
    {
        "predictions": [1, 1],
        "references": [1, 1],
        "result": {"metric_score": 1}
    },
    {
        "predictions": [1, 0],
        "references": [1, 1],
        "result": {"metric_score": 0.5}
    }
]


def test_ECE():
    N = 10  # N evaluation instances {(x_i,y_i)}_{i=1}^N
    K = 5  # K class problem

    def random_mc_instance(concentration=1, onehot=False):
        reference = np.argmax(
            np.random.dirichlet(([concentration for _ in range(K)])), -1
        )  # class targets
        prediction = np.random.dirichlet(([concentration for _ in range(K)]))  # probabilities
        if onehot:
            reference = np.eye(K)[np.argmax(reference, -1)]
        return reference, prediction

    references, predictions = list(zip(*[random_mc_instance() for i in range(N)]))
    references = np.array(references, dtype=np.int64)
    predictions = np.array(predictions, dtype=np.float32)
    res = ECE()._compute(predictions, references)
    print(f"ECE: {res['ECE']}")