Skip to content

Preprocessing

concrete_fhe_toolkit.ml.utils

Helper utilities for the machine learning module.

one_hot_encode

one_hot_encode(label: Any, num_classes: int) -> Union[np.ndarray, List[Any]]

Convert an encrypted class label into a one-hot encoded boolean array.

Parameters:

Name Type Description Default
label Any

The encrypted class label.

required
num_classes int

The number of classes.

required

Returns:

Type Description
Union[ndarray, List[Any]]

Union[np.ndarray, List[Any]]: A one-hot encoded boolean array.

Example
from concrete_fhe_toolkit.ml.utils import one_hot_encode

# In FHE circuit: one_hot_encode(2, num_classes=4) -> [0, 0, 1, 0]
Source code in src/concrete_fhe_toolkit/ml/utils.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def one_hot_encode(label: Any, num_classes: int) -> Union[np.ndarray, List[Any]]:
    """Convert an encrypted class label into a one-hot encoded boolean array.

    Args:
        label (Any): The encrypted class label.
        num_classes (int): The number of classes.

    Returns:
        Union[np.ndarray, List[Any]]: A one-hot encoded boolean array.

    Example:
        ```python
        from concrete_fhe_toolkit.ml.utils import one_hot_encode

        # In FHE circuit: one_hot_encode(2, num_classes=4) -> [0, 0, 1, 0]
        ```
    """
    classes = np.arange(num_classes)
    return classes == label

binarize

binarize(array: Union[ndarray, List[Any]], threshold: Any) -> Union[np.ndarray, List[Any]]

Binarize an encrypted array based on a threshold.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required
threshold Any

The threshold to compare against.

required

Returns:

Type Description
Union[ndarray, List[Any]]

Union[np.ndarray, List[Any]]: The binarized boolean array.

Example
from concrete_fhe_toolkit.ml.utils import binarize

# In FHE circuit: binarize([1, 5, 10], 5) -> [0, 1, 1]
Source code in src/concrete_fhe_toolkit/ml/utils.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def binarize(array: Union[np.ndarray, List[Any]], threshold: Any) -> Union[np.ndarray, List[Any]]:
    """Binarize an encrypted array based on a threshold.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.
        threshold (Any): The threshold to compare against.

    Returns:
        Union[np.ndarray, List[Any]]: The binarized boolean array.

    Example:
        ```python
        from concrete_fhe_toolkit.ml.utils import binarize

        # In FHE circuit: binarize([1, 5, 10], 5) -> [0, 1, 1]
        ```
    """
    tensor = fhe.array(array)
    return tensor >= threshold  

clip_array

clip_array(array: Union[ndarray, List[Any]], min_val: Any, max_val: Any) -> Union[np.ndarray, List[Any]]

Clip values in an encrypted array to a minimum and maximum range.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required
min_val Any

The minimum value to clip to.

required
max_val Any

The maximum value to clip to.

required

Returns:

Type Description
Union[ndarray, List[Any]]

Union[np.ndarray, List[Any]]: The clipped encrypted array.

Example
from concrete_fhe_toolkit.ml.utils import clip_array

# In FHE circuit: clip_array([-5, 10, 25], 0, 15) -> [0, 10, 15]
Source code in src/concrete_fhe_toolkit/ml/utils.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def clip_array(array: Union[np.ndarray, List[Any]], min_val: Any, max_val: Any) -> Union[np.ndarray, List[Any]]:
    """Clip values in an encrypted array to a minimum and maximum range.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.
        min_val (Any): The minimum value to clip to.
        max_val (Any): The maximum value to clip to.

    Returns:
        Union[np.ndarray, List[Any]]: The clipped encrypted array.

    Example:
        ```python
        from concrete_fhe_toolkit.ml.utils import clip_array

        # In FHE circuit: clip_array([-5, 10, 25], 0, 15) -> [0, 10, 15]
        ```
    """
    tensor = fhe.array(array)
    return np.clip(tensor, min_val, max_val)

normalize_array

normalize_array(array: Union[ndarray, List[Any]], divisor: int) -> Union[np.ndarray, List[Any]]

Normalize an encrypted array by floor dividing by a constant scalar divisor.

Parameters:

Name Type Description Default
array Union[ndarray, List[Any]]

The encrypted array or list of elements.

required
divisor int

The integer value to divide elements by.

required

Returns:

Type Description
Union[ndarray, List[Any]]

Union[np.ndarray, List[Any]]: The normalized encrypted array.

Example
from concrete_fhe_toolkit.ml.utils import normalize_array

# In FHE circuit: normalize_array([10, 25, 50], 10) -> [1, 2, 5]
Source code in src/concrete_fhe_toolkit/ml/utils.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def normalize_array(array: Union[np.ndarray, List[Any]], divisor: int) -> Union[np.ndarray, List[Any]]:
    """Normalize an encrypted array by floor dividing by a constant scalar divisor.

    Args:
        array (Union[np.ndarray, List[Any]]): The encrypted array or list of elements.
        divisor (int): The integer value to divide elements by.

    Returns:
        Union[np.ndarray, List[Any]]: The normalized encrypted array.

    Example:
        ```python
        from concrete_fhe_toolkit.ml.utils import normalize_array

        # In FHE circuit: normalize_array([10, 25, 50], 10) -> [1, 2, 5]
        ```
    """
    tensor = fhe.array(array)
    return tensor // divisor