5 PyTorch Functions You Need to Know.
By: Faysal Mh
Tags: Pytorch, Tensor Package, Torch, Pytorch Function, Tensor

Next import torch library.

# import torch

Function 1 — TORCH.MOVEDIM

torch.movedim (input, source, destination)

The movedim function moves the destination of input at the position in the source to the position in the destination. Other dimensions of input remain in their original order. It takes three arguments input, source, and destination. Source and destination must be unique. Let’s see an example, so that makes it easier to understand.

In the first example, we simply moved from a matrix to another matrix. Here t1 is the input, 1 is the source, and 0 is the destination.

Here is another example of more clarity.

In this example, more specific at source and destination.

In these two examples, we saw how it works. Now we’d see if we break this rule, then what would happen?

Here we try to move from a 1D array to a 3D array that is impossible.

This function is used when moving an axis from a source to a destination. Especially when working with a variable dimension tensor.

Function 2 — TORCH.CHUNK

torch.chunk (input, chunks, dim=0)

The chunk function returns, splitting a tensor into a specific number of chunks. Each chunk is a view of the input tensor. The last chunk will be smaller if the tensor size of a dimension is not divisible by chunks. It takes three arguments input, chunks, and dimension. Let’s see an example, so that makes it easier to understand.

In this above example, the tensor split and the number of chunks returns with dimension.

Here is another example.

Here the tensor split one dimension.

In these two examples, we saw how it works. Now we’d see if we use it in the large dimension?

There is an error because we try to chunk large dimensions.

Using this function, we can manipulate data of our tensor by chunk function. This is a very useful function in data science.

Function 3 — TORCH.DSTACK

torch.dstack(tensors, *, out=None)

The dstack function stack tensors in sequence depthwise. This is equivalent to concatenation along the third axis after 1D and 2D tensors have been reshaped by thorch.atleast_3d().

The parameters of this function include tensors, where the sequence of tensor to concatenate and out is the output tensor that is optional.

In this example, t1 & t2 both 1D tensor have been reshaped by 3D tensors.

Here t1 & t2 both 2D tensors have been reshaped by 3D tensors.

Here is the runtime error occurred, why so?

We know that stack tensors in sequence depthwise, but in this above example, we see that an extra element in the t2 tensor.

When we work with data dstack is a very useful function

Function 4 — TORCH.HSTACK

torch.hstack(tensors, *, out=None)

Like dstack function hstack function stack tensors in sequence but horizontally. This is equivalent to concatenation along the first axis for 1D tensors, and along the second axis for all other tensors.

The parameters of this function include tensors, where the sequence of tensor to concatenate and out is the output tensor that is optional as like dstack function.

Here t1 & t2 both 1D tensors concatenated horizontally.

In this example, a 2D tensor is stacked along the second axis for all other tensors.

Here is an error because hstack argument must be a tuple of tensors we try to stack on a tensor.

Like dstack function, hstack function also a lot of use in data science.

Function 5 — TORCH.STD

torch.std(input, unbiased=True)

The std function returns the standard deviation of all elements in the input tensor.

The arguments of this function include input where the input tensor and unbiased whether to use the unbiased estimation or not.

If unbiased is False , then the standard deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Here is a simple example of torch.std that find the standard deviation of all the elements.

This example finds the standard deviation of all elements in the above tensor.

Here is an error because we input all those elements as integers. std function supports only floating types.

It is a math-related function that finds the value of the dispersion.

Conclusion

This notebook is about basic tensor operations. Its main purpose is to understand the PyTorch tensors and get hands-on with them. In this post, I’m trying to explain a simple way with examples so that this function's usage more clear.