Sine is a basic trigonometric function. While working with tensors in PyTorch, users may want to calculate the sine of each element in the specific tensor. PyTorch provides a “torch.sin()” method to perform this operation. This method accepts a specific input tensor and returns a tensor with the calculated sine values of tensor elements. 

This blog will illustrate the method to compute the sine of specific tensor elements in PyTorch. 

How to Find/Calculate Sine of Specific Tensor Elements in PyTorch?

To calculate/compute tensor elements’s sine in PyTorch, follow the below-provided steps:

  • Import PyTorch library
  • Define the desired tensor
  • Calculate the sine of tensor elements using the “torch.sin()” method 
  • Print the calculated sine values of the tensor elements

Go through the following examples for a practical demonstration:

Example 1: Calculate the Sine of 1D Tensor Elements

In the first section, create a 1D tensor and compute the sine of its elements. 

Step 1: Install PyTorch Library

First, install the “torch” library to calculate/find the sine of tensor elements:

import torch

Step 2: Define a 1D Tensor 

Then, define a 1D tensor and print its elements. For example, we are making a “T1” tensor from a list using the “torch.Tensor()” function:

T1 = torch.Tensor([2.9, 6.3, 4.1, 9.7, 7.8])

print(T1)

This has created a 1D tensor:

Step 3: Calculate the Sine of Tensor Elements 

Now, utilize the “torch.sin()” method and pass the above-created input tensor as a parameter to calculate the sine of its elements:

Tens_sine = torch.sin(T1)

Step 4: Print Calculated Sine of Tensor Elements 

Finally, display the calculated sine values of the tensor elements:

print(Tens_sine)

The below output shows the computed sine values of the “T1” tensor elements:

Example 2: Calculate the Sine of 2D Tensor Elements

In the second section, define a 2D tensor and compute the sine of its elements.

Step 1: Install PyTorch Library

First, install the “torch” library:

import torch

Step 2: Define a 2D Tensor 

Then, define the 2D tensor and print its elements. Here, we are defining a “T2” tensor through the “torch.Tensor()” function:

T2 = torch.Tensor([[2.3, 5.3, 4.4, 9.1],
                  [0.2, 0.3, 0.5, 0.7],
                  [1.1, 0.3, 8.4, 4.9]])
print(T2)

This has created the 2D tensor as seen below:

Step 3: Calculate the Sine of Tensor Elements 

Now, compute the sine of the above-created tensor elements using the “torch.sin()” method and passing the input tensor as a parameter:

Tens_sine = torch.sin(T2)

Step 4: Print Calculated Sine of Tensor Elements 

Lastly, display the calculated sine values of the 2D tensor elements:

print(Tens_sine)

In the below output, the computed sine values of the “T2” tensor can be seen:

We have efficiently explained the method of calculating the sine of tensor elements in PyTorch.

Note: Click on the provided link to access our Google Colab Notebook.

Conclusion

To calculate/find the sine of tensor elements in PyTorch, first, install the “torch” library. Then, define a desired tensor and view its elements. Next, use the “torch.sin()” method and pass the input tensor as a parameter to calculate the sine of its elements. Finally, display the calculated sine values of the tensor elements. This blog has illustrated the method to compute the sine of desired tensor elements in PyTorch.