PyTorch Tensor to NumPy: Convert A PyTorch Tensor To A Numpy Multidimensional Array

PyTorch Tensor to NumPy - Convert a PyTorch tensor to a NumPy multidimensional array so that it retains the specific data type

Type: FREE   By: Sebastian Gutierrez, AIWorkbox.com Instructor Sebastian Gutierrez   Duration: 3:57   Technologies: NumPy, PyTorch, Python

Page Sections: Video  |  Code  |  Transcript


< > Code:

You must be a Member to view code

Access all courses and lessons, gain confidence and expertise, and learn how things work and how to use them.

    or   Log In


Transcript:

We import NumPy as np.

import numpy as np


And we print the version that we are using of NumPy.

print(np.__version__)

We’re using 1.13.3.


Next, we import PyTorch.

import torch


Then we print the PyTorch version that we are using.

print(torch.__version__)

We are using PyTorch version 0.2.0_4.


For the first example, we’re going to create a PyTorch example integer tensor.

pt_ex_int_tensor = (torch.rand(2, 3, 4) * 100).int()

We’re going to use the PyTorch rand functionality to create a tensor that is 2x3x4.

We’re going to multiply it by 100 and then we’re going to cast it to an integer tensor.


Now we print our example PyTorch example integer tensor and we see that it is size 2x3x4 and it is an IntTensor and we see the numbers that were generated.

print(pt_ex_int_tensor)


To convert this PyTorch tensor to a NumPy multidimensional array, we’re going to use the .numpy() PyTorch functionality.

np_ex_int_mda = pt_ex_int_tensor.numpy()

We’re going to convert our PyTorch example IntTensor to NumPy using that functionality and we’re going to assign it to the Python variable np_ex_int_mda for NumPy example integer multidimensional array.


With this NumPy example integer multidimensional array, we can check the shape to see what came out

np_ex_int_mda.shape

And we see that it is 2x3x4 which is the same thing as our IntTensor.


We can check the data type.

np_ex_int_mda.dtype

We see that it is int32 and that is the same data type that we have for our PyTorch tensor.

We can see here IntTensor and that’s what we cast our random PyTorch tensor up here to.


Lastly, we can print our NumPy multidimensional array

print(np_ex_int_mda)

And we see that it is the same thing – 10, 49, 91, 80; 10, 49, 91, 80; 82, 66, 41, 45; 82, 66, 41, 45.

So that was how we converted a PyTorch tensor that had integers to a NumPy multidimensional array that had integers.


Next, we’re going to do the same exercise but this time, we’re going to do it with a Float32 tensor.


The first thing we do is we define a Python variable pt(for PyTorch)_ex_float_tensor.

pt_ex_float_tensor = torch.rand(2, 3, 4) * 100

We use the PyTorch random functionality to generate a PyTorch tensor that is 2x3x4 and multiply it by 100.


Next, we print our PyTorch example floating tensor and we see that it is in fact a FloatTensor of size 2x3x4.

print(pt_ex_float_tensor)


To convert the PyTorch tensor to a NumPy multidimensional array, we use the .numpy() PyTorch functionality on our existing tensor and we assign that value to np_ex_float_mda.

np_ex_float_mda = pt_ex_float_tensor.numpy()


We can look at the shape

np_ex_float_mda.shape

And we see that it is 2x3x4 which is what we would expect.


We can look at the data type

np_ex_float_mda.dtype

And see that it is Float32.

When we use the PyTorch random tensor functionality, by default it creates a Float32 tensor.

We see that NumPy has the exact same data type.


Lastly, we can print the values to see that we have the same information.

print(np_ex_float_mda)

Looking at this matrix, we see 23.3159.

Here, we see 23.31588.

NumPy is just showing a few more digits.

We see 23.4223, 23.4223; 17.8295; so on and so forth.


So to convert a PyTorch floating or IntTensor or any other data type to a NumPy multidimensional array, we use the .numpy() functionality to change the PyTorch tensor to a NumPy multidimensional array.



Back to deep learning tutorial lesson list