# Weirdly high accuracy compared to seal

**URL:** https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025
**Category:** Library Questions
**Created:** [January 17, 2024, 11:03am UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025 "2024-01-17T11:03:07Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![LHenke](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@LHenke](https://openfhe.discourse.group/u/LHenke)
#### Post date: [January 17, 2024, 11:03am UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/1 "2024-01-17T11:03:07Z")

</div>

Hello all,

I am using the CKKS implementation of OpenFHE to do PPML, and I have a working implementation of Tenseal, agains which I am comparing OpenFHE. Now when I am running an inference session over a Pneumonia MNIST model, which does not use Polynomial approximation due to only having Square activation functions, I get an insanely high accuracy from OpenFHE compared to Tenseal, although it should be using the same parameters.

For Tenseal (Seal) I am entering the params:  
poly\_modulus\_degree = 8192  
coeff\_mod\_bit\_sizes = [34, 25, 25, 25, 25, 25, 25, 34]

As far as I understood this translates to: (parameter object implicit)  
SetFirstModSize(34)  
SetScalingModSize(25)  
SetMultiplicativeDepth(6)  
SetRingDim(8192)  
SetScalingTechnique(FLEXIBLEAUTO)

Activated FHE features are:  
PKE  
LEVELDSHE  
ADVANCEDSHE

Now for the inference results. For just one example I get the outputs:  
Tenseal: [-7.008189 5.3304128]  
OpenFHE: [-9.62276159 7.14398429]  
Plain: [-9.666669 7.1789775]

Linear layers are carried out on flattened inputs using matrix multiplication. Am I getting something wrong with translating the parameters here, or what seems to be the issue?

Cheers and thanks in advance

---

<div class="post-metadata">

### Author: ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)
#### Post date: [January 17, 2024, 10:15pm UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/2 "2024-01-17T22:15:12Z")

</div>

`FLEXIBLEAUTO` (with a different scaling factor for each level) has a higher precision than the mode where the same scale is used for all levels (called `FIXEDAUTO` in OpenFHE). My guess is that you are using SEAL in the mode comparable to `FIXEDMANUAL` (in SEAL, the developer has to compute scaling factors on their own - not done by the library).

A simple test you can try: change the scaling techniques from FLEXIBLEAUTO to FIXEDAUTO and see how much worse the accuracy will become.

---

<div class="post-metadata">

### Author: ![LHenke](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@LHenke](https://openfhe.discourse.group/u/LHenke)
#### Post date: [January 19, 2024, 9:23am UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/3 "2024-01-19T09:23:39Z")

</div>

Thank you! So the accuracy is not weirdly high after all!

I still have one question however: Why does it not create the crypto context if I set the security standard to 128, although I get \log(mod) = 184.031?

Cheers and thanks in advance

---

<div class="post-metadata">

### Author: ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)
#### Post date: [January 20, 2024, 12:51am UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/4 "2024-01-20T00:51:04Z")

</div>

For RLWE, the ring dimension for CKKS with hybrid key switching is determined by \log Q + \log P, where Q corresponds to the ciphertext modulus and P is the auxiliary modulus used for hybrid key switching (by default, \log P \approx \frac{1}{3} \log Q. In your case, you need to bump up the ring dimension to 16K or use a larger number of digits for hybrid key switching to make \log P smaller.

A suggestion related to the use of the DIscourse forum: please use different topics for different questions.

---

<div class="post-metadata">

### Author: ![LHenke](https://avatars.discourse-cdn.com/v4/letter/l/c0e974/32.png) [@LHenke](https://openfhe.discourse.group/u/LHenke)
#### Post date: [January 23, 2024, 9:47am UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/5 "2024-01-23T09:47:32Z")

</div>

Suggestion noted, I think now it is too late to start a new thread about this. So it should work if I set the KeySwitchTechnique to BV? What other implication does this have?

---

<div class="post-metadata">

### Author: ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)
#### Post date: [January 23, 2024, 4:09pm UTC](https://openfhe.discourse.group/t/weirdly-high-accuracy-compared-to-seal/1025/6 "2024-01-23T16:09:59Z")

</div>

It’s slower than the hybrid key-switching technique. Check Table 4 [here](https://eprint.iacr.org/2021/204.pdf).
