In this Python tutorial, we will learn about** PyTorch Tensor to NumPy** and we will also cover different examples related to **PyTorch Tensor to NumPy**. And, we will cover these topics.

- PyTorch tensor to numpy
- PyTorch tensor to numpy detach
- PyTorch tensor to numpy float
- PyTorch tensor to numpy CPU
- PyTorch tensor to numpy int
- PyTorch tensor to numpy dtype
- PyTorch tensor to numpy GPU
- PyTorch Cuda tensor to numpy
- Transform PyTorch tensor to numpy

## PyTorch Tensor to NumPy

In this section, we will learn about how to convert **PyTorch tensor to NumPy** in python.

- PyTorch tensor is the same as a numpy array it is just a simply n-dimensional array and used arbitrary numerical computation.
- PyTorch tensor to numpy is defined as a process that occupies on CPU and shares the same memory as the numpy array.

**Code:**

In the following code, we will import some libraries from which we can see the conversion of tensor to NumPy array.

**m = torch.tensor([12.14, 22.58, 32.02, 42.5, 52.6])**is used to creating the one dimensional tensor with float type elements.**print(m)**is used to print the tensor array on the screen.**m = m.numpy()**is the method is used to convert the tensor into a numpy array.**m**this m is used to display the numpy array on the screen.

**# importing torch module**
import torch
**# import numpy module**
import numpy as num
m = torch.tensor([12.14, 22.58, 32.02, 42.5, 52.6])
print(m)
m = m.numpy()
m

**Output:**

After running the above code, we get the following output in which we can see that the tensor array is converting into the NumPy array and the result is printed on the screen.

Also, check: Cross Entropy Loss PyTorch

## PyTorch tensor to numpy detach

In this section, we will learn about how we can convert the **PyTorch tensor to numpy detach** in python.

PyTorch tensor to numpy detach is defined as a process that detaches the tensor from the CPU and after that using **numpy()** for numpy conversion.

**Code:**

In the following code, we will import the torch module from which we can see the conversion of tensor to numpy detach.

**tensorarray = torch.tensor([[15,25,35],[45,55,65],[75,85,95]])**is used to creating the tensor array.**print(tensorarray)**is used to print the tensor array on the screen.**numpyarray= tensorarray.cpu().detach().numpy()**is used for converging the tensor to numpy detach.**numpyarray**is used to display the numpy array on the screen.

```
import torch
tensorarray = torch.tensor([[15,25,35],[45,55,65],[75,85,95]])
print(tensorarray)
numpyarray= tensorarray.cpu().detach().numpy()
numpyarray
```

**Output:**

In the following output, we can see that the conversion of tensor to numpy detach is shown on the screen.

Read: PyTorch Save Model – Complete Guide

## PyTorch tensor to numpy float

In this section, we will learn about how to convert the **PyTorch tensor to NumPy float** in python.

- Before moving forward, we should have a piece of knowledge about float. Float is a data type that includes the fractions represented in the decimal format.
- PyTorch tensor to numpy float is used to convert the tensor array to a numpy float array.

**Code:**

In the following code, we will import the torch module for the conversion of the tensor to NumPy float.

**tensorarray = torch.tensor([[2.,3,4],[5,6,7],[8,9,10]],requires_grad=True)**is used for creating the tensor array.**print(tensorarray)**is used to print the tensor array on the screen.**numpyarray = tensorarray.detach().numpy()**is used to converting the tensor array to numpy float.**numpyarray**is used to display the numpy array on the screen.

```
import torch
tensorarray = torch.tensor([[2.,3,4],[5,6,7],[8,9,10]],requires_grad=True)
print(tensorarray)
numpyarray = tensorarray.detach().numpy()
numpyarray
```

**Output:**

After running the above code, we get the following output in which we can see that the tensor array is converted into a NumPy float array.

Read: PyTorch Batch Normalization

## PyTorch tensor to numpy CPU

In this section, we will learn about how **we can convert the tensor to numpy CPU** in python.

- PyTorch tensor is the same as a numpy array and it is just a simply n-dimensional array and used arbitrary numerical computation.
- PyTorch tensor to numpy CPU is a process of converting tensor to numpy CPU. The tensor is stored on the CPU and shares the same memory.

**Code:**

In the following code, we will import some libraries from which the conversion of the tensor to numpy CPU is done.

**tensorarray = torch.tensor([[25,35,45],[55,65,75],[85,95,15]])**is used for creating the tensor array.**print(tensorarray)**is used to print the tensor array on the screen.**numpyarray= tensorarray.cpu().numpy()**is used for converting the tensor to numpy CPU.**numpyarray**is used to display the numpy array on the screen.

```
import torch
import numpy
tensorarray = torch.tensor([[25,35,45],[55,65,75],[85,95,15]])
print(tensorarray)
numpyarray= tensorarray.cpu().numpy()
numpyarray
```

**Output:**

In the following output, we can see that the conversion of tensor to numpy CPU is printed on the screen.

Read: Keras Vs PyTorch – Key Differences

## PyTorch tensor to numpy int

In this section, we will learn about** PyTorch tensor to numpy int** in python.

**PyTorch tensor to NumPy int** is defined as a process in which we are converting the tensor array to NumPy in the array.

**Code:**

In the following code, we will import some libraries from which we can convert the arrays tensor to numpy.

**m = tf.constant([[3, 4], [5, 6]])**is used for creating the tensor array.**print(m)**is used to print the value of m.**m.numpy()**is used to convert the Pytorch tensor to numpy.

```
import tensorflow as tf
import numpy
m = tf.constant([[3, 4], [5, 6]])
n = tf.add(m, 3)
print(m)
m.numpy()
```

**Output:**

After running the above code we get the following output in which we can see that the PyTorch tensor to numpy inst is printed on the screen.

Read: PyTorch MSELoss – Detailed Guide

## PyTorch tensor to numpy dtype

In this section, we will learn about the **PyTorch tensor to NumPy dtype** in python.

- Before moving forward we should have some piece of knowledge about dtype.
- dtype is a Data type that describes how many bytes a fixed size of the block of memory keeps in touch with an array.Types of data types are integer, float, etc.
- PyTorch tensor to numpy dtype is defined as a process to convert tensor to numpy dtype array.

**Code:**

In the following code, we will import some libraries from which we can convert tensor to numpy dtype.

**tensor = torch.tensor([[5.,10,15],[20,25,30],[35,40,45]])**is used for creating the tensor array.**print(tensor)**is used to print the tensor array on the screen.**numpy = tensor.numpy()**is used to convert the tensor to numpy array.

```
import torch
import numpy
tensor = torch.tensor([[5.,10,15],[20,25,30],[35,40,45]])
print(tensor)
numpy = tensor.numpy()
numpy
```

**Output:**

In the following output, we can see that the PyTorch tensor to numpy dtype is printed on the screen.

Read PyTorch Logistic Regression

## PyTorch tensor to numpy GPU

In this section, we will learn about** PyTorch tensor to NumPy GPU** in python.

A tensor can be generated from the NumPy array and it’s similar to the numpy and its attributes describe the datatypes, shapes, etc.

**Code:**

In the following code, we will import the torch module from which we can convert tensor to numpy GPU array.

**torch.eye(2)**function returns the 2d tensor with size m*n.-
**a.to(“cuda”)**Cuda is an application programming interface that permits the software to use certain types of graphics processing units (GPU). **a.to(“cpu”).numpy()**is used to convert tensor to numpy.

```
import torch
a = torch.eye(2)
a = a.to("cuda")
a.to("cpu").numpy()
```

**Output:**

After running the above code, we get the following output in which we can see that the PyTorch tensor to NumPy GPU array is printed on the screen.

Read PyTorch Binary Cross Entropy

## PyTorch Cuda tensor to NumPy

In this section, we will learn about how to convert **PyTorch Cuda tensor to numpy** in python.

- Before moving forward we should have some piece of knowledge about Cuda.
- Cuda stands for compute unified device architecture which is an application programming interface that permits the software to use certain types of GPU.
- PyTorch Cuda tensor to numpy is defined as a process to convert the Cuda tensor to numpy array.

**Code:**

In the following code, we will import some libraries from which we can create tensor and then convert tensor to NumPy.

**tensor = torch.tensor([2, 4, 6, 8, 10], dtype=torch.float32, requires_grad=True).cuda()**is used to creat tensor on GPU.**print(tensor)**is used to print the tensor array on the screen.**num = tensor.detach().cpu().numpy()**is used to convert tensor to numpy.**num**is used to display the numpy array on the screen.

```
import torch
import numpy
```**
# Create tensor on the GPU**
tensor = torch.tensor([2, 4, 6, 8, 10], dtype=torch.float32, requires_grad=True).cuda()
print(tensor)
num = tensor.detach().cpu().numpy()
num

**Output:**

In the following output, we can see that conversion of PyTorch Cuda tensor to numpy array is printed on the screen.

Read: PyTorch Dataloader + Examples

## Transform PyTorch tensor to numpy

In this section, we will learn about **how to transform PyTorch tensor to numpy** in python.

Transform PyTorch tensor to numpy is defined as a process to convert the PyTorch tensor to numpy array.

**Code:**

In the following code, we will import some libraries from which we can transform PyTorch torch to numpy.

**m = torch.tensor([[2, 4, 6, 8, 10], [3, 6, 9, 12, 15],[4, 8, 12, 16, 20]])**is used to create two dimensional tensor with integer type elements.**print(m)**is used to print the tensor array.**m = m.numpy()**is used to convert tensor to numpy array.**m**is used to display the numpy array on the screen.

```
import torch
import numpy
m = torch.tensor([[2, 4, 6, 8, 10], [3, 6, 9, 12, 15],
[4, 8, 12, 16, 20]])
print(m)
m = m.numpy()
m
```

**Output: **After running the above code we get the following output in which we can see that the transformation of tensor to NumPy array is printed on the screen.

So, in this tutorial, we discussed **PyTorch tensor to numpy** and we have covered different examples related to its implementation. Here is the list of examples that we have covered.

- PyTorch tensor to numpy
- PyTorch tensor to numpy detach
- PyTorch tensor to numpy float
- PyTorch tensor to numpy CPU
- PyTorch tensor to numpy int
- PyTorch tensor to numpy dtype
- PyTorch tensor to numpy GPU
- PyTorch Cuda tensor to numpy
- Transform PyTorch tensor to numpy

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.