Skip to content

multiple_validators

MultipleValidators

Bases: BaseValidator

Wraps multiple validators and returns the sum of their scores.

Source code in pytorch_adapt\validators\multiple_validators.py
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
class MultipleValidators(BaseValidator):
    """
    Wraps multiple validators and returns the sum of their scores.
    """

    def __init__(
        self,
        validators: Union[List["BaseValidator"], Dict[str, "BaseValidator"]],
        weights: Union[List[float], Dict[str, float]] = None,
        return_sub_scores=False,
        **kwargs,
    ):
        """
        Arguments:
            validators: A list of validators or a dictionary mapping from strings to validators.
            weights: A list of floats or a dictionary mapping from validator names to floats.
                If ```None```, then the validators will be equally weighted.
            return_sub_scores: If ```True```, then return the score of each validator,
                in addition to their summed value.
        """
        super().__init__(**kwargs)
        self.validators = c_f.enumerate_to_dict(validators)
        self.weights = c_f.default(weights, {k: 1 for k in self.validators.keys()})
        self.weights = c_f.enumerate_to_dict(self.weights)
        if self.validators.keys() != self.weights.keys():
            raise KeyError("validator keys and weight keys must be the same")
        self.return_sub_scores = return_sub_scores
        pml_cf.add_to_recordable_attributes(self, list_of_names=["weights"])

    def _required_data(self):
        output = [v.required_data for v in self.validators.values()]
        output = list(itertools.chain(*output))
        return list(set(output))

    def compute_score(self):
        pass

    def __call__(self, **kwargs) -> Union[float, Tuple[float, Dict[str, float]]]:
        """
        Returns:
            The sum of the validator scores. If ```self.return_sub_scores``` then
                it also returns a dictionary containing each validator's weighted score.
        """
        kwargs = self.kwargs_check(kwargs)
        outputs = {}
        for k, v in self.validators.items():
            score = v(**c_f.filter_kwargs(kwargs, v.required_data))
            outputs[k] = score * self.weights[k]
        final = sum(outputs.values())
        if self.return_sub_scores:
            return final, outputs
        return final

    def __repr__(self):
        return c_f.nice_repr(self, self.extra_repr(), self.validators)

    def extra_repr(self):
        x = super().extra_repr()
        x += f"\n{c_f.extra_repr(self, ['weights'])}"
        return x

__call__(**kwargs)

Returns:

Type Description
Union[float, Tuple[float, Dict[str, float]]]

The sum of the validator scores. If self.return_sub_scores then it also returns a dictionary containing each validator's weighted score.

Source code in pytorch_adapt\validators\multiple_validators.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def __call__(self, **kwargs) -> Union[float, Tuple[float, Dict[str, float]]]:
    """
    Returns:
        The sum of the validator scores. If ```self.return_sub_scores``` then
            it also returns a dictionary containing each validator's weighted score.
    """
    kwargs = self.kwargs_check(kwargs)
    outputs = {}
    for k, v in self.validators.items():
        score = v(**c_f.filter_kwargs(kwargs, v.required_data))
        outputs[k] = score * self.weights[k]
    final = sum(outputs.values())
    if self.return_sub_scores:
        return final, outputs
    return final

__init__(validators, weights=None, return_sub_scores=False, **kwargs)

Parameters:

Name Type Description Default
validators Union[List['BaseValidator'], Dict[str, 'BaseValidator']]

A list of validators or a dictionary mapping from strings to validators.

required
weights Union[List[float], Dict[str, float]]

A list of floats or a dictionary mapping from validator names to floats. If None, then the validators will be equally weighted.

None
return_sub_scores

If True, then return the score of each validator, in addition to their summed value.

False
Source code in pytorch_adapt\validators\multiple_validators.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def __init__(
    self,
    validators: Union[List["BaseValidator"], Dict[str, "BaseValidator"]],
    weights: Union[List[float], Dict[str, float]] = None,
    return_sub_scores=False,
    **kwargs,
):
    """
    Arguments:
        validators: A list of validators or a dictionary mapping from strings to validators.
        weights: A list of floats or a dictionary mapping from validator names to floats.
            If ```None```, then the validators will be equally weighted.
        return_sub_scores: If ```True```, then return the score of each validator,
            in addition to their summed value.
    """
    super().__init__(**kwargs)
    self.validators = c_f.enumerate_to_dict(validators)
    self.weights = c_f.default(weights, {k: 1 for k in self.validators.keys()})
    self.weights = c_f.enumerate_to_dict(self.weights)
    if self.validators.keys() != self.weights.keys():
        raise KeyError("validator keys and weight keys must be the same")
    self.return_sub_scores = return_sub_scores
    pml_cf.add_to_recordable_attributes(self, list_of_names=["weights"])