-1
$\begingroup$

I am translating some Matlab code into Python and I having some problems regarding matrix multiplication accuracy.I've compared some intermediate values in my code and I found that the results of plain matrix multiplication were different.

Here is a minimal reproductive example (including all relevant code and data, a little big ~250M). The following is the python part:

import numpy as np

W = np.load('F.npy')
ksp = np.load('ksp.npy')
WH = W.conj().T

b = WH@ksp
np.save('b.npy',b)   # export python side result

And the MATLAB version:(https://github.com/ShannonZ/npyread for npy load)

F = npyread('F.npy');
ksp = npyread('ksp.npy');
b_py = npyread('b.npy');   
b_mat       = F'*ksp;               

The following figures reveal the element-wise differences between the results from python and MATLAB.

element-wise compare

abs(python-matlab)

The F-norm of those differences were very small, in the order of 1E-9. |b_{py} - b_{mat}|F / (|b{py}|F |b{mat}|_F) ≈ 1E-22. (the accurate value is 1.0180e-22)

But those multiplications were used in a while loop (steepest descending algorithm). It seems that there was an accumative error and finally the result was wrong.

What I've Tried

  1. change parameters in python side to get a more clearer results. Failed! Cannot produce any similar image after changing multiple parameters

Oppositely, in the MATLAB side, I can use some different values of both a and reg. The resultant image after 3000 iterations is smooth and clear.

  1. change the precisian of data in numpy: complex128 => complex64. Failed! No effect.

  2. locate the originate of the errors: Matrix Multiplication accurancy differs

MATLAB F'd - Python WH@d

My Ultimate Objective: I can understand this point Matrix multiplication accuracy Matlab vs Python But IMHO, there should be a way to make the python code usable. Python should have the capability to do what MATLAB can do. Any suggestions would be appreaciated.

P.S. Condition number ≈ 1E20

$\endgroup$
12
  • $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$
    – Community Bot
    Aug 26 at 16:31
  • 5
    $\begingroup$ This doesn't seem to be a minimal example. If the difference is truly attributable to matrix multiplication, then you should be able to exhibit one row and one column whose product (a single number) in Python differs from that in Matlab (by more than typical rounding errors allow). The difference can then be seen without resorting to graphic images. $\endgroup$
    – hardmath
    Aug 27 at 16:12
  • 1
    $\begingroup$ Besides what @hardmath is mentioning, you are using two different colormaps, that already makes comparisons difficult. On top of that, you are using different number of levels in each image (5 and 12?) $\endgroup$
    – nicoguaro
    Aug 29 at 3:24
  • 1
    $\begingroup$ The question has been edited to remove the code that iteratively solves for a regularized inverse solution. Thus the images can't be produced by the code that remains. Are there significant differences between the MATLAB and Python results for a single matrix-vector multiplication? Can you save the results of the matrix vector multiplication in both Python and MATLAB and then compare them? $\endgroup$ Aug 29 at 4:08
  • 1
    $\begingroup$ @OP: could you please report normwise relative errors $\|C_1 - C_2\|_F / (\|A\|_F \|B\|_F)$? As well as the value of $n$ and the data type that is used by both languages (single or double precision). $\endgroup$ Aug 29 at 9:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy