When differentiating by programming, the central difference is preferable to the forward difference. The reason is that the central difference has less error.
import numpy as np
#Forward difference
def numerical_diff_forward(f,x):
h = 1e-4
return (f(x+h) - f(x)) / h
#Central difference
def numerical_diff_center(f,x):
h = 1e-4
return (f(x+h) - f(x-h)) / (h*2)
np.float32(1e-3)
def function(x):
return 0.01*x**2+0.1*x
print(abs(0.2 - numerical_diff_forward(function,5)))
print(abs(0.2 - numerical_diff_center(function,5)))
#9.999991725240243e-07
#9.102163467389346e-13
reference [Deep Learning from scratch-the theory and implementation of deep learning learned in Python]
Recommended Posts