# PyTorch Variable To NumPy: Convert PyTorch autograd Variable To NumPy Multidimensional Array

PyTorch Variable To NumPy - Transform a PyTorch autograd Variable to a NumPy Multidimensional Array by extracting the PyTorch Tensor from the Variable and converting the Tensor to the NumPy array

PyTorch Variable To NumPy - Transform a PyTorch autograd Variable to a NumPy Multidimensional Array by extracting the PyTorch Tensor from the Variable and converting the Tensor to the NumPy array

## Video Transcript

First, we import NumPy as np:

``````import numpy as np
``````

And then we print the NumPy version that we are using:

``````print(np.__version__)
``````

We are using 1.13.3.

Next, we import PyTorch:

``````import torch
``````

And we print the PyTorch version that we are using:

``````print(torch.__version__)
``````

We’re using PyTorch version 0.2.0_4.

We import the PyTorch variable from the torch.autograd package.

``````from torch.autograd import Variable
``````

For this video, we’re going to work with a Python variable named random_variable_ex that creates a PyTorch variable which is of size 2x3x4 that’s going to be filled with random float32 numbers that are multiplied by 100 and then cast to integers.

``````random_variable_ex = Variable((torch.rand(2, 3, 4) * 100).int(), requires_grad=True)
``````

We can check the type of the random_variable_ex:

``````type(random_variable_ex)
``````

And we see that it is a PyTorch autograd variable.

Now that we have this variable, we want to convert it to a NumPy multidimensional array.

Since we know that we can convert a PyTorch tensor to a NumPy multidimensional array using the numpy() functionality, we can try that:

``````random_variable_ex.numpy()
``````

So we pass in our random_variable_ex and then chain .numpy().

Unfortunately, this causes an error.

The error is AttributeError: 'Variable' object has no attribute 'numpy'.

That’s strange because when we print the variable:

``````print(random_variable_ex)
``````

we can see that it’s a PyTorch IntTensor of size 2x3x4.

The trick here is that PyTorch Variables can’t be transformed to NumPy because PyTorch Variables are wrappers around PyTorch Tensors that save history and operation on the Variable.

They do not, however, have objects that can be converted to NumPy.

Again, this is a “Variable containing” so the PyTorch Variable contains this Torch Tensor that’s inside of it.

It’s not the actual thing so we can’t use it.

So what we have to do is we have to extract the PyTorch Tensor from the PyTorch Variable using the PyTorch data functionality.

``````random_tensor_ex = random_variable_ex.data
``````

So we have our PyTorch Variable and then at the end we say .data.

We assign this to the random_tensor_ex Python variable.

When we print this variable:

``````print(random_tensor_ex)
``````

you see that we have our torch.IntTensor.

Comparing it to the variable here, the variable said “Variable containing” whereas when we print this, this line is blank and has the actual tensor.

Now that we have the tensor, we can convert it to a NumPy multidimensional array using the .numpy() functionality and we’re going to assign it to the Python variable np_random_mda_ex.

``````np_random_mda_ex = random_tensor_ex.numpy()
``````

We can check the shape of the converted NumPy array:

``````np_random_mda_ex.shape
``````

and we see that it is 2x3x4 which matches the Tensor we had – 2x3x4.

We can check the data type:

``````np_random_mda_ex.dtype
``````

We see that it is int32 and we know that we cast the original PyTorch Variable to int’s, so that is why it’s int32.

We reprint the PyTorch Variable to make sure we can check the values against our NumPy multidimensional array.

``````print(random_variable_ex)
``````

Finally, we visually inspect the values of the NumPy multidimensional array.

``````print(np_random_mda_ex)
``````

We see 16, 46, 90, 14; 16, 46, 90, 14.

And that is how you can transform a PyTorch autograd Variable to a NumPy Multidimensional Array by extracting the PyTorch Tensor from the Variable and converting the Tensor to a NumPy Array.