TTFunctionEvaluator#

class TTFunctionEvaluator#

This class is used to evaluate different available functions on CTileTensor objects, such as polynomial evaluation and sigmoid.

compare(self: pyhelayers.TTFunctionEvaluator, a: pyhelayers.CTileTensor, b: pyhelayers.TileTensor, g_rep: int, f_rep: int, max_diff: float) pyhelayers.CTileTensor#

The same as FunctionEvaluator::compare, but works on tile tensors elementwise. The first argument must be a CTileTensor and the second a TileTensor (encrypted or plain). The usual broadcasting rules of binary operations apply.

For more details on the comparison operator, see documentation of FunctionEvaluator::compare

Parameters:
  • a (CTileTensor) – First CTileTensor to compare.

  • b (TileTensor) – Second TileTensor to compare.

  • g_rep (int) – Controls the accuracy of the result. A higher g_rep value increases accuracy on the account of slower and deeper computation.

  • f_rep (int) – Controls the accuracy of the result. A higher f_rep value increases accuracy on the account of slower and deeper computation.

  • max_diff – An upper bound on |a-b|.

Ptype max_diff:

float

Raises:

ValueError – If “a” and “b” don’t have the same shape.

inverse(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, lower_bound: float, upper_bound: float, bit_resolution: int = 5) pyhelayers.CTileTensor#

The same as FunctionEvaluator::inverse, but works on a CTileTensor object as source, CTile-wise. For more details, see documentation of FunctionEvaluator::inverse.

Parameters:
  • src (CTileTensor) – CTileTensor to calculate its inverse.

  • lower_bound (double) – a lower bound on the tiles of src. The tighter this bound is, the more accurate the result will be. This lower bound must be non-negative.

  • upper_bound (double) – an upper upper bound on the tiles src. The tighter this bound is, the more accurate the result will be

  • bit_resolution (int) – Controls the accuracy of the result. A higher bit_resolution value will increase the accuracy of the result at the account of consumig more multiplication depth. Defaults to 5.

Return type:

CTileTensor

poly_eval_in_place(*args, **kwargs)#

Overloaded function.

  1. poly_eval_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, coefs: numpy.ndarray[numpy.float64], type: pyhelayers.EvalType = <EvalType.MIN_DEPTH: 2>) -> None

    Polynomial evaluation, in place. Evaluates the given plain polynomial on input “src”, and stores the result in “src”.

    param src:

    The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.

    type src:

    CTileTensor

    param coefs:

    The coefficients of the polynomial. coefs[0] is the free coefficient.

    type coefs:

    numpy array of floats

    param type:

    The type of the evaluation algorithm. See also the documnetation of “EvalType”. Defaults to MIN_DEPTH.

    type type:

    EvalType

  2. poly_eval_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, coefs: pyhelayers.CTileVector, normalized: bool = False) -> None

    Polynomial evaluation, in place. Receives a polynomial with encrypted coefficients, evaluates it on the input “src” and stores the result in “src”.

    param src:

    The input of the polynomial. This will contain the result of the evaluation at the end of the function execution.

    type src:

    CTileTensor

    param coefs:

    The (encrypted) coefficients of the polynomial. coefs[0] is the free coefficient.

    type coefs:

    numpy array of floats

    param normalized:

    If False, the polynomial to evaluate will be composed from the coefficients in “coeffs” vector only. Otherwise, an extra term, whose coefficient is 1 and whose power is len(coefs), will be added. Defaults to False.

    type normalized:

    bool

pow_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, degree: int) None#

Computes src^degree, in place.

Parameters:
  • src (CTileTensor) – A CTileTensor to calculate its power.

  • degree (int) – The required exponent.

sigmoid3_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None#

An approximation of sigmoid by an order 3 polynomial, in place.

Parameters:

src (CTileTensor) – A CTileTensor to calculate its sigmoid.

sigmoid7_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None#

An approximation of sigmoid by an order 7 polynomial, in place.

Parameters:

src (CTileTensor) – A CTileTensor to calculate its sigmoid.

sigmoid9_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor) None#

An approximation of sigmoid by an order 9 polynomial, in place.

Parameters:

src (CTileTensor) – A CTileTensor to calculate its sigmoid.

sign_in_place(self: pyhelayers.TTFunctionEvaluator, src: pyhelayers.CTileTensor, gRep: int, fRep: int, max_abs_val: float = 1, binary_res: bool = False) None#

Computes the (approximated) sign of src, in place. The sign is computed as g(g(…g(f(f(…f(x)))))), where g(x) and f(x) are two degree 7 polynomials. The number of times g(x) and f(x) appear in the composition can be controlled by gRep and fRep arguments, respectively. all values in src must be in the range [-max_abs_val, max_abs_val].

Parameters:
  • src (CTileTensor) – A CTileTensor to calculate its sign. All of its values must be in the range [-1, 1].

  • gRep (int) – How many repetitions of g(x).

  • fRep (int) – How many repetitions of f(x).

  • max_abs_val (float) – An upper bound on the absolute value of src. Defaults to 1.

  • binary_res (bool) – If True, the result will be close to 0 when src < 0 and close to 1 when src > 0. If False, the result will be close to -1 when src < 0 and close to 1 when src > 0. Defaults to False.