extra_function

extra_function(f0=None, using_numpy=True)[source]

Using extra function with numerical differentiation.

It can be used for numpy function or numba.vectorize function interface.

>>> import numpy as np
>>> sin2 = extra_function(np.sin)
>>> a = tf.Variable([1.0,2.0], dtype="float64")
>>> with tf.GradientTape(persistent=True) as tape0:
...     with tf.GradientTape(persistent=True) as tape:
...         b = sin2(a)
...     g, = tape.gradient(b, [a,])
...
>>> h, = tape0.gradient(g, [a,])
>>> assert np.allclose(np.sin([1.0,2.0]), b.numpy())
>>> assert np.allclose(np.cos([1.0,2.0]), g.numpy())
>>> assert np.sum(np.abs(-np.sin([1.0,2.0]) - h.numpy())) < 1e-3

The numerical accuracy is not so well for second derivative.