question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

float16 normalization problem with numpy >= 1.21

See original GitHub issue

Describe the bug

Hi!

I think sklearn.preprocessing.normalize doesn’t work properly on float16 for the latest versions of numpy. I am not sure if this is a bug with numpy or sklearn, but figured folks might want to know. I think for dense matrices, np.einsum is used to compute norms, so the instability might be there. Regardless…

Steps/Code to Reproduce

import numpy as np
import sklearn
print(sklearn.__version__)

## if numpy is 1.20.3 or below, this works
## if numpy is 1.21.X or above, this breaks
print(np.__version__)
import sklearn.preprocessing

np.random.seed(1)
test = np.random.random((1,512)).astype(np.float16)
test_norm = sklearn.preprocessing.normalize(test)
print(np.linalg.norm(test_norm))

#output for sklearn 1.0.1 and numpy 1.21.2 (anything 1.21.+)
#1.0.1
#1.21.2
#13.25

#output for sklearn 1.0.1 and numpy 1.20.3
#1.0.1
#1.20.3
#1.0

Expected Results

regardless of numpy version, np.linalg.norm should be close to 1 after normalization

Actual Results

the norm is 13 for any numpy 1.21.+

Versions

see above — I’m using the latest sklearn, but if i also use the latest numpy, float16 normalization no longer seems to work.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
thomasjpfancommented, Mar 29, 2022

Closing because this issue was fixed in NumPy.

1reaction
glemaitrecommented, Nov 7, 2021

Thanks @ogrisel ! Should I close this issue out because it’s numpy? or keep it open ?

We can keep it open just to track the regression.

Read more comments on GitHub >

github_iconTop Results From Across the Web

NumPy 1.21.0 Release Notes
There are unresolved problems compiling NumPy 1.20.0 with gcc-11.1. ... is strongly discouraged and will now enforce a normalized type tuple if done....
Read more >
NumPy 1.21.5 Release Notes
NumPy 1.21.5 is a maintenance release that fixes a few bugs discovered after the 1.21.4 ... #20462: BUG: Fix float16 einsum fastpaths using...
Read more >
NumPy 1.21.4 Release Notes
Previously, the header files only worked for M1 and this caused problems for folks building x86_64 extensions. This problem was not seen before...
Read more >
NumPy 1.21.2 Release Notes
The 1.21.x series is compatible with Python 3.10.0rc1 and Python 3.10 will be officially supported after it is released. The previous problems with...
Read more >
NumPy 1.21.1 Release Notes
3.17 to deal with problems on arm64. The Python versions supported for this release are 3.7-3.9. The 1.21.x series is compatible with development...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found