-
Notifications
You must be signed in to change notification settings - Fork 50
/
numerical_gradient.py
35 lines (26 loc) · 962 Bytes
/
numerical_gradient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from functools import partial
import numpy as np
def compute(theta, J, epsilon=0.0001):
"""Accepts an array, and a function.
Also optionally accepts an epsilon defining
neighborhood step size to check.
Returns an array of partial derivatives.
The partial derivatives are numerically computed
by looking at the neighborhood around theta numerically.
"""
assert theta.ndim == 1
size = len(theta)
grad = np.zeros(size)
def offset(size, epsilon, i):
"""Accepts size int, epsilon real, and integer i.
Returns new array of that size with zeros in almost all positions,
but epsilon in the ith position.
"""
y = np.zeros(size)
y[i] += epsilon
return y
o = partial(offset, size, epsilon)
for i in xrange(size):
e = o(i)
grad[i] = (J(theta + e) - J(theta - e)) / (2 * epsilon)
return grad