-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNumericalGradient.py
More file actions
36 lines (33 loc) · 1.22 KB
/
NumericalGradient.py
File metadata and controls
36 lines (33 loc) · 1.22 KB
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
35
36
import numpy as np
def numerical_gradient(f, x, accuracy=1e-5):
grads = np.zeros(x.shape)
iterator = np.nditer(x, flags = ['multi_index'], op_flags = ['readwrite'])
while not iterator.finished:
index = iterator.multi_index
original = x[index]
x[index] += accuracy
upper_bound = f(x)
x[index] = original
x[index] -= accuracy
lower_bound = f(x)
x[index] = original
grads[index] = (upper_bound - lower_bound) / (2*accuracy)
iterator.iternext()
return grads
def numerical_gradient_layer(f, x, dout, accuracy=1e-5):
grads = np.zeros(x.shape)
iterator = np.nditer(x, flags = ['multi_index'], op_flags = ['readwrite'])
while not iterator.finished:
index = iterator.multi_index
original = x[index]
x[index] += accuracy
upper_bound = f(x).copy()
x[index] = original
x[index] -= accuracy
lower_bound = f(x).copy()
x[index] = original
grads[index] = np.sum((upper_bound - lower_bound) * dout) / (2*accuracy)
iterator.iternext()
return grads
def relative_error(result, expected):
return np.abs(result - expected) / np.maximum(np.abs(result) + np.abs(expected), 1e-8)