Tensor Operations in Pytorch explained with code

Original article was published on Deep Learning on Medium

Tensor Operations in Pytorch explained with code

Intro to Pytorch

Pytorch is an open-source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing. It was developed by the Facebook AI Research lab(FAIR) primarily.

Tensor Operations

In Pytorch, Tensor is a multi-dimensional matrix that can contain various elements of the same data type. It can be a number, vector, matrix, or any n-dimensional array. Now, let us have a look at some functions of it.


The rand function returns a tensor with random elements that are uniformly distributed over the interval [0,1).

torch.rand(*size, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

<iframe src=”https://jovian.ml/embed?url=https://jovian.ml/sswaroop46/01-tensor-operations/v/1&cellId=1″ title=”Jovian Viewer” height=”112″ width=”800″ frameborder=”0″ scrolling=”auto”></iframe>

At first here, We imported the required modules torch and NumPy.

<iframe src=”https://jovian.ml/embed?url=https://jovian.ml/sswaroop46/01-tensor-operations/v/1&cellId=3″ title=”Jovian Viewer” height=”136″ width=”800″ frameborder=”0″ scrolling=”auto”></iframe>

Here in the above example, We are passing two parameters which are size and requires_grad. As we passed 4 as size, 4 random numbers are generated. We have to set requires_grad to true whenever we need the autograd to record operations on the returned tensor or for computing gradients using the backward function. requires_grad sets to False by default. So, here the rand function returned tensor with 4 randomly generated values assigned to variable a.

<iframe src=”https://jovian.ml/embed?url=https://jovian.ml/sswaroop46/01-tensor-operations/v/1&cellId=5″ title=”Jovian Viewer” height=”140″ width=”800″ frameborder=”0″ scrolling=”auto”></iframe>

Here, We send two parameters ‘2′(No. of rows) and ‘4’(No. of columns) as a size to rand function which then returns a tensor of a 2-dimensional array with 8 random values.

<iframe src=”https://jovian.ml/embed?url=https://jovian.ml/sswaroop46/01-tensor-operations/v/1&cellId=7″ title=”Jovian Viewer” height=”244″ width=”800″ frameborder=”0″ scrolling=”auto”></iframe>

For rand function, datatype cannot be torch.int32 or torch.int64 for a uniform distribution but it can be torch.float32 or torch.float64 datatype.