I believe you also have to use .detach(). I had to convert my Tensor to a numpy array on Colab which uses CUDA and GPU. I did it like the following:
Copy# this is just my embedding matrix which is a Torch tensor object
embedding = learn.model.u_weight
embedding_list = list(range(0, 64382))
input = torch.cuda.LongTensor(embedding_list)
tensor_array = embedding(input)
# the output of the line below is a numpy array
tensor_array.cpu().detach().numpy()
Answer from azizbro on Stack OverflowI believe you also have to use .detach(). I had to convert my Tensor to a numpy array on Colab which uses CUDA and GPU. I did it like the following:
Copy# this is just my embedding matrix which is a Torch tensor object
embedding = learn.model.u_weight
embedding_list = list(range(0, 64382))
input = torch.cuda.LongTensor(embedding_list)
tensor_array = embedding(input)
# the output of the line below is a numpy array
tensor_array.cpu().detach().numpy()
This worked for me:
Copynp_arr = torch_tensor.detach().cpu().numpy()
Tensors vs Numpy Arrays
RuntimeError: Numpy is not available
Is pytorch faster than numpy on a single CPU?
Torch is being built with the wrong version of NumPy (with pip)
Videos
As I'm trying to learn pytorch, I notice this heavy focus on this tensors as a datatype, but I'm not really clear what it's advantages are over numpy arrays. After all, numpy arrays can be 0,1, 2, and even 3 dimensional, so I'm just unclear on the advantage of tensors, and I wanted to ask you guys, "When and why do we use tensors instead of numpy arrays?"
» pip install fastapi
A while ago I had benchmarked pytorch against numpy for fairly basic matrix operations (broadcast, multiplication, inversion). I didn't run the benchmark for a variety of sizes though. It seemed that pytorch was markedly faster than numpy, possibly it was using more than one core (the hardware had a dozen of cores). Is that a general rule even if constraining pytorch to a single core?
Hello, I need help with the problem I'm trying to solve for a few days now. I have to run a project which uses a bunch of packages, including NumPy 1.22 and PyTorch 1.13. I'm using Windows 10 and Python 3.10.11 with pip 23.0.1. When I install the appropriate versions of the packages and try to run the project, I'm getting error: Failed to initialize NumPy: module compiled against API version 0x10 but this version of numpy is 0xf (Triggered internally at ..\torch\csrc\utils\tensor_numpy.cpp:77.). AFAIK 0xf is 1.22 (the version I have installed) and 0x10 is 1.23/1.24.
What I tried:
-
Reinstalling Python including removing everything Python-related (like files in %APPDATA%) to be sure that no versions of NumPy and PyTorch exist in my system (except for packages bundled in some software that I don't want to uninstall).
-
Checking the Path variable to be sure that the correct version of Python and pip is used.
-
Using venv to have a clear environment.
But still somehow torch seems to be installed with NumPy 1.23/1.24 despite the fact that I have no such version of that package in my system (I searched my entire disk). When I import NumPy and print the version and the path, it correctly shows version 1.22 and the path to the package in venv I created.
I also can't update to the newest version of NumPy (or to 1.23/1.24) because then I get incompatibility with SciPy version. I also can't upgrade the project's requirements, the code is from a paper I'm not the author of so it would be cumbersome.