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 Overflow
Discussions

Tensors vs Numpy Arrays
reach disarm sophisticated strong six coherent cooperative important reminiscent telephone This post was mass deleted and anonymized with Redact More on reddit.com
🌐 r/learnmachinelearning
27
56
April 21, 2023
RuntimeError: Numpy is not available
Issue description I was running some other code and I faced the error "RuntimeError: Numpy is not available". I searched around and found out that running the code I have attached below p... More on github.com
🌐 github.com
14
May 26, 2022
Is pytorch faster than numpy on a single CPU?
If you really enforce a single thread, then it's all up to the BLAS library which does the heavy lifting. Both numpy and pytorch are compatible with the most common libs, so it's a matter of which lib they are linked against respectively. Numpy with MKL will most likely beat pytorch with OpenBLAS for most workloads. More on reddit.com
🌐 r/Python
29
64
July 12, 2024
Torch is being built with the wrong version of NumPy (with pip)
Are you using virtual environments? If not I'd strongly suggest you look at using either Miniconda (a package managing tool for python) or the slightly more manual approach of using venv . That way you can more reliably try different versions of your dependencies to debug this out. Once you do, come back and let us know if you are still seeing these issues and we can dig deeper into the issue. But for now, the potential for cross dependency conflict is too high and could make trouble shooting a real nightmare. More on reddit.com
🌐 r/learnpython
7
9
April 13, 2025
🌐
Kaggle
kaggle.com › code › amirmotefaker › pytorch-vs-numpy
PyTorch vs NumPy
July 14, 2024 - NOTEPytorch vs NumpyNumPyPyTorchPyTorch: Tensors and autogradPyTorch: Defining new autograd functionsPyTorch: nn modulePyTorch: optimalPyTorch: Custom nn ModulesPyTorch: Control Flow + Weight Sharing
Find elsewhere
🌐
PyTorch
docs.pytorch.org › intro › learning pytorch with examples › warm-up: numpy
Warm-up: numpy — PyTorch Tutorials 2.11.0+cu130 documentation
July 20, 2022 - import numpy as np import math # Create random input and output data x = np.linspace(-math.pi, math.pi, 2000) y = np.sin(x) # Randomly initialize weights a = np.random.randn() b = np.random.randn() c = np.random.randn() d = np.random.randn() learning_rate = 1e-6 for t in range(2000): # Forward pass: compute predicted y # y = a + b x + c x^2 + d x^3 y_pred = a + b * x + c * x ** 2 + d * x ** 3 # Compute and print loss loss = np.square(y_pred - y).sum() if t % 100 == 99: print(t, loss) # Backprop to compute gradients of a, b, c, d with respect to loss grad_y_pred = 2.0 * (y_pred - y) grad_a = gr
🌐
GitHub
github.com › pytorch › pytorch › issues › 78341
RuntimeError: Numpy is not available · Issue #78341 · pytorch/pytorch
May 26, 2022 - import numpy import torch test_torch = torch.rand(100,1,28,28) print(test_torch.numpy()) PyTorch or Caffe2: Pytorch · How you installed PyTorch (conda, pip, source): pip3 install torch torchvision torchaudio · OS: Windows 11 · PyTorch version: 1.11.0+cpu ·
Author   pytorch
🌐
Quora
quora.com › What-are-the-benefits-of-PyTorch-over-NumPy
What are the benefits of PyTorch over NumPy? - Quora
Answer: PyTorch is a deep learning focused library while NumPy is for scientific computing. Both of these libraries are made with different goals in mind: * If you want to just do basic matrix operations/transformation and array operations then ...
🌐
PyTorch
pytorch.org › blog › compiling-numpy-code
Compiling NumPy code into C++ or CUDA via torch.compile – PyTorch
October 17, 2023 - Quansight engineers have implemented support for tracing through NumPy code via torch.compile in PyTorch 2.1. This feature leverages PyTorch’s compiler to generate efficient fused vectorized code without having to modify your original NumPy code.
🌐
PyPI
pypi.org › project › fastapi
fastapi · PyPI
FastAPI framework, high performance, easy to learn, fast to code, ready for production
      » pip install fastapi
    
Published   May 23, 2026
Version   0.136.3
🌐
Saturn Cloud
saturncloud.io › blog › converting-from-numpy-array-to-pytorch-tensor-a-comprehensive-guide
Converting from Numpy Array to PyTorch Tensor: A Guide | Saturn Cloud Blog
January 20, 2024 - It’s a staple in the data science community for its efficiency and ease of use. PyTorch, on the other hand, is an open-source machine learning library based on the Torch library.
🌐
Medium
medium.com › @yunjiangster › comparing-speed-of-torch-and-numpy-f22b11aabfcf
Comparing Speed of Torch and Numpy | by Yunjiang Jiang | Medium
June 19, 2023 - Huggingface converts the input to the compute_metrics function from torch tensors to numpy arrays already, since it wasn’t expecting people to do heavy computation within the compute_metrics function.
🌐
Reddit
reddit.com › r/learnpython › torch is being built with the wrong version of numpy (with pip)
r/learnpython on Reddit: Torch is being built with the wrong version of NumPy (with pip)
April 13, 2025 -

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:

  1. 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).

  2. Checking the Path variable to be sure that the correct version of Python and pip is used.

  3. 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.

🌐
PyTorch Forums
discuss.pytorch.org › nlp
Converting tensors into numpy array and vise versa - nlp - PyTorch Forums
January 28, 2022 - i’m working on distilbert for text classification, and want to save the tensors into a data frame so i can use it again as feature generator to illustrate my question more: below are token IDs for one sentence in a row in csv file i’m working on : Token IDs: tensor([ 101, 11113, 2080, 25876, ...
🌐
NumPy
numpy.org › doc › stable › user › basics.interoperability.html
Interoperability with NumPy — NumPy v2.4 Manual
See the Dask array documentation and the scope of Dask arrays interoperability with NumPy arrays for details. Several Python data science libraries implement the __dlpack__ protocol. Among them are PyTorch and CuPy. A full list of libraries that implement this protocol can be found on this page of DLPack documentation. ... >>> import torch >>> x_torch = torch.arange(5) >>> x_torch tensor([0, 1, 2, 3, 4]) >>> x_np = np.from_dlpack(x_torch) >>> x_np array([0, 1, 2, 3, 4]) >>> # note that x_np is a view of x_torch >>> x_torch[1] = 100 >>> x_torch tensor([ 0, 100, 2, 3, 4]) >>> x_np array([ 0, 100, 2, 3, 4])
🌐
DataCamp
datacamp.com › doc › numpy › pytorch-tensors
NumPy to PyTorch Tensors
Here, we convert a PyTorch tensor that tracks gradients to a NumPy array, using `detach()` to ensure no gradient tracking. Avoid sharing memory unintentionally. Remember that `torch.from_numpy()` shares memory with the NumPy array; changes to one affect the other.
🌐
GitHub
github.com › pytorch › pytorch
GitHub - pytorch/pytorch: Tensors and Dynamic neural networks in Python with strong GPU acceleration · GitHub
April 23, 2026 - You can write new neural network layers in Python using the torch API or your favorite NumPy-based libraries such as SciPy.
Starred by 101K users
Forked by 28K users
Languages   Python 63.7% | C++ 28.8% | Cuda 2.7% | C 1.3% | Objective-C++ 1.0% | CMake 0.6%