Original article was published on Deep Learning on Medium
5 Important Pytorch tensor functions
PyTorch is an open source machine learning library based on the Torch library, used for various deep learning applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab (FAIR).
Tensorflow, Keras, MXNet, Caffe2 etc. are some of the alternatives of pytorch.
A tensor is an n-dimensional data container. It is quite similar to NumPy’s ndarray. For example, 1d-tensor is a vector, 2d-tensor is a matrix, 3d-tensor is a cube, and 4d-tensor is a vector of cubes.
These tensors are used as basic building blocks of a deep learning network.
Creating a tensor
We can create tensors using tensor() method from torch module.
We can make a tensor as follows:
Here we have made a 2-D tensor. We can make tensors of different dimensions in similar way.
We will be going through 5 basic Pytorch tensor methods :
We will also see their working examples along with where they break.
Returns a tensor filled with random numbers from a uniform distribution on the interval [0,1)
The size of the tensor can be passed as a list or tuple.
The above code returned a tensor of size (2,3) having values between 0 and 1.
The above code returned a 4 x 1 tensor. It is a 1D tensor.
We can not pass float values as size of a tensor.
We can use this method to create tensors with random values which lie between 0 and 1. They can be used as weight tensors in model building.
Splits a tensor into a specific number of chunks.
- tensor -number of parts
- dimension across which to divide
The function divided the tensor x into 3 equal parts.
Here as we provided the dimension as 1, it divided the tensor column-wise.
As x is a 1D tensor, it shown an error when we asked the function to divide the tensor by 2nd dimension.
We can use the method to divide the tensor into parts or batches to operate on them separately or iteratively rather than all the values at same time.
Concatenates the given sequence of seq tensors in the given dimension.
All tensors must either have the same shape (except in the concatenating dimension) or be empty.
- list or tuple of tensors
- dimension along which to concat
- output tensor
We combined here three tensors (we got by splitting tensor x) across dimension 0(row) to get x again.
We combined here three tensors (we got by splitting tensor y) across dimension 1(column) to get y again.
For concatenation, the tensors should have same dimension.
We can use this method to join tensors. But we need to ensure that the tensors should be same across all dimensions except across which we concat.
Sorts the elements of the input tensor along a given dimension in ascending order by value.
If dim is not given, the last dimension of the input is chosen.
If descending is True then the elements are sorted in descending order by value.
- dimension across which to sort (optional)
- descending (optional)
- output tensor (optional)
Returns sorted tensor and indices.
The function returned the sorted tensor across its last dimension i.e. it compared elements of the columns of each row and arranged them.
It also returned the positions of the indices of values after sorting.
We can see the tensor sorted across dimension 0 i.e. the values in same column number of the rows and sorted.
The error is there as we set dimension as 2. But the dimensions can be 0 or 1 as it is a 2D tensor.
So, we can use this method to sort the tensors in required dimensions (both in ascending and descending order)
Clamp all elements in input into the range min, max and return a resulting tensor.
If in a tensor:
- value > max then value is converted into max value
- value < min then value is converted into min value
- else min < value < max then no change
- min value
- max value
- output tensor
Here the first value 1 has been converted into min value i.e. 2 and 6.2 has been converted into max value i.e. 6.
We can also mention only the min or max value.
Here as we have mentioned only max as 6, only the values (i.e. 6.2) is converted to 6.
We can not apply this clamp method on tensors with data types other than which are of numeric types.
This method comes really handy when we need to add a limit to our tensor values.
In this notebook we came across 5 basic methods of pytorch functions to work with tensors. Along with when the functions can be used, we should keep an eye on the conditions under which they break.
I will be adding few other interesting and important tensor functions.
You can go through the official Pytorch documentation which has a plethora of such methods with good examples.
Official documentation for Pytorch : https://pytorch.org/docs/stable/index.html
Feel free to give your feedback.
Connect with me at:
# Example 3 - breaking (to illustrate when it breaks)
tensor, indices= torch.sort(t,dim=2)
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-31-9e566a5a45d4> in <module>() 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 tensor, indices= torch.sort(t,dim=2) IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)