Learn practical skills, build real-world projects, and advance your career

All about PyTorch tensor operations for Trigonometric functions

PyTorch :

PyTorch is a Python-based scientific computing package that uses the power of graphics processing units. It is also one of the preferred deep learning research platforms built to provide maximum flexibility and speed.

Trigonometric functions :

In mathematics , the trigonometric functions (also called circular functions, angle functions or goniometric functions) are real functions which relate an angle of a right-angled triangle to ratios of two side lengths.

List of functions choosen :-

  • torch.sin
  • torch.cos
  • torch.tan
  • torch.sinh
  • torch.cosh

Before we begin, let's install and import PyTorch

# Import torch and other required modules
import torch

Function 1 - torch.sin

  • The input type is tensor and if the input contains more than one element, element-wise sine is computed.
  • It expects the input in radian form and the output is in the range [-1, 1].

Syntax :

 -> torch.sin(x, out=None)

Parameters :

  ->  x: Input tensor
  ->  name (optional): Output tensor

Return type :

  -> A tensor with the same type as that of x. 
# Example 1 - working 
# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5]) 
print("Tensor a = ",a) 

# Applying the sin function and 
# storing the result in 'b' 
b = torch.sin(a)
print("All sin values of Tensor 'a' =",b) 


Tensor a = tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000]) All sin values of Tensor 'a' = tensor([ 0.8415, -0.4794, -0.2555, -0.8632, 0.0000, -0.2151])

Here we got a new tensor with the sine of the elements of input.