How are tensors created using PyTorch
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.
Syntax
The syntax of the tensor() function is as follows:
torch.tensor(data,*,dtype=None,device=None,requires_grad=False,pin_memory=False)
Parameters
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 isNoneand 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.
Create tensors using PyTorch
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)
Explanation
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 thedtype(datatype) is passed asfloat.Line 7: A tensor is created using the
randn()function and thedtypeis passed asfloat.
Get the size and shape of a tensor
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)
Explanation
Line 4: We create a tensor using the
radn()function and thedtypeis defined asfloat.Line 5: The
size()andshapeattribute is used to print the size and shape of the tensor.
Note: We can also reshape tensors using
view()orreshape()functions.
Copy a tensor
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)
Explanation
Line 3: We create a tensor using the
ones()function withfloatas thedtype.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
UserWarningdue 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)
Explanation
Line 3: We create a tensor using the
ones()function withfloatas thedtype.Line 5: We create a new tensor using
clone()and thedetach()function removes the tensor from the computation graph.
Exercise
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