<p>What you learned was the sinus function and not its derivative : during the training process, you are controlling the error with your cost function that takes into account only the values, but it does not control the slope at all : you could have learned a very noisy function but matching the data points exactly.</p>

<p>If you are just using the data point in your cost function, you have no guarantee about the derivative you've learned. However, with some advanced training technics, you could also learn such a derivative : <a href="https://arxiv.org/abs/1706.04859" rel="nofollow noreferrer">https://arxiv.org/abs/1706.04859</a></p>

<p>So as a summary, it is not a code issue but only
 a theoritical issue</p>
Derivative of neural network with respect to input
<p>I trained a neural network to do a regression on the sine function and would like to compute the first and second derivative with respect to the input.
I tried using the tf.gradients() function like this (neural_net is an instance of tf.keras.Sequential):</p>

<pre class="lang-py prettyprint-override"><code>prediction = neural_net(x_value)
dx_f = tf.gradients(prediction, x_value)
dx_dx_f = tf.gradients(dx_f, x_value)
</code></pre>

<p>x_value is an array that has the length of the test size.
However, this results in <a href="https://i.stack.imgur.com/VpqAf.jpg" rel="noreferrer">predictions and derivatives</a>. The prediction of the network (blue curve) basically exactly catches the sine function, but I had to divide the first derivative (orange) with a factor of 10 and the second derivative (green) with a factor of 100 in order for it to be in the same order of magnitude. So the, the first derivative looks (after that rescale) ok, but the seond derivative is completely erratic. Since the prediction of the sine function works really well there is clearly something funny going on here.</p>