Skip to content
Open
Show file tree
Hide file tree
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
118 changes: 118 additions & 0 deletions content/pytorch/concepts/tensor-operations/terms/sinh/sinh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
Title: '.sinh()'
Description: 'Computes the hyperbolic sine of each element in a PyTorch tensor.'
Subjects:
- 'Machine Learning'
- 'Data Science'
Tags:
- 'PyTorch'
- 'Deep Learning'
- 'Machine Learning'
- 'Tensors'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
---

The **`.sinh()`** function in PyTorch computes the hyperbolic sine of each element in the input tensor. This operation applies the mathematical function $\sinh(x) = \frac{e^x - e^{-x}}{2}$ element-wise to all values in the tensor.

The hyperbolic sine function is commonly used in neural network activation functions, numerical methods, and scientific computing applications involving hyperbolic trigonometric transformations.

## Syntax

```pseudo
torch.sinh(input, *, out=None) → Tensor
```

**Parameters:**

- `input`: The input tensor containing elements for which the hyperbolic sine will be computed.
- `out` (optional): A tensor to store the output. If provided, the result is written to this tensor.

**Return value:**

Returns a new tensor where each element is the hyperbolic sine of the corresponding element in the input tensor.

## Example 1: Single Element Tensor

This example demonstrates computing the hyperbolic sine of a single-element tensor:

```py
import torch

# Create a single-element tensor
x = torch.tensor([2.0])

# Compute the hyperbolic sine
result = torch.sinh(x)

print("Input:", x)
print("sinh(2.0):", result)
```

This produces the following output:

```
Input: tensor([2.])
sinh(2.0): tensor([3.6269])
```

## Example 2: 1D Array

This example shows how to compute the hyperbolic sine of a 1D tensor:

```py
import torch

# Create a 1D tensor with various values
x = torch.tensor([0.0, 1.0, -1.0, 2.0, -2.0])

# Compute the hyperbolic sine
result = torch.sinh(x)

print("Input tensor:", x)
print("Hyperbolic sine:", result)
```

This produces the following output:

```
Input tensor: tensor([ 0., 1., -1., 2., -2.])
Hyperbolic sine: tensor([ 0.0000, 1.1752, -1.1752, 3.6269, -3.6269])
```

Note that the hyperbolic sine function is antisymmetric, meaning $\sinh(-x) = -\sinh(x)$, which is why `sinh(1.0)` and `sinh(-1.0)` have opposite signs but equal magnitudes.

## Example 3: Multi-Dimensional Array

This example demonstrates computing the hyperbolic sine of a 2D tensor:

```py
import torch

# Create a 2D tensor (2x3 matrix)
x = torch.tensor([[0.0, 0.5, 1.0],
[1.5, 2.0, 2.5]])

# Compute the hyperbolic sine
result = torch.sinh(x)

print("Input tensor:")
print(x)
print("\nHyperbolic sine:")
print(result)
```

This produces the following output:

```
Input tensor:
tensor([[0.0000, 0.5000, 1.0000],
[1.5000, 2.0000, 2.5000]])

Hyperbolic sine:
tensor([[ 0.0000, 0.5211, 1.1752],
[ 2.1293, 3.6269, 6.0502]])
```

The `.sinh()` function preserves the shape of the input tensor, applying the hyperbolic sine operation element-wise to each value in the multi-dimensional array.
84 changes: 84 additions & 0 deletions content/pytorch/concepts/tensor-operations/terms/sqrt/sqrt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
Title: '.sqrt()'
Description: 'Computes the square root of each element in a PyTorch tensor.'
Subjects:
- 'Machine Learning'
- 'Data Science'
Tags:
- 'PyTorch'
- 'Deep Learning'
- 'Machine Learning'
- 'Tensors'
CatalogContent:
- 'intro-to-py-torch-and-neural-networks'
- 'paths/computer-science'
---

The **`.sqrt()`** function in PyTorch computes the square root of each element in the input tensor. This operation applies the mathematical function $\sqrt{x}$ element-wise to all values in the tensor.

The square root function is commonly used in neural networks for normalization techniques, distance calculations (such as Euclidean distance), standard deviation computations, and various mathematical transformations in deep learning applications.

## Syntax

```pseudo
torch.sqrt(input, *, out=None) → Tensor
```

**Parameters:**

- `input`: The input tensor containing non-negative elements for which the square root will be computed.
- `out` (optional): A tensor to store the output. If provided, the result is written to this tensor.

**Return value:**

Returns a new tensor where each element is the square root of the corresponding element in the input tensor.

## Example 1: Single Element Tensor

This example demonstrates computing the square root of a single-element tensor:

```py
import torch

# Create a single-element tensor
x = torch.tensor([16.0])

# Compute the square root
result = torch.sqrt(x)

print("Input:", x)
print("sqrt(16.0):", result)
```

This produces the following output:

```
Input: tensor([16.])
sqrt(16.0): tensor([4.])
```

## Example 2: 1D Array

This example shows how to compute the square root of a 1D tensor:

```py
import torch

# Create a 1D tensor with various values
x = torch.tensor([0.0, 1.0, 4.0, 9.0, 16.0, 25.0])

# Compute the square root
result = torch.sqrt(x)

print("Input tensor:", x)
print("Square root:", result)
```

This produces the following output:

```
Input tensor: tensor([ 0., 1., 4., 9., 16., 25.])
Square root: tensor([0., 1., 2., 3., 4., 5.])
```

Note that $\sqrt{0} = 0$ and the square root function is only defined for non-negative real numbers. Attempting to compute the square root of negative numbers will result in `nan` (not a number) values.