def f(x,y): # We can call other user defined functions z = pow(x,5) # and return more than one parameter return z + x*math.sqrt(y), x+y
x,y = 3,6 print "value: ", f(x,y) der = adolpy.derivate(f) derivative = der(x,y) print print "Automatic derivatives" for i in derivative: print i.dot print "Manually computed jacobian" print [5 * x**4 + math.sqrt(y), x/(2*math.sqrt(y))] print [1,1] step = 1e-5 print "Numerically computed by central differences for step=", step print [(f(x+step,y)[0] - f(x-step,y)[0])/(2*step), (f(x,y+step)[0] - f(x, y-step)[0])/(2*step)] print [(f(x+step,y)[1] - f(x-step,y)[1])/(2*step), (f(x,y+step)[1] - f(x, y-step)[1])/(2*step)] print print "Now, a function defined by parts (huber loss), delta = 3" print "For a < delta, huber(a) = .5 * a**2" print "For a = delta, huber(a) = .5 * a**2 == delta * (abs(a) - delta/2)" print "For a > delta, huber(a) = delta * (abs(a) - delta/2)"