'Numpy calculations always differ per function call inside loop for SVD reconstruction

my function that tries to reconstruct an image using SVD with 3 loops sometimes yields different results and tries to calculate the reconstructed image with bad values. Sometimes it works completely fine but if I repeatedly try to reconstruct the same image with the same parameters, it eventually shows bad results. This happens at random and probably not due to variables not being overwritten/deleted correctly after each function call. Sometimes I also get this error message:

/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:21: RuntimeWarning: invalid 
value encountered in add
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:452: RuntimeWarning: overflow 
encountered in double_scalars
  dv = np.float64(self.norm.vmax) - np.float64(self.norm.vmin)
/usr/local/lib/python3.7/dist-packages/matplotlib/image.py:455: RuntimeWarning: invalid 
value encountered in double_scalars
  newmin = vmid - dv * fact
/usr/local/lib/python3.7/dist-packages/matplotlib/colors.py:1026: RuntimeWarning: 
overflow encountered in double_scalars
  resdat /= (vmax - vmin)

Here is my code which reproduces this bug: https://www.kaggle.com/krajnovic/numpy-variance

I debugged it for quite some time and I think the issue happens at the last summation of the "reco" object.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source