Solved by Apurv25 in post #5 
              
              
                Hi, 
I found the problem in the above implementation. In the CPP version, I was not clamping the values after doing denormalisation. I have put a clamping function and now it is working as expected. 
The edit part if anyone stumbles on the same problem is below: 
tensor = tensor.mul(0.5).add(0.5).mu…