afnio.cognitive.modules.exact_match_evaluator
afnio.cognitive.modules.exact_match_evaluator.ExactMatchEvaluator
Bases: Module
Evaluates predictions using an exact match criterion.
This module leverages the ExactMatchEvaluator
operation from afnio.autodiff.evaluator and is a specialized version of the
DeterministicEvaluator
that uses an exact matching function to compare the prediction and target.
It returns an evaluation score (1 for exact match, 0 otherwise)
and an explanation describing the evaluation result.
Examples:
>>> from afnio import cognitive as cog
>>> from afnio import set_backward_model_client
>>> set_backward_model_client("openai/gpt-4o")
>>> class ExactColor(cog.Module):
... def __init__(self):
... super().__init__()
... self.exact_match = cog.ExactMatchEvaluator()
... def forward(self, prediction, target):
... return self.exact_match(prediction, target)
>>> prediction = afnio.Variable(
... data="green",
... role="color prediction",
... requires_grad=True
... )
>>> target = "red"
>>> eval = ExactColor()
>>> score, explanation = eval(prediction, target)
>>> print(score.data)
0
>>> print(explanation.data)
'The evaluation function, designed for 'exact match', compared the <DATA> fields of the predicted variable and the target variable, resulting in a score: 0.'
>>> explanation.backward()
>>> prediction.grad[0].data
'Reassess the criteria that led to the initial prediction of 'green'.'
Raises:
| Type | Description |
|---|---|
TypeError
|
If the types of |
ValueError
|
If the lengths of |
See Also
afnio.autodiff.evaluator.ExactMatchEvaluator
for the underlying operation.
Source code in afnio/cognitive/modules/exact_match_evaluator.py
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
forward(prediction, target, reduction_fn=sum, reduction_fn_purpose='summation')
Forward pass for the exact match evaluator function.
Warning
Users should not call this method directly. Instead, they should call the
module instance itself, which will internally invoke this forward method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prediction
|
Variable
|
The predicted variable to evaluate, which can have scalar or
list |
required |
target
|
str | list[str] | Variable
|
The target (ground truth) to compare against, which can be a string,
a list of strings, or a |
required |
reduction_fn
|
Callable[[List[Any]], Any] | None
|
An optional function to aggregate scores across a batch of
predictions and targets. If |
sum
|
reduction_fn_purpose
|
str | Variable | None
|
A brief description of the purpose of |
'summation'
|
Returns:
| Name | Type | Description |
|---|---|---|
score |
Variable
|
A variable containing the evaluation score(s),
or their aggregation if |
explanation |
Variable
|
A variable containing the explanation(s) of the evaluation,
or their aggregation if |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the types of |
ValueError
|
If the lengths of |
Source code in afnio/cognitive/modules/exact_match_evaluator.py
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |