PyTorch For Deep Learning — Tensor Basics

Ashwin Prasad
Analytics Vidhya
Published in
4 min readSep 9, 2020

What is Pytorch ?

Pytorch is a Deep Learning Library Devoloped by Facebook. it can be used for various purposes such as Natural Language Processing , Computer Vision, etc

Prerequisites

Python, Numpy, Pandas and Matplotlib

Tensor Basics

What is a tensor ?
A Tensor is a n-dimensional array of elements. In pytorch, everything is a defined as a tensor.

Some Tensor operations with PyTorch:

before we do anything with pytorch , we need to import the torch library.
and let’s create a numpy array with numpy

#importing the required libraries
import torch
import numpy as np
#creating numpy array
data = np.array([1,2,3,4,5])

Now , we can convert this numpy into Torch Tensor in 4 different ways

#different ways for creating tensors
output1 = torch.Tensor(data)
output2 = torch.tensor(data)
output3 = torch.from_numpy(data)
output4 = torch.as_tensor(data)
print(“{}\n{}\n{}\n{}”.format(output1,output2,output3,output4))
Output:
tensor([1., 2., 3., 4., 5.])
tensor([1, 2, 3, 4, 5])
tensor([1, 2, 3, 4, 5])
tensor([1, 2, 3, 4, 5])

let’s see the difference between these methods:

  1. The data type of elements in the tensor is float by default
  2. The data type of the elements in the tensor is by default the data types of the elements itself.
  3. Shares data between the numpy array and the tensor. changes in the numpy array is reflected to the tensor object and vice versa
  4. This is same as 3 but is similar to 2. because , the default data type is that of the data original data itself

Any one of these above methods can be use. it’s based on the use cases and is the programmer’s choice

Some Useful Tensor Functions in PyTorch

1) Eye Function : it’s a function that can be used to create an identity matrix of given dimensions

# Eye Functiontorch.eye(5)Output: tensor([[1., 0., 0., 0., 0.],         
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.],
[0., 0., 0., 1., 0.],
[0., 0., 0., 0., 1.]])

2) zeros Function : This Function is used to create a tensor of given dimensions with all the elements being 0.

#creating a tensor full of value '1'torch.ones(4,4)Output:tensor([[0., 0.],         
[0., 0.]])

3) Ones Function: This Function is used to create a tensor of given dimensions with all the elements being 1.

#creating a tensor full of value '1'torch.ones(4,4)Output:tensor([[1., 1., 1., 1.],         
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])

4) Rand : creates a tensor of specified dimensions with random values

#creating tensor with random valuestorch.rand(5,3)Output:tensor([[0.4212, 0.1785, 0.6450],         
[0.0284, 0.9095, 0.9114],
[0.0811, 0.5611, 0.2545],
[0.1132, 0.3313, 0.9921],
[0.2310, 0.4237, 0.6206]])

5) Reshape and Flatten ( shaping functions ) : changes the shape of the tensor.
note: the shape of the tensor can only be changed if the resultant tensor has the same number of elements as the original one

#reshaping tensors
output5 = torch.rand(6,4)
print(output5,"\n")
print(output5.reshape(12,2),"\n")
#flattening the tensor
print(output5.flatten())
Output :
tensor([[0.2267, 0.3610, 0.3366, 0.1552],
[0.3319, 0.3566, 0.3814, 0.9854],
[0.4764, 0.1241, 0.8957, 0.2228],
[0.6024, 0.0024, 0.4686, 0.9024],
[0.0275, 0.3080, 0.7966, 0.8439],
[0.3685, 0.7664, 0.6974, 0.0545]])
tensor([[0.2267, 0.3610],
[0.3366, 0.1552],
[0.3319, 0.3566],
[0.3814, 0.9854],
[0.4764, 0.1241],
[0.8957, 0.2228],
[0.6024, 0.0024],
[0.4686, 0.9024],
[0.0275, 0.3080],
[0.7966, 0.8439],
[0.3685, 0.7664],
[0.6974, 0.0545]])
tensor([0.2267, 0.3610, 0.3366, 0.1552, 0.3319, 0.3566, 0.3814, 0.9854, 0.4764,0.1241, 0.8957, 0.2228, 0.6024, 0.0024, 0.4686, 0.9024, 0.0275, 0.3080,0.7966, 0.8439, 0.3685, 0.7664, 0.6974, 0.0545])

6) Squeeze and Unsqueeze operations: the squeeze function removes redundant dimensions whereas the unsqueeze function adds an extra dimension to the tensor

output6 = torch.as_tensor([[1,2,3,4,5,6,6,7]])print("{}\n{}\n{}".format(output6,output6.squeeze(), output6.squeeze().unsqueeze(dim=0)))output: tensor([[1, 2, 3, 4, 5, 6, 6, 7]]) 
tensor([1, 2, 3, 4, 5, 6, 6, 7])
tensor([[1, 2, 3, 4, 5, 6, 6, 7]])

7) Tensor Concatenation : concatenates 2 tensors.
notice the difference when the axis parameter is changed

#tensor concatenationtensor1 = torch.tensor([[1,2,3,4,5]])
tensor2 = torch.tensor([[6,7,8,9,10]])
print("{}\n{}".format(torch.cat((tensor1,tensor2),axis=1), torch.cat((tensor1,tensor2),axis=0)))Output:tensor([[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]])
tensor([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])

Conclusion

PyTorch is a very powerful and highly capable library and I personally love it because of a lot of reasons.
some of them are :
1.It’s abstract but not way too abstract

2.Utilises Object Oriented Programing

3.we can define everything on our own and know what’s happening instead of calling some functions.

4.Pytorch’s AutoGrad

5. it’s highly pythonic and we feel like we are coding in regular python while using PyTorch

So, These are the basic Tensor operations and functions in Pytorch and that’s it for this part.

The code files will be available at my github repo

Thank You

--

--

Ashwin Prasad
Analytics Vidhya

I write about things that intrigue me on any field of Computer Science, with more weightage to Machine Learning and Systems Programming