
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to compute the sine of elements of a tensor in PyTorch?
To compute the sine of elements of a tensor, we use the torch.sin() method. It returns a new tensor with the sine values of the elements of the original input tensor. It takes a tensor as the input parameter and outputs a tensor.
Steps
Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.
Create a tensor and print it.
Compute torch.sin(input). It takes input, a tensor as input parameter, and returns a new tensor with the sine values of elements of the input.
Print the tensor with the sine values of elements of the original input tensor.
Example 1
# Python program to compute sine of the elements of a tensor # import necessary library import torch # create a tensor T = torch.Tensor([1.3,4.32,4.4,5.3,4.5]) print("Original Tensor T:\n", T) # Compute the sine of above tensor sine_T = torch.sin(T) print("Sine value of elements of tensor T:\n", sine_T)
Output
Original Tensor T: tensor([1.3000, 4.3200, 4.4000, 5.3000, 4.5000]) Sine value of elements of tensor T: tensor([ 0.9636, -0.9240, -0.9516, -0.8323, -0.9775])
Example 2
# Python program to compute sine of the elements of a tensor # import necessary library import torch # Create a 2D tensor of size 3x5 T = torch.Tensor([[1.3,4.32,4.4,5.3,4.5], [0.2,0.3,0.5,0.7,0.9], [1.1,1.2,2.3,3.1,4.9]]) print("Original Tensor T:\n", T) # Compute the sine of above tensor sine_T = torch.sin(T) print("Sine value of elements of tensor T:\n", sine_T)
Output
Original Tensor T: tensor([[1.3000, 4.3200, 4.4000, 5.3000, 4.5000], [0.2000, 0.3000, 0.5000, 0.7000, 0.9000], [1.1000, 1.2000, 2.3000, 3.1000, 4.9000]]) Sine value of elements of tensor T: tensor([[ 0.9636, -0.9240, -0.9516, -0.8323, -0.9775], [ 0.1987, 0.2955, 0.4794, 0.6442, 0.7833], [ 0.8912, 0.9320, 0.7457, 0.0416, -0.9825]])
- Related Articles
- How to compute the Logarithm of elements of a tensor in PyTorch?
- PyTorch – How to compute the logistic sigmoid function of tensor elements?
- How to compute the histogram of a tensor in PyTorch?
- PyTorch – How to compute the error function of a tensor?
- How to sort the elements of a tensor in PyTorch?
- How to compute the inverse hyperbolic sine in PyTorch?
- How to compute the mean and standard deviation of a tensor in PyTorch?
- PyTorch – How to get the exponents of tensor elements?
- PyTorch – How to compute element-wise entropy of an input tensor?
- How to compute the element-wise angle of the given input tensor in PyTorch?
- How to access the metadata of a tensor in PyTorch?
- How to find the transpose of a tensor in PyTorch?
- How to find the k-th and the top "k" elements of a tensor in PyTorch?
- Compute the Hyperbolic sine of the array elements in Python
- Compute the inverse Hyperbolic sine of array elements in Python

Advertisements