How to solve the pytorch RuntimeError: Numpy is not available without upgrading numpy to the latest version because of other dependencies
Asked Answered
D

7

18

I am running a simple CNN using Pytorch for some audio classification on my Raspberry Pi 4 on Python 3.9.2 (64-bit). For the audio manipulation needed I am using librosa. librosa depends on the numba package which is only compatible with numpy version <= 1.20.

When running my code, the line

spect_tensor = torch.from_numpy(spect).double()

throws the RuntimeError:

RuntimeError: Numpy is not available

Searching the internet for solutions I found upgrading Numpy to the latest version to resolve that specific error, but throwing another error, because Numba only works with Numpy <= 1.20.

Is there a solution to this problem which does not include searching for an alternative to using librosa?

Dental answered 31/3, 2022 at 8:11 Comment(2)
numpy upgraded its c API between 1.19 and 1.20 in a mildly non backwards compatible way and it's taken a while to get everyone on the same page. New numba versions support numpy >1.20 but I'm not sure if librosa has gotten with the program yet. If not you might need to keep everything in an older versionCubby
I did set up my raspberry pi completely fresh and installed every package with the same version as they are on my laptop. While my code runs without any problems on my laptop it still throws the same exact error as before when running on my raspberry pi. How is this possible?Dental
D
6

Just wanted to give an update on my situation. I downgraded torch to version 0.9.1 which solved the original issue. Now OpenBLAS is throwing a warning because of an open MPLoop. But for now my code is up and running.

Dental answered 5/4, 2022 at 11:17 Comment(2)
Came across the same error with Pytorch 1.12.1. I downgraded to version 1.11.0 since older version don't seem to exist anymore and it workedDelimitate
You may want to look at setting up a dedicated Python environment for this project (with either virtual environments or something like conda / mamba). Not only will this allow you to install specific versions of packages and their (version specific) dependencies, but also to freeze those packages at those versions (preventing upgrades) and even package the environment for migration or redistribution to other machines with (more) guaranteed reproducibility. -- allowing you to maintain up to date packages in your main / system installation!Nickels
T
25

This will be easily solved by upgrading numpy.... When I face this error, that time numpy version 1.22 was installed.... I update version to 1.24.1 using this command

pip install numpy==1.24.1

Error Resolved

Thread answered 5/1, 2023 at 8:5 Comment(2)
This breaks numba for me, which is a dependency of a dependency rather than something I directly use :P github.com/numba/numba/issues/8615Drusi
Even though I am running torch and torch audio 2.2.2, I had to pin numpy to 1.24.1 to get whisper to work properly as a model from huggingface otherwise I was getting the numpy not found error. Very weird.Maddux
C
7

For some who may be facing this issue in a brand new python environment, you may just have to restart Jupyter Notebook. I received this error simply because I had started up notebook, and then installed numpy in my python environment after realizing it was not previously installed.

If you've done this, just kill the jupyter session and restart. It will pick up the new numpy install.

Corvus answered 13/8, 2023 at 13:16 Comment(0)
D
6

Just wanted to give an update on my situation. I downgraded torch to version 0.9.1 which solved the original issue. Now OpenBLAS is throwing a warning because of an open MPLoop. But for now my code is up and running.

Dental answered 5/4, 2022 at 11:17 Comment(2)
Came across the same error with Pytorch 1.12.1. I downgraded to version 1.11.0 since older version don't seem to exist anymore and it workedDelimitate
You may want to look at setting up a dedicated Python environment for this project (with either virtual environments or something like conda / mamba). Not only will this allow you to install specific versions of packages and their (version specific) dependencies, but also to freeze those packages at those versions (preventing upgrades) and even package the environment for migration or redistribution to other machines with (more) guaranteed reproducibility. -- allowing you to maintain up to date packages in your main / system installation!Nickels
W
1

Issue : Faced same error while writing code for LLMs.(NumPy is not available)

Fix : Restart the Kernel. (Thanks to NPE_Exception for clue, credit goes to this member)

Details : Cause of issue is due to installing packages as found missing while writing code in note book . Looks like should not be done that way. It needs kernel restart to get reference.

Withdrew answered 19/11, 2023 at 15:30 Comment(0)
S
0

Instead of using spect_tensor = torch.from_numpy(spect).double() use this spect_tensor = torch.tensor(spect).double()

Simms answered 2/6, 2023 at 2:40 Comment(1)
Can you provide more details?Bocage
D
0

Getting same error for numpy 2.0 as of August 17th 2024.

Current solution is :

pip install "numpy<2"
Dedicated answered 17/8, 2024 at 10:39 Comment(0)
S
-2

I faced the same issue. Simply restarting the kernel should help.

Snowinsummer answered 4/4, 2024 at 10:11 Comment(1)
This answer does not address the issue the OP has regarding version incompatibility between different libraries. -- If you are certain it is an appropriate solution to the question posed then please expand you answer to give clear and detailed information on how to implement your solution and how it resolves the problem as described.Nickels

© 2022 - 2025 — McMap. All rights reserved.