# How to calculate ReLU function through ciphertext conversion?

**URL:** <https://openfhe.discourse.group/t/how-to-calculate-relu-function-through-ciphertext-conversion/1658>\
**Category:** Library Questions\
**Tags:** questions, openfhe-help\
**Created:** [October 21, 2024, 4:58am UTC](https://openfhe.discourse.group/t/how-to-calculate-relu-function-through-ciphertext-conversion/1658 "2024-10-21T04:58:22Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![andreea.alexandru](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/andreea.alexandru/32/378_2.png) [@andreea.alexandru](https://openfhe.discourse.group/u/andreea.alexandru)\
**Post date:** [October 21, 2024, 1:37pm UTC](https://openfhe.discourse.group/t/how-to-calculate-relu-function-through-ciphertext-conversion/1658/2 "2024-10-21T13:37:21Z")

</div>

I ran your code and no exception is being thrown on my side.

Nevertheless, there are many misunderstandings in your code, and it does not do what you intend to do.

- GenerateLUTviaFunction only works for p \<= 8. This is written in SCHEME\_SWITCHING\_CAPABILITY.md, which judging by the input x you are feeding, is not sufficient for your application.
- You combine code from FuncViaSchemeSwitching() but replace the line `auto pLWE = ccLWE->GetMaxPlaintextSpace().ConvertToInt(); // Small precision because GenerateLUTviaFunction needs p < q` with pLWE from different functions that work with larger precision. You also make scaleCF 1 instead of 1/pLWE.
- FHEW works with modular arithmetic, so every message is modulo p. This means that your condition in fp (x \<= p) is always true. In modular arithmetic modulo p, positive numbers are less than p/2 and negative numbers are between p/2 and p.
- When you print the expected result relu, you feed 0 instead of p.

To obtain the relu functionality, it is better to use the sign/compare capability using CKKS\<-\>FHEW conversion, which returns a vector of elements encrypting zero for positive values and 1 for negative values (in real arithmetic), and then in CKKS you should multiply c1 by (1 - cTemp2). You can try to adapt the solution from [Can FHE be used for comparing 2 ciphertexts? - #8 by Pavanranganath](https://openfhe.discourse.group/t/can-fhe-be-used-for-comparing-2-ciphertexts/1045/8).

---

_[View the full topic](https://openfhe.discourse.group/t/how-to-calculate-relu-function-through-ciphertext-conversion/1658)._
