PyTorch Useful miscellaneous functions

  • by

This story is about the miscellaneous functions in PyTorch which are useful. I have created other stories on PyTorch basics Part1 and Part2.

In this story we will look at some useful functions which are not covered in part1 and 2.

torch.stack this function is also similar to concatenation but it will create a 3-D tensor from stacking 2 tensors.

torch.mean generates mean value of tensor. A simple but useful function, input to this function is tensor itself.

torch.trace as the function name suggests it calculates trace of the tensor. Trace means sum of its principal diagonal elements. Input to this function is tensor itself.

torch.unique will generate a tensor with unique elements from the main tensor which is passed as input.

Trending Bot Articles:

1. 3 Tips for your Voice and Chatbot Program from Gartner’s Customer Service Hype Cycle 2020

2. Deploying Watson Assistant Web Chat in Salesforce Lightning Console

3. Are Chatbots Vulnerable? Best Practices to Ensure Chatbots Security

4. Your Path to AI — An IBM Developer Series

torch.masked_Select is like a filter applied on a tensor to get only specific values from a tensor based on a position. we have two tensors 1st tensor is a main tensor on which you want a filter and 2nd tensor is the mask tensor where values of it are either true or false.

How does it work? e.g: say i have a 4×4 tensor and i want to extract only values which are greater than 0.3 then we create mask based on 4×4 tensor and apply it using masked_select as in the below picture

torch.where is a conditional statement function. This require three inputs 1st is conditional statement eg: X > 0.4. 2nd is the primary tensor X and 3rd is secondary tensor Y. This will apply the condition on tensor X at every index, if a value does not satisfy the condition then value from Y at same index is sent as output.

We can also perform linear equations in PyTorch. e.g: Y = X*X+3X+2I. where I is identity matrix means only diagonal elements are 1.

Tensor operations in GPU MODE . When you are trying to work on tensors in GPU mode make sure that all the tensors are set to GPU compatible else you will see this error.

Here X was on CUDA(GPU) and Y was on CPU mode so it gave that error. To fix this make y also into GPU mode.

These are some useful functions in PyTorch. This story will be modified in future to add more functions. Thanks for reading till the end.

Story on basics of PyTorch: PyTorch Basics Part1 and PyTorch Basics Part2

Next Story is on Scipy and NLP functions like Named Entity Recognition, lemmatization and stemming, POS tagging and N-grams.

Don’t forget to give us your 👏 !

https://medium.com/media/7078d8ad19192c4c53d3bf199468e4ab/href


PyTorch Useful miscellaneous functions was originally published in Chatbots Life on Medium, where people are continuing the conversation by highlighting and responding to this story.

Leave a Reply

Your email address will not be published. Required fields are marked *