Skip to content
This repository was archived by the owner on Feb 27, 2026. It is now read-only.

Commit c9b93f0

Browse files
authored
New Entry - PyTorch - Tensor Operations - Tan (#8186)
* Create file and write content * Update documentation to use torch.tan() instead of .tan() ---------
1 parent b2241a8 commit c9b93f0

File tree

1 file changed

+76
-0
lines changed
  • content/pytorch/concepts/tensor-operations/terms/tan

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
Title: '.tan()'
3+
Description: 'Returns the tangent of each element in the input tensor.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Machine Learning'
7+
Tags:
8+
- 'Deep Learning'
9+
- 'PyTorch'
10+
- 'Tensors'
11+
CatalogContent:
12+
- 'intro-to-py-torch-and-neural-networks'
13+
- 'paths/machine-learning'
14+
---
15+
16+
The **`torch.tan()`** function returns the tangent of each element in the input [tensor](https://www.codecademy.com/resources/docs/pytorch/tensors). It is part of PyTorch’s math operations used in deep learning and scientific computing.
17+
18+
## Syntax
19+
20+
```pseudo
21+
torch.tan(input, *, out=None) → Tensor
22+
```
23+
24+
**Parameters:**
25+
26+
- `input` (Tensor): Input tensor with one or more elements in radians.
27+
- `out` (Tensor, optional): Optional tensor to store the output.
28+
29+
**Return value:**
30+
31+
A tensor containing the tangent of each input element, with the same shape as the input tensor.
32+
33+
## Example 1: Using `torch.tan()` with a 1D tensor
34+
35+
In this example, `torch.tan()` computes the tangent of a 1D tensor containing angles in radians:
36+
37+
```py
38+
import torch
39+
40+
# Create a tensor with values in radians
41+
input_tensor = torch.tensor([0, torch.pi / 4, torch.pi / 6])
42+
43+
# Compute the tangent
44+
output_tensor = torch.tan(input_tensor)
45+
46+
print(output_tensor)
47+
```
48+
49+
The output of this code is:
50+
51+
```shell
52+
tensor([0.0000, 1.0000, 0.5774])
53+
```
54+
55+
## Example 2: Applying `torch.tan()` with a 2D tensor
56+
57+
In this example, `torch.tan()` is applied to a 2D tensor of angles in radians:
58+
59+
```py
60+
import torch
61+
62+
# Create a 2x2 tensor with elements with values in radians
63+
matrix = torch.tensor([[0, torch.pi / 4], [torch.pi, torch.pi / 6]])
64+
65+
# Compute the tangent
66+
result = torch.tan(matrix)
67+
68+
print(result)
69+
```
70+
71+
The output of this code is:
72+
73+
```shell
74+
tensor([[0.0000e+00, 1.0000e+00],
75+
[8.7423e-08, 5.7735e-01]])
76+
```

0 commit comments

Comments
 (0)