ML (Utils)
one_hot_encode()¶
Tests the function.
This cell verifies the one_hot_encode function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.utils import one_hot_encode
def test_one_hot_encode(label):
import numpy as np
res = one_hot_encode(label, 1)
return fhe.array(res) if isinstance(res, list) else res
compiler = fhe.Compiler(test_one_hot_encode, {'label': 'encrypted'})
inputset = [([0, 1, 2],), ([-1, 0, 1],), ([0, 0, 0],)]
circuit = compiler.compile(inputset)
_successes = 0
for inp in inputset:
try:
expected = one_hot_encode(inp[0], 1)
import numpy as np
if isinstance(expected, (list, tuple)) or type(expected).__name__ == 'ndarray':
np.testing.assert_array_equal(circuit.encrypt_run_decrypt(*inp), expected)
else:
assert int(circuit.encrypt_run_decrypt(*inp)) == int(expected), f"Failed at {inp}"
except AssertionError:
raise
except Exception as e:
print(f"Skipping {inp} due to bounds or other error: {e}")
else:
_successes += 1
assert _successes > 0, "one_hot_encode: all inputs were skipped — test is broken"
print(f"one_hot_encode tests passed! ({_successes}/{len(inputset)})")
binarize()¶
Tests the function.
This cell verifies the binarize function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.utils import binarize
def test_binarize(array):
threshold = 2
return binarize(array, threshold)
compiler = fhe.Compiler(test_binarize, {'array': 'encrypted'})
inputset = [([1, 2, 3],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("binarize tests passed! (1/1)")
clip_array()¶
Tests the function.
This cell verifies the clip_array function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.utils import clip_array
def test_clip_array(array):
min_val = 1
max_val = 2
return clip_array(array, min_val, max_val)
compiler = fhe.Compiler(test_clip_array, {'array': 'encrypted'})
inputset = [([1, 2, 3],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("clip_array tests passed! (1/1)")
normalize_array()¶
Tests the function.
This cell verifies the normalize_array function mathematically against its cleartext counterpart, using a dynamically generated input set that covers positive, negative, zero, and array edge cases while respecting the [-7, 7] FHE RAM constraints.
from concrete import fhe
from concrete_fhe_toolkit.ml.utils import normalize_array
def test_normalize_array(array):
divisor = 2
return normalize_array(array, divisor)
compiler = fhe.Compiler(test_normalize_array, {'array': 'encrypted'})
inputset = [([2, 4, 6],)]
circuit = compiler.compile(inputset)
circuit.encrypt_run_decrypt(*inputset[0])
print("normalize_array tests passed! (1/1)")