跳到内容

Utils

assert_matrix_correlation_above_value(actual, expected, mask=None, min_correlation=0.95, msg='')

断言两个张量接近,其均方根误差 (RMSE) 相对于每个矩阵的缩放均方根值。 这会告诉您 RMSE 是否意味着这两个矩阵彼此之间的相似程度更高,而不是值被随机置换的情况。

参数

名称 类型 描述 默认值
actual Tensor

实际张量。

必需
expected Tensor

预期张量。

必需
mask Optional[Tensor]

如果只想比较某些值,请应用此掩码,RMSE 将仅在未掩码的项目上计算。

None
min_relative_rmse

相对容差参数。

必需
源代码位于 bionemo/testing/utils.py
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
def assert_matrix_correlation_above_value(  # noqa: D417
    actual: torch.Tensor,
    expected: torch.Tensor,
    mask: Optional[torch.Tensor] = None,
    min_correlation: float = 0.95,
    msg: str = "",
) -> None:
    """Assert that two tensors are close with a root mean squared error (RMSE)
        relative to the scaled root mean square values for each matrix. This tells
        you if the RMSE implies that the two matrices are more similar to eachother
        as-is than would be the case if values were randomly permuted.

    Args:
        actual: The actual tensor.
        expected: The expected tensor.
        mask: If there are only some values you want to compare,
            apply this mask and RMSE will be computed on the unmasked items only.
        min_relative_rmse: The relative tolerance parameter.
    """  # noqa: D205
    if mask is None:
        mask = torch.ones_like(actual)
    else:
        if len(mask.shape) < len(actual.shape):
            mask = mask[..., None]
    masked_actual = actual[mask.expand_as(actual).to(bool)]
    masked_expected = expected[mask.expand_as(expected).to(bool)]
    corr = torch.corrcoef(torch.stack([masked_actual, masked_expected]))[0, 1]
    if corr < min_correlation:
        raise AssertionError(f"Correlation below threshold: {corr} < {min_correlation}. {msg}")

assert_matrix_mape_below_value(actual, expected, mask=None, max_mape=0.1, eps=0.001, msg='')

断言两个张量接近,其均方根误差 (RMSE) 相对于每个矩阵的缩放均方根值。 这会告诉您 RMSE 是否意味着这两个矩阵彼此之间的相似程度更高,而不是值被随机置换的情况。

参数

名称 类型 描述 默认值
actual Tensor

实际张量。

必需
expected Tensor

预期张量。

必需
mask Optional[Tensor]

如果只想比较某些值,请应用此掩码,RMSE 将仅在未掩码的项目上计算。

None
min_relative_rmse

相对容差参数。

必需
源代码位于 bionemo/testing/utils.py
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
def assert_matrix_mape_below_value(  # noqa: D417
    actual: torch.Tensor,
    expected: torch.Tensor,
    mask: Optional[torch.Tensor] = None,
    max_mape: float = 0.1,
    eps: float = 1e-3,
    msg: str = "",
) -> None:
    """Assert that two tensors are close with a root mean squared error (RMSE)
        relative to the scaled root mean square values for each matrix. This tells
        you if the RMSE implies that the two matrices are more similar to eachother
        as-is than would be the case if values were randomly permuted.

    Args:
        actual: The actual tensor.
        expected: The expected tensor.
        mask: If there are only some values you want to compare,
            apply this mask and RMSE will be computed on the unmasked items only.
        min_relative_rmse: The relative tolerance parameter.
    """  # noqa: D205
    if mask is None:
        mask = torch.ones_like(actual)
    else:
        if len(mask.shape) < len(actual.shape):
            mask = mask[..., None]
    masked_actual = actual[mask.expand_as(actual).to(bool)]
    masked_expected = expected[mask.expand_as(expected).to(bool)]
    mape = (
        torch.mean(
            torch.abs(masked_actual - masked_expected)
            / torch.maximum(torch.abs(masked_expected), torch.zeros_like(masked_expected) + eps)
        )
        * 100.0
    )
    if mape > max_mape:
        raise AssertionError(f"MAPE below threshold: {mape} > {max_mape}. {msg}")