Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to code this neural net activation function, using the C language:

$$f(x) = 1.7159 \tanh( \frac{2}{3} x )$$

and I will also need to code its derivative.

I've read that the derivative of $\tanh(x)$ is $\operatorname{sech}^2(x)$, but since C doesn't have a hyperbolic secant function I will need to use $\cosh$, i.e. the derivative of $\tanh(x)$ is $1\over \cosh^2(x)$, I think.

Since my knowledge of calculus is very rusty, my best attempt for the derivative of the above function is:

$$1 \over \cosh^2(\frac{2}{3}x)$$

Is this correct?

share|cite|improve this question
up vote 4 down vote accepted

The derivative is $\frac{2*1.7159}{3} / \cosh^2 \frac{2x}{3}$ by chain rule. Note that it can be also written as $\frac{2*1.7159}{3} (1- \tanh^2 \frac{2x}{3})$ which might make slightly more sense as you only need to find $\tanh$ then (if you want to find $f(x)$ and $f'(x)$ at the same time). All the useful relations can be found in wikipedia.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.