-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ulab std method for matrix #9
Comments
Can you provide a minimal example that would produce these results? What is the git signature of the version you are running? And which platform? stm32, unix, or something else? By the way, I wanted to clean up the code on std and similar functions, so this is certainly a very good time to bring this issue up. |
Hi , thanks for a great library. I compiled micropython port for ESP32 and ulab followed your instruction. Everything works great and fast except for some hiccups. I'll list all those in a short while. Minimal example: |
I haven't tried the ESP32, so I can't really comment on that. But I believe, errors are universal, so I would really appreciate, if you could post a summary of what you have found. I have to admit, I don't have too much time for testing, so feedback of this kind is really useful.
You can say
Thanks! I will look into this. |
Thanks. Quick summarise of what I found so far:
|
OK, this should be easy to fix.
This issue has been mentioned in the manual (https://micropython-ulab.readthedocs.io/en/latest/ulab.html#binary-operators), and has been discussed here: https://forum.micropython.org/viewtopic.php?f=3&t=7005&start=30#p40469 A fix is underway, but till then, just keep ndarrays on the left hand side.
This will change very soon: I am adding Boolean arrays (this would simplify the code, and would also be numpy-conform). So, I take note of the issue, but I believe, this would have been sorted out very soon, no matter what. |
I have just pushed a fix to testing https://github.com/v923z/micropython-ulab/tree/testing . That should solve the problem of the mean/std/sum functions (I have tested it on the unix port).
returns
Incidentally,
also works. However, I have also modified the output of relational operators, so, e.g.,
which will now return a Boolean ndarray: |
Thanks for lighting fast update. I'm on the testing branch Unfortunately, I test the numerical function std and it still produces
I think it's platform specific bug on the ESP32. I will update you with more debug info for the platform later. Thanks. |
Can you test it on the unix port? |
FWIW, on my unix (macOS) port, it yields the correct result:
|
Thanks! Should we push this issue upstream, then? I haven't a clue what the problem might be, so I think, it could be beneficial to bring in some micropython developer. I am just not sure what the proper way of raising this issue is... |
I guess so. Probably here? |
Before doing that, we should probably isolate the problem. E.g., what is the result of
(this would give us an indication as to whether the error comes from the sqrt function) or
(is the issue related to matrices?) or the python implementation of the std function?
If these all yield the correct results, then we will probably have to whip up a minimal implementation, without ulab, but with the relevant functions, that produces the error. If that comes to that, I would suggest that we create a repository, and work there. By the way, what is the float type on ESP32? You can find that out by checking the output of the .rawsize method https://micropython-ulab.readthedocs.io/en/latest/ulab.html#rawsize . |
First of the float type
The result of
Which is correct, except for some rounding. The result of Which also correct. Now the last one got problem with trying to get sqrt of a negative number due to rounding. I break down the output as below:
The error is because the result of The sqrt function works correctly. I'll try to enable double precission in the build and report back. |
Thanks for the thorough report!
I thought that it was double, but obviously, I was mistaken.
OK, so this proves that the calculation of the standard deviation can't be done in that particular way. One has to use a different formula then. I can make it safe, at the expense of having to iterate the loop twice.
First, I am not sure you can do that, but more importantly, it wouldn't solve the problem. This whole isssue is the result of the finite resolution of floats and doubles. We would run into the same difficulties, but with different numbers. A proper solution is to use a different algorithm for the standad deviation. I will try to implement that tonight. I will ping you, once the fix is in testing. I hope this glitch hasn't discouraged you. |
I assume you're thinking of something like the other classical formula, which avoids substracting large similar numbers?
If so, can you please consider adding the |
Exactly. Actually, it can be done in a single pass, at the expense of an extra multiplication and division. I have to see, whether that is faster than running the loop twice. It might also be a bit platform-dependent.
This is a very good point, thanks! I will try to add this. |
I have just pushed a fix to testing. Could you, please, check that out, and see if it does what you need. |
Hi,
Thanks a lot. |
Btw I got this problem, which works before, maybe because index has been switched to boleen ndarray? I know you still working on slicing and stuff after the change.
My temporary fix would be a-b[0]. This happens when I subtract the mean result, e.g. a-mean(a). |
I bet, this never worked, because I never got to working out the binary operator code in its entirety.
Now, this is a different issue: Just as a side note, I want to re-write the |
Merge upstream
Symptom:
ulab.std() sometimes produce nan in the result
ulab version 0.25
Reproduce:
import ulab as np
a = np.array([[109.5894, 117.2053, 69.3245],
[109.5894, 117.1788, 69.3245],
[109.596, 117.1854, 69.3245],
[109.596, 117.1854, 69.3245],
[109.5894, 117.1722, 69.31788],
[109.5894, 117.1921, 69.31788],
[109.5894, 117.1854, 69.31788],
[109.596, 117.1656, 69.31788],
[109.596, 117.1589, 69.31126],
[109.596, 117.1788, 69.30463],
[109.596, 117.1656, 69.29801],
[109.596, 117.1391, 69.29801],
[109.5894, 117.1656, 69.29801],
[109.5828, 117.1523, 69.29801],
[109.5762, 117.1391, 69.29139],
[109.5695, 117.1457, 69.29139],
[109.5762, 117.1457, 69.29139],
[109.5762, 117.1258, 69.28477],
[109.5695, 117.1192, 69.28477],
[109.5695, 117.0861, 69.28477],
[109.5695, 117.0728, 69.27814],
[109.5695, 117.0728, 69.27814],
[109.5695, 117.0861, 69.28477],
[109.5629, 117.0861, 69.29139],
[109.5695, 117.1059, 69.29139],
[109.5695, 117.1126, 69.28477],
[109.5762, 117.1059, 69.28477],
[109.5828, 117.1258, 69.29139],
[109.5762, 117.1457, 69.29139],
[109.5762, 117.1324, 69.29139],
[109.5762, 117.1192, 69.28477],
[109.5762, 117.1192, 69.28477],
[109.5695, 117.1391, 69.28477],
[109.5695, 117.1059, 69.28477],
[109.5762, 117.1457, 69.28477],
[109.5828, 117.1258, 69.28477],
[109.5828, 117.0993, 69.28477],
[109.5828, 117.1457, 69.28477],
[109.5828, 117.1523, 69.29139],
[109.5894, 117.0993, 69.28477],
[109.5828, 117.1059, 69.27814],
[109.5762, 117.1457, 69.27814],
[109.5695, 117.1126, 69.27814],
[109.5695, 117.1192, 69.27814],
[109.5695, 117.1457, 69.28477],
[109.5695, 117.1324, 69.29139],
[109.5695, 117.1192, 69.28477],
[109.5762, 117.1523, 69.29139],
[109.5695, 117.1722, 69.29801],
[109.5695, 117.1523, 69.29801],
[109.5629, 117.1391, 69.29801],
[109.5695, 117.1722, 69.30463],
[109.5762, 117.1391, 69.31126],
[109.5762, 117.0927, 69.30463],
[109.5695, 117.1391, 69.30463],
[109.5695, 117.0927, 69.30463],
[109.5629, 117.053, 69.30463],
[109.5629, 117.0795, 69.30463],
[109.5563, 117.0728, 69.30463],
[109.5497, 117.0265, 69.29801],
[109.5497, 117.0066, 69.29139],
[109.5497, 117.0199, 69.29139],
[109.5497, 116.9868, 69.29139],
[109.5497, 116.9603, 69.29139],
[109.5497, 116.9801, 69.29801],
[109.5497, 116.9603, 69.29801],
[109.543, 116.9404, 69.29139],
[109.543, 116.9338, 69.28477],
[109.5364, 116.9536, 69.28477],
[109.543, 116.9536, 69.29139],
[109.543, 116.9603, 69.29139],
[109.5364, 116.947, 69.29139],
[109.5298, 116.9669, 69.29139],
[109.5298, 116.9801, 69.29139],
[109.5232, 116.947, 69.29139],
[109.5298, 116.9735, 69.29139],
[109.5232, 117.0066, 69.28477],
[109.5165, 117.0, 69.27814],
[109.5099, 116.9868, 69.27152],
[109.5099, 117.0132, 69.27152],
[109.5099, 117.0199, 69.27152],
[109.5099, 116.9735, 69.27152],
[109.5033, 117.0132, 69.2649],
[109.4967, 117.0662, 69.25828],
[109.4967, 117.0265, 69.25828],
[109.4967, 117.0397, 69.25828],
[109.5033, 117.0728, 69.25166],
[109.4967, 117.053, 69.25166],
[109.4967, 117.0, 69.24503],
[109.4967, 117.0331, 69.23841],
[109.4901, 117.0662, 69.23841],
[109.4967, 117.0464, 69.23841],
[109.4901, 117.0728, 69.23841],
[109.4901, 117.0993, 69.23841],
[109.4901, 117.0662, 69.23841],
[109.4967, 117.0199, 69.23841],
[109.4901, 117.053, 69.23841],
[109.4901, 117.053, 69.23841],
[109.4901, 117.0199, 69.23841],
[109.4834, 117.0596, 69.23841]])
print(np.std(a,axis=0))
Result:
array([nan, nan, nan], dtype=float)
Expected result:
array([0.03424415, 0.07198399, 0.02212126], dtype=float)
The text was updated successfully, but these errors were encountered: