Skip to content

Commit 4e7cf44

Browse files
numerical grad check added
1 parent 18d4a67 commit 4e7cf44

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

helper_functions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,30 @@ def loss(target, nn_out):
8383
def softmax(x):
8484
expA = np.exp(x - np.max(x))
8585
return expA / expA.sum(axis=0)
86+
87+
88+
def numericalgradient(fun,w,e):
89+
"""
90+
Provides a numerical estimate of the gradient of fun w.r.t. to parameters w
91+
:param e: epsilon
92+
:return: numerical gradient estimate (shape of w)
93+
"""
94+
# get dimensionality
95+
d = len(w)
96+
# initialize numerical derivative
97+
dh = np.zeros(d)
98+
# go through dimensions
99+
for i in range(d):
100+
# copy the weight vector
101+
nw = w.copy()
102+
# perturb dimension i
103+
nw[i] += e
104+
# compute loss
105+
l1, temp = fun(nw)
106+
# perturb dimension i again
107+
nw[i] -= 2*e
108+
# compute loss
109+
l2, temp = fun(nw)
110+
# the gradient is the slope of the loss
111+
dh[i] = (l1 - l2) / (2*e)
112+
return dh

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy