We import PyTorch.
import torch
Then we print the torch version we are using.
print(torch.__version__)
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.
print(x)
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.
type(random_tensor)
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.
type(integers_only)
We can see that it is in fact a PyTorch IntTensor.