PyTorch .tan()
Published Feb 20, 2026
The torch.tan() function returns the tangent of each element in the input tensor. It is part of PyTorch’s math operations used in deep learning and scientific computing.
Syntax
torch.tan(input, *, out=None) → Tensor
Parameters:
input(Tensor): Input tensor with one or more elements in radians.out(Tensor, optional): Optional tensor to store the output.
Return value:
A tensor containing the tangent of each input element, with the same shape as the input tensor.
Example 1: Using torch.tan() with a 1D tensor
In this example, torch.tan() computes the tangent of a 1D tensor containing angles in radians:
import torch# Create a tensor with values in radiansinput_tensor = torch.tensor([0, torch.pi / 4, torch.pi / 6])# Compute the tangentoutput_tensor = torch.tan(input_tensor)print(output_tensor)
The output of this code is:
tensor([0.0000, 1.0000, 0.5774])
Example 2: Applying torch.tan() with a 2D tensor
In this example, torch.tan() is applied to a 2D tensor of angles in radians:
import torch# Create a 2x2 tensor with elements with values in radiansmatrix = torch.tensor([[0, torch.pi / 4], [torch.pi, torch.pi / 6]])# Compute the tangentresult = torch.tan(matrix)print(result)
The output of this code is:
tensor([[0.0000e+00, 1.0000e+00],[8.7423e-08, 5.7735e-01]])
Learn PyTorch on Codecademy
- Learn to build machine learning models with Python.
- Includes 10 Courses
- With Certificate
- Beginner Friendly.23 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours