👾 Add some machine learning experiments

This commit is contained in:
Mia Steinkirch 2019-10-27 15:05:11 -07:00
parent cab89257dd
commit 729da96322
60 changed files with 39649 additions and 0 deletions

View file

@ -0,0 +1,31 @@
# compute the gradient numerically:
# a generic function takes a function f, a vector x o evaluate
# the gradient on, and returns the gradient of f at x:
def eval_numerical_gradient(f, x):
"""
a naive implementation of numerical gradient of f at x
- f should be a function that takes a single argument
- x is the point (numpy array) to evaluate the gradient at
"""
fx = f(x) # evaluate function value at original point
grad = np.zeros(x.shape)
h = 0.00001
# iterate over all indexes in x
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
# evaluate function at x+h
ix = it.multi_index
old_value = x[ix]
x[ix] = old_value + h # increment by h
fxh = f(x) # evalute f(x + h)
x[ix] = old_value # restore to previous value (very important!)
# compute the partial derivative
grad[ix] = (fxh - fx) / h # the slope
it.iternext() # step to next dimension
return grad