# Tanh

```python
class Tanh(Activation):
    def __init__(self):
        super().__init__()

    def apply(self, x: np.ndarray) -> np.ndarray:
        """
        Tanh activation function.
            f(x) = (e^x - e^(-x)) / (e^x + e^(-x))

        Parameters:
        x : np.ndarray
            Numpy array containing the weighted sum of inputs.

        Returns:
        np.ndarray
            Numpy array with the tanh function applied element-wise.

        Raises:
            TypeError:
                If input is not a numpy array.
            ValueError:
                If input contains NaN or infinity values.
        """
        typecheck(x)
        self.activated = np.tanh(x)
        return self.activated

    def derivative(self, x: np.ndarray) -> np.ndarray:
        """
        Tanh derivative function.
            f'(x) = 1 - f(x)^2

        Parameters:
        x : np.ndarray
            Numpy array containing the weighted sum of inputs.

        Returns:
        np.ndarray
            Numpy array with the tanh derivative applied element-wise.
        """
        self.activated = self.apply(x)
        return 1 - self.activated ** 2
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ncxlib.gitbook.io/ncxlib/getting-started/api-documentation/overview/activation/tanh.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
