PyTorch Tensor Type: Print And Check PyTorch Tensor Type

PyTorch Tensor Type - print out the PyTorch tensor type without printing out the whole PyTorch tensor

Type: FREE   By: Sebastian Gutierrez, Instructor Sebastian Gutierrez   Duration: 1:42   Technologies: Python, PyTorch

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


We import PyTorch.

import torch

Then we print the torch version we are using.


We’re using 0.2.0_4.

We construct an uninitialized PyTorch tensor, we define a variable x and set it equal to torch.Tensor(3, 3, 3).

x = torch.Tensor(3, 3, 3)

We can then print that tensor to see what we created.


A few things to note looking at the printing:

First - All the entries are uninitialized.

Second - The last line tells us that it is a FloatTensor.

Third - Printing the Tensor tells us what type of PyTorch Tensor it is.

And Four - By default, PyTorch Tensors are created using floating numbers.

Next let's create a second tensor, random_tensor, using the PyTorch rand functionality.

random_tensor = torch.rand(3, 3, 3)

This random_tensor tensor is a PyTorch Tensor where each entry is a random number pulled from a uniform distribution from 0 to 1.

To see what the random_tensor Type is, without actually printing the whole Tensor, we can pass the random_tensor to the Python type function.


From this you can see that it is a PyTorch FloatTensor.

Finally, we define an uninitialized PyTorch IntTensor which only holds integers.

integers_only = torch.IntTensor(2, 2, 2)

Even though we know it's an IntTensor since we defined it that way, we can still check the type of the tensor.


We can see that it is in fact a PyTorch IntTensor.

Back to PyTorch Tutorial Lesson List