5 Interesting PyTorch Functions for beginners

An short introduction about PyTorch and the chosen functions.

  • erfinv
  • linspace
  • transpose
  • quasirandom.SobolEngine
  • lgamma

What do you want to know about 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, primarily developed by Facebook’s AI Research lab (FAIR). It is free and open-source software released under the Modified BSD license.

How do you install Pytorch?

For windows,

For MAC OS,

For Linux,

How to import Pytorch?

To import PyTorch,

Function 1 — torch.erfinv

torch.erfinv is one of the math operations which computes the inverse error function of each element of output.

The inverse error function is defined in the range of (-1,1) as:

erfinv(erf(x))=x

Out[]:

Here, it is showing the output after applying erfinv fuction.

In [ ]:

Out[]:

The above example are showing output after applying erfinv function.

In[]:

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last)

<ipython-input-23-13f84a2ed394> in <module>()

1 # Example 3 - breaking (to illustrate when it breaks) ---->

2 torch.erfinv(torch.tensor([1,3,4]))

RuntimeError: erfinv_vml_cpu not implemented for 'Long'

The values in tensor functions are yielding Long Tensors which can not be interpreted by the torch.erfinv function. It can be solved by entering at least one value as a float. for eg.- 1 as 1.0 .

Function 2 — linspace

linspace function creates a one-dimensional tensor of size steps whose values are evenly spaced from start to end, inclusive. That is, the value are:

(start,start+end-start/steps,…,start +(steps-1)*end-start/steps, end)

In [5]:

Out[5]:

Here we have a 1-dimensionl array as the output where the element’s starting value is 5,and then it is depreciating towards the value of 2 in 5 steps, and the middle values are shown according to the steps given in linspace function.

Out[16]:

We can also get a 1-dimensional array with negative values by giving starting value negative and end value also negative if you want.

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last)

<ipython-input-23-6970e8d7033d> in <module>()

1 # Example 3 - breaking (to illustrate when it breaks) ---->

2 torch.linspace(2,50,steps=-20)

RuntimeError: number of steps must be non-negative

In the above example, it was shown that 1-D array can be created with negative values by specifying start point and point with negative values.But,same is not applicable for giving a negative value in steps parameter.

It is not advisable to skip giving value to step parameter as it is possible that in future PyTorch versions, the output will give error.

The linspace function can be used for deep neutral network and nlp purposes.

Function 3 — transpose

torch.transpose(input,dim0,dim1)

Transpose function returns a tensor with a transposed version of input. The resulting out tensor shares it’s underlying storage with the input tensor, so changing the values would change the value of the other.

In [52]:

Out[52]:

The Output is showing a tensor with transposed version of x tensor and also the dimensions are swapped.

Out[54]:

Explanation about example

In [62]:

--------------------------------------------------------------------------- IndexError Traceback (most recent call last)

<ipython-input-62-1581f2897b4e> in <module>()

1 # Example 3 - breaking (to illustrate when it breaks)

2 z=torch.randn(3,1,2) ----> 3 torch.transpose(z,2,3)

IndexError: Dimension out of range (expected to be in range of [-3, 2], but got 3)

In the above example, index error was shown as dim1 parameter was not in the expected range.

The Transpose function can be used during matrices multiplication.

Function 4 — sobolengine

torch.quasirandom.SobolEngine(dimension,scramble=False,seed=None)

This function is an engine for generating(scrambled) Sobol sequences which are an example of low discrepancy quasi-random sequences.

In [65]:

Out[65]:

In the above example, the output is showing sobol sequences in the tensor with dimensions acting as number of columns.

Out[80]:

In the above example,we have produced a scrambled Sobol sequences with the scramble parameter.

In [82]:

--------------------------------------------------------------------------- ValueError Traceback (most recent call last)

<ipython-input-82-da5ae28173d6> in <module>()

1 # Example 3 - breaking (to illustrate when it breaks)

----> 2 soboleng=torch.quasirandom.SobolEngine(0)

3 soboleng.draw(3)

/usr/local/lib/python3.6/dist-packages/torch/quasirandom.py in __init__(self, dimension, scramble, seed)

46 def __init__(self, dimension, scramble=False, seed=None):

47 if dimension > self.MAXDIM or dimension < 1:

---> 48 raise ValueError("Supported range of dimensionality "

49 f"for SobolEngine is [1, {self.MAXDIM}]")

50 ValueError: Supported range of dimensionality for SobolEngine is [1, 1111]

The output is showing a ValueError as the dimension 0 is not acceptable in the supported range of dimensionality which is [1,1111].

The sobolengine can be used for sampling sequences upto a maximum dimension of 1111.

Function 5 — lgamma

torch.lgamma(input,*,out=None)

The lgamma function is used to compute the logarithm of the gamma function on input.

In [86]:

Out[86]:

In the above example, the output is showing the values of logarithm of the gamma function on x tensor.

In [103]:

Out[103]:

In the above example, the output is showing the values of logarithms of the gamma function of b tensor.

In [113]:

--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last)

<ipython-input-113-af9c7eaf8101> in <module>()

1 # Example 3 - breaking (to illustrate when it breaks)

2 z=torch.arange(-1.0,3,1.0) ----> 3 torch.lgamma(z^2)

RuntimeError: "bitwise_xor_cpu" not implemented for 'Float'

In the above example, the output is showing RuntimeError as the bitwise operation is not implemented for float

The lgamma function can be used to support broadcasting to a common shape and float inputs.

Conclusion

In this session, we have covered 5 different types of Pytorch functions for beginners. There are more functions which you can explore through the official Pytorch website. Also, It would be advisable to run the code yourself after reading the story.

Reference Links

Provide links to your references and other interesting articles about tensors

Also, any Feedback would be much appreciated.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store