[CppAD] Automatic differentiation of tanh fails for some values
Brad Bell
bradbell at seanet.com
Tue Jul 26 07:48:06 EDT 2011
Looking at the heading "tan" on
http://www.coin-or.org/CppAD/Doc/std_math_ad.xml#Derivatives.tanh
you will see that tanh is currently implemented as
sinh(x) / cosh(x)
which yields inf / inf for large values of x.
Looking at the heading "Tan and Tanh" in the CppAD wish list
http://www.coin-or.org/CppAD/Doc/wishlist.xml#Tan%20and%20Tanh
you will see an item that would fix this problem.
I have added a user atomic example that tests this solution; see
http://www.coin-or.org/CppAD/Doc/user_tan.cpp.xml
In particular, see the following section of its correctness test:
// check tanh results for a large value of x
CPPAD_TEST_VECTOR<float> x(n), f(m);
x[0] = std::numeric_limits<float>::max() / two;
f = F.Forward(0, x);
tanh = 1.;
ok &= NearEqual(f[1], tanh, eps, eps);
df = F.Forward(1, dx);
tanhp = 0.;
ok &= NearEqual(df[1], tanhp, eps, eps);
The next and final step in this effort will be to complete the wish list
item using the algorithms in user_tan.cpp.
P.S.
I tried the solution
tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)) = 1 - 2 / (1 +
exp(2x))
which works for function values, but when evaluating derivatives one gets
4 * exp(2x) / ( 1 + exp(2x) )^2
which yields inf / inf = nan for large values of x.
On 7/12/2011 10:37 AM, Srikrishnan Ramadurai wrote:
> Hi,
>
> I am trying to find the jacobian for a function that has a few tanh
> functions in it . I've noticed that the differentiation of tanh function
> fails for some arguments.
>
> A test example is y=tanh(c*x1)+x2
>
> The Jacobian of y is obtained when c is small say 20, but fails with an
> error when c is something like 100. This is using the driver function
> Jacobian.
>
> This is the error that I receive.
>
> cppad-20110711 error from a known source:
> y_p = f.Forward(p, x_p): has a nan in y_p for p = 0.
> Error detected by false result for
> false
>
> Is there something that I need to do to make the differentiation work? Any
> help is appreciated.
>
> The sample code in c++ is attached. The system is a intel core 2 quad
> machine running on 64 bit ubuntu 10.04.
>
> Thanks.
>
> Regards
> Srikrishnan
>
>
>
> _______________________________________________
> CppAD mailing list
> CppAD at list.coin-or.org
> http://list.coin-or.org/mailman/listinfo/cppad
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://list.coin-or.org/pipermail/cppad/attachments/20110726/b02442f5/attachment.html
More information about the CppAD
mailing list