PyTorch Tensor To List: Convert a PyTorch Tensor To A Python List

PyTorch Tensor To List: Use PyTorch tolist() to convert a PyTorch Tensor into a Python list

PyTorch Tensor To List: Use PyTorch tolist() to convert a PyTorch Tensor into a Python list

Video Transcript


First, we import PyTorch.

import torch


Then we check the PyTorch version we are using.

print(torch.__version__)

We are using PyTorch version 1.0.1.post2.


Next, let’s create a PyTorch tensor full of floating point numbers.

pytorch_tensor = torch.tensor(
[
  [
      [0.1, 0.2, 0.3],
      [0.4, 0.5, 0.6],
      [0.7, 0.8, 0.9]
  ],
  [
      [1.1, 1.2, 1.3],
      [1.4, 1.5, 1.6],
      [1.7, 1.8, 1.9]
  ]
]
)

We’re going to use torch.tensor, we’re going to pass in the data structure, and then we’re going to assign it to the Python variable pytorch_tensor.


To confirm that it’s a PyTorch tensor, let’s use the Python type operation.

type(pytorch_tensor)

So type pytorch_tensor .

We evaluate it and we see that the class is torch.Tensor.

So it is a PyTorch tensor.


Let’s also print the PyTorch tensor before we convert it to a Python list to make sure that it is what we set out to create.

print(pytorch_tensor)

We see that it’s a tensor.

We see that it is 2x3x3, and that it contains floating point numbers which we can tell because all of the numbers have decimal places.


Next, let’s use the PyTorch tolist operation to convert our example PyTorch tensor to a Python list.

python_list_from_pytorch_tensor = pytorch_tensor.tolist()

So you can see we have tolist() and then we assign the result to the Python variable python_list_from_pytorch_tensor.


The first thing we can do is we can print to see what it looks like.

print(python_list_from_pytorch_tensor)

We see that it does look like a nested Python list.

We can also see that all of the original numbers from the PyTorch tensor are there inside of the Python list we just created.


Finally, just to make sure we’ve converted the PyTorch tensor to a list, we want to check three main things: (a) that it is a Python list, (b) that the nested list has preserved the tensor structure, and (c) that the numbers are still floating point numbers.


First, let’s check what kind of object the Python variable python_list_from_pytorch_tensor actually is.

type(python_list_from_pytorch_tensor)

We see that it is a Python list.


Next, let’s check to make sure it’s a nested list that preserves the PyTorch tensor structure.

python_list_from_pytorch_tensor[0][1][2]

So here, we see that we have a matrix that is 3x3.

So with our nested list, we want to select the first list, and then within that, we want to select the second element, remembering that Python is a zero-based index language, and then we want to select the third element.

So here, we would say 0, then 1, and then 3.

So we’re looking for this number.

That would have been the one in the PyTorch tensor.

And here, we will be looking for this number.

And that’s the number that we get.

So we see that the nested list has preserved the PyTorch tensor structure.


Lastly, we can use the Python type operation to check to make sure that the numbers are still floating point numbers after converting it from a PyTorch tensor to a Python list.

type(python_list_from_pytorch_tensor[0][1][2])

So we’re going to use the same number we just got from the nested list above and this time, we’re going to run it through the Python type operation.

And we see that the number is of class floating.


Perfect! We were able to use PyTorch’s “to list” operation to convert a PyTorch tensor into a Python list.

Receive the Data Science Weekly Newsletter every Thursday

Easy to unsubscribe at any time. Your e-mail address is safe.