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

Tensor operations in PyTorch

Trigonometric Functions in PyTorch

Trigonometric functions are widely used in sciences related to geometric spaces such as: robotics, machine vision, and machine learning.

PyTorch supports various trigonometric functions. This notebooks shows how to use and debbug the following functions:

  • torch.cos()
  • torch.sin()
  • torch.tan()
  • torch.acos()
  • torch.asin()
  • torch.atan()

NOTE: PyTorch just as other libraries assumes that the values are in radians and not in degrees.

# Uncomend this lines in case you don't have the following modules installed

# !pip install pytorch
# !pip install matplotlib
# Import modules
import torch
import matplotlib.pyplot as plt

Function 1 - torch.cos(input_tensor)

This function returns the cosine tensor of the elements contained in the input_tensor

# Example 1 ...
# x is a tensor of size 10 and type 'float32'
x = torch.Tensor([10., 20, 30, 40, 50, 60, 70, 80, 90, 100])

# print the x tensor and its dtype
print('x is a tensor type: ', x.dtype)
print(x)

# apply torch.sin() to the x tensor
y = torch.cos(x)

# print the output of the tensor sin_x
print('The cosine of the tensor x is: ')
print(y)