Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions content/pytorch/concepts/tensor-operations/terms/tan/tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
Title: '.tan()'
Description: 'Returns the tangent of each element in the input tensor.'
Subjects:
- 'Computer Science'
- 'Machine Learning'
Tags:
- 'Deep Learning'
- 'PyTorch'
- 'Tensors'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/machine-learning'
---

The **`.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.

## Syntax

```py
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 `.tan()` with a 1D tensor

In this example, `.tan()` computes the tangent of a 1D tensor containing angles in radians:

```py
import torch

# Create a tensor with values in radians
input_tensor = torch.tensor([0, torch.pi / 4, torch.pi / 6])

# Compute the tangent
output_tensor = torch.tan(input_tensor)

print(output_tensor)
```

The output of this code is:

```shell
tensor([0.0000, 1.0000, 0.5774])
```

## Example 2: Applying `.tan()` with a 2D tensor

In this example, `.tan()` is applied to a 2D tensor of angles in radians:

```py
import torch

# Create a 2x2 tensor with elements with values in radians
matrix = torch.tensor([[0, torch.pi / 4], [torch.pi, torch.pi / 6]])

# Compute the tangent
result = torch.tan(matrix)

print(result)
```

The output of this code is:

```shell
tensor([[0.0000, 1.0000],
[0.0000, 0.5774]])
```