A tensor is an array having three or more dimensions. However, it is commonplace to call
We can create tensors in PyTorch the same way we create arrays in NumPy. By using the tensor()
function in PyTorch, we can create scalars and tensors.
The syntax of the tensor()
function is as follows:
torch.tensor(data,*,dtype=None,device=None,requires_grad=False,pin_memory=False)
The following parameters are required by the tensor()
function:
data
(array_like): This is the initial data for the tensor. It can be a list, tuple, NumPy, scalar, or other types.
dtype
: This is the desired data type of returned tensor and is optional.
device
: This is the device of the constructed tensor. If it is None
and the data is a tensor, then the device of the data is used. Otherwise, it is built on the CPU.
requires_grad
: If autograd is enabled it should record operations on the returned tensor. This is an optional parameter.
pin_memory
: If it is set, then the returned tensor would be allocated in the pinned memory. This is an optional parameter.
Tensors in PyTorch have equivalent functions as their NumPy counterparts like ones()
, zeros()
, rand()
, randn()
, and so on.
In the following example, we create four tensors of different dimensions:
import torch# creating a scalar, and three tensors (vector, matrix, tensor)_scalar = torch.tensor(3.14159)_vector = torch.tensor([1, 2, 3])_matrix = torch.ones((2, 3), dtype=torch.float)_tensor = torch.randn((5, 6, 7), dtype=torch.float)print(_scalar)print(_vector)print(_matrix)print(_tensor)
Line 4: A scalar is created using the tensor()
function and a float value is passed as an argument.
Line 5: A vector is created using the tensor()
function, and a list is passed as an argument.
Line 6: A matrix is created using the ones()
function and the dtype
(datatype) is passed as float
.
Line 7: A tensor is created using the randn()
function and the dtype
is passed as float
.
We can get the shape of a tensor by using the size()
function or the shape
attribute, as demonstrated in the following code:
import torch# creating a tensor and printing it's shapetensor = torch.randn((5, 6, 7), dtype=torch.float)print(tensor.size(), tensor.shape)
Line 4: We create a tensor using the radn()
function and the dtype
is defined as float
.
Line 5: The size()
and shape
attribute is used to print the size and shape of the tensor.
Note: We can also reshape tensors using
view()
orreshape()
functions.
If we want to copy all data in the memory, we may use either its new_tensor()
or clone()
functions, as demonstrated in the following code:
import torchmatrix = torch.ones((2, 3), dtype=torch.float)different_matrix = matrix.new_tensor(matrix.view(1, 6))different_matrix[0, 1] = 3.print(matrix)print(different_matrix)
Line 3: We create a tensor using the ones()
function with float
as the dtype
.
Line 4: We create a new tensor using new_tensor()
and the first matrix is passed as an argument.
Note: In the above code, we might be getting a
UserWarning
due tonew_tensor()
. PyTorch prefers that we useclone()
together withdetach()
instead ofnew_tensor()
.
The following code uses the clone()
function with the detach()
function:
import torchmatrix = torch.ones((2, 3), dtype=torch.float)# Lets follow PyTorch's suggestion and use "clone" methodanother_matrix = matrix.view(1, 6).clone().detach()# Again, if we change one of its elements...another_matrix[0, 1] = 4.# The original tensor (matrix) is left untouched!print(matrix)print(another_matrix)
Line 3: We create a tensor using the ones()
function with float
as the dtype
.
Line 5: We create a new tensor using clone()
and the detach()
function removes the tensor from the computation graph.
Try to solve the following quiz to test your understanding:
Which function is recommended by PyTorch when we have to create a copy of a tensor?
new_tensor
clone
reshape
view
Free Resources