Pytorch Torch Arccosh
## PyTorch torch.arccosh
`torch.arccosh` is a PyTorch function used to compute the element-wise inverse hyperbolic cosine (also known as $\text{arcosh}$ or $\cosh^{-1}$) of an input tensor.
This function is an alias for `torch.acosh`. Both functions yield identical results and can be used interchangeably.
---
### Mathematical Definition
For a given input $x$, the inverse hyperbolic cosine is defined mathematically as:
$$\text{arccosh}(x) = \ln\left(x + \sqrt{x^2 - 1}\right)$$
* **Domain:** $[1, \infty)$
* **Codomain:** $[0, \infty)$
> **Note:** Since the domain of the inverse hyperbolic cosine function is restricted to real numbers greater than or equal to 1, passing values less than 1 will result in `NaN` (Not a Number) for real-valued tensors, or complex numbers if the input tensor is complex.
---
### Syntax
```python
torch.arccosh(input, *, out=None) -> Tensor
```
#### Parameters
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `input` | *Tensor* | The input tensor containing values $\ge 1$. |
| `out` | *Tensor (Optional)* | The output tensor. Defaults to `None`. |
#### Returns
* **Tensor**: A tensor of the same shape and data type as the `input` containing the element-wise inverse hyperbolic cosine values.
---
### Code Examples
#### Example 1: Basic Usage with Valid Inputs (Real Numbers $\ge 1$)
```python
import torch
# Create a tensor with values greater than or equal to 1
x = torch.tensor([1.0, 1.5, 2.0, 3.0])
# Compute the inverse hyperbolic cosine
result = torch.arccosh(x)
print("Input Tensor:")
print(x)
print("\nResult Tensor:")
print(result)
```
**Output:**
```text
Input Tensor:
tensor([1.0000, 1.5000, 2.0000, 3.0000])
Result Tensor:
tensor([0.0000, 0.9624, 1.3170, 1.7627])
```
---
#### Example 2: Handling Out-of-Domain Inputs (Values $< 1$)
If you pass values strictly less than 1, PyTorch will return `nan` for those elements.
```python
import torch
# Create a tensor containing values less than 1
x = torch.tensor([0.5, 1.0, 2.0])
# Compute arccosh
result = torch.arccosh(x)
print(result)
```
**Output:**
```text
tensor([ nan, 0.0000, 1.3170])
```
---
#### Example 3: In-Place Operation
If you want to perform the operation in-place to save memory, you can use `torch.arccosh_` (with a trailing underscore) or use the `out` parameter.
```python
import torch
x = torch.tensor([1.0, 2.0, 3.0])
# In-place modification
x.arccosh_()
print(x)
```
**Output:**
```text
tensor([0.0000, 1.3170, 1.7627])
```
---
### Considerations & Best Practices
1. **Alias Equivalence**: `torch.arccosh` and `torch.acosh` are completely identical in behavior and performance. You can use either depending on your naming preference.
2. **Domain Constraints**: Always ensure your input values are $\ge 1.0$ when working with real-valued tensors to avoid unexpected `nan` values in your pipeline. If your model's forward pass can produce values $< 1$, consider using `torch.clamp(input, min=1.0)` before applying `torch.arccosh`.
3. **Complex Number Support**: If the input tensor is complex, `torch.arccosh` will compute the complex inverse hyperbolic cosine, which extends the domain to all complex numbers.
YouTip