Sunday, December 29, 2024
Google search engine
HomeLanguagesCreating a Tensor in Pytorch

Creating a Tensor in Pytorch

All the deep learning is computations on tensors, which are generalizations of a matrix that can be indexed in more than 2 dimensions. Tensors can be created from Python lists with the torch.tensor() function.

The tensor() Method:

To create tensors with Pytorch we can simply use the tensor() method:

Syntax: 

torch.tensor(Data)

Example:

Python3




import torch
  
V_data = [1, 2, 3, 4]
V = torch.tensor(V_data)
print(V)


Output: 

tensor([1, 2, 3, 4])

To create a matrix we can use:

Python3




import torch
  
M_data = [[1., 2., 3.], [4, 5, 6]]
M = torch.tensor(M_data)
print(M)


Output: 

tensor([[1., 2., 3.],
        [4., 5., 6.]])

To create a 3D tensor you can use the following code template:

Python3




import torch
  
T_data = [[[1., 2.], [3., 4.]],
          [[5., 6.], [7., 8.]]]
T = torch.tensor(T_data)
print(T)


Output: 

tensor([[[1., 2.],
         [3., 4.]],

        [[5., 6.],
         [7., 8.]]])

However, if we run the following code:

Python3




import torch
  
x = torch.tensor([[1, 2], [3, 4, 5]])
print(x)


Output: 

ValueError: expected sequence of length 2 at dim 1 (got 3)

This happens because Tensors are basically matrices, and they cannot have an unequal number of elements in every dimension.

The randint() method:

The randint() method returns a tensor filled with random integers generated uniformly between low (inclusive) and high (exclusive) for a given shape. The shape is given by the user which can be a tuple or a list with non-negative members. The default value for low is 0. When only one int argument is passed, low gets the value 0, by default, and high gets the passed value. Like zeros() an empty tuple or list for the shape creates a tensor with zero dimension.

Syntax: torch.randint(<low>,<high>,<shape>)

Example:

Python3




import torch
  
randint_tensor = torch.randint(5, (3,3))
print(randint_tensor)


Output:

tensor([[3, 2, 3],
        [4, 4, 0],
        [3, 3, 4]])

The complex() method:

The complex() method takes two arguments (real and imag) and returns a complex tensor with its real part equal to real and its imaginary part equal to imag where both real and imag are tensors having the same datatype and same shape.

Syntax: torch.complex(<real tensor>,<another real tensor which is to be used as imaginary part>)

Example:

Python3




import torch
  
real = torch.rand(2, 2)
print(real)
imag = torch.rand(2, 2)
print(imag)
complex_tensor = torch.complex(real, imag)
print(complex_tensor)


Output:

tensor([[0.7655, 0.7181],
        [0.8479, 0.0369]])
tensor([[0.5604, 0.0012],
        [0.6316, 0.6574]])
tensor([[0.7655+0.5604j, 0.7181+0.0012j],
        [0.8479+0.6316j, 0.0369+0.6574j]])

The eye() method:

The eye() method returns a 2-D tensor with ones on the diagonal and zeros elsewhere(identity matrix) for a given shape (n,m) where n and m are non-negative. The number of rows is given by n and columns is given by m.  The default value for m is the value of n and when only n is passed, it creates a tensor in the form of an identity matrix.

Syntax: torch.eye(<n,m or the shape>)

Example:

Python3




import torch
  
n = m = 3
eye_tensor = torch.eye(n, m)
print(eye_tensor)


Output:

tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]])

The zeros() method:

This method can be used when you need a tensor where all elements are zeros, of a specified shape. The shape can be given as a tuple or a list or neither. If you pass an empty tuple or an empty list then the zeros() method returns a tensor of shape (dimension) 0, having 0 as its only element, whose data type is float. Negative numbers or float cannot be passed as a shape.

Syntax: 

torch.zero(D1,D2)

Here,
D1: It represents the horizontal dimension of the tensor.
D2: It represents the vertical dimension of the tensor.

Example:

Python3




import torch
  
zeros_tensor = torch.zeros(3,2)
print(zeros_tensor)


Output: 

tensor([[0., 0.],
        [0., 0.],
        [0., 0.]])

The rand() method:

The rand() method returns a tensor filled with random numbers from a uniform distribution on the interval 0 (inclusive) to 1 (exclusive) for a given shape. The shape is given by the user and can be given as a tuple or list or neither. Similar to zeros() and ones() passing an empty tuple or list creates a scalar-tensor of zero dimension.  Like zeros() the shape argument only takes a tuple or a list with non-negative members. An empty tuple or list creates a tensor with zero dimension.

The rand() method can be used to set random weights and biases in a neural network.

Syntax: torch.rand(<shape>)

Python3




import torch
  
rand_tensor = torch.rand(3, 3)
print(rand_tensor)


Output:

tensor([[0.6307, 0.7281, 0.0130],
        [0.7359, 0.0241, 0.2845],
        [0.2154, 0.3773, 0.6545]])

 

The ones() method:

Similar to zeros(), ones() returns a tensor where all elements are 1, of specified size (shape).

Syntax: torch.tensor(<shape>)

Example:

Python3




import torch
  
ones_tensor = torch.ones((4,4,4))
print(ones_tensor)


Output: 

tensor([[[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]],

        [[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]],

        [[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]],

        [[1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.],
         [1., 1., 1., 1.]]])

The arange() method:

The arange() method is used to get a 1-D tensor(row matrix), with elements from start (inclusive) to end (exclusive) with a common difference step (the default value for start is 0 while that for step is 1). The elements of the tensor can be said to be in Arithmetic Progression, with the given step as a common difference. All three parameters, start, end, and step can be positive, negative, or float.

Syntax: torch.arange(<start>,<end>,<step-size>)

Example:

Python3




import torch
  
arange_tensor = torch.arange(2, 20, 2)
print(arange_tensor)


Output:

 tensor([ 2,  4,  6,  8, 10, 12, 14, 16, 18])

The full() method:

The full() method is used when we need a tensor of a shape given by the shape argument, with all its elements equal to the fill value given by the user.  Again, passing an empty tuple or list creates a scalar-tensor of zero dimension. While using full, it is necessary to give shape as a tuple or a list (which can be empty), or it throws an error. Also, the members of the shape list cannot be negative or float.

Syntax: torch.full(<shape>,<value to be filled with>)

Example:

Python3




import torch
  
full_tensor = torch.full((3,2), 3)
print(full_tensor)


Output:

tensor([[3, 3],
        [3, 3],
        [3, 3]])

 

The linspace() method:

The linspace() method returns a 1-D dimensional tensor too(row matrix), with elements from start (inclusive) to end (inclusive). However, unlike arange(), we pass the number of elements that we need in our 1D tensor instead of passing step size (as shown above). Pytorch calculates the step automatically for the given start and end values. It is understandable that the number of elements can only be a non-negative integer.

Syntax: torch.linspace(<start> , <end>, <number of elements>)

Example:

Python3




import torch
  
linspace_tensor = torch.linspace(1, 7.75, 4)
print(linspace_tensor)


Output: 

tensor([ 1.0000,  3.2500,  5.5000,  7.7500])

Unlike arange(), in linspace() we can have a start greater than end since the common difference is automatically calculated.

Tensor([[0.6307, 0.7281, 0.0130],
        [0.7359, 0.0241, 0.2845],
        [0.2154, 0.3773, 0.6545]])

And that’s how we can create tensors in PyTorch. 

RELATED ARTICLES

Most Popular

Recent Comments