cs231赋值1Q4: Two-Layer Neural Network (25 points)中的梯度检查逻辑似乎有错误
它正在做梯度检查
two_layer_net.ipynb使用参数W
定义lambda,但未使用
from cs231n.gradient_check import eval_numerical_gradient
loss, grads = net.loss(X, y, reg=0.05)
for param_name in grads:
f = lambda W: net.loss(X, y, reg=0.05)[0] # <--- W is not used anywhere
# f = lambda : net.loss(X, y, reg=0.05)[0] # <--- Should be like this
param_grad_num = \
eval_numerical_gradient(f, # <--- lambda passed as f
net.params[param_name], verbose=False)
cs231n.gradient\u check.eval\u numerical\u gradient.py作为f(x)
调用,但x
将不使用
def eval_numerical_gradient(f, x, verbose=True, h=0.00001):
"""
a naive implementation of numerical gradient of f at x
- f should be a function that takes a single argument # <--- Should have no need to take an argument
- x is the point (numpy array) to evaluate the gradient at
"""
grad = np.zeros_like(x)
it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
while not it.finished:
# evaluate function at x+h
ix = it.multi_index
oldval = x[ix]
x[ix] = oldval + h # increment by h
fxph = f(x) # evalute f(x + h) # <--- x will not be used
# fxph = f() # <--- Should be like this
x[ix] = oldval - h
fxmh = f(x) # evaluate f(x - h)
x[ix] = oldval # restore
# compute the partial derivative with centered formula
grad[ix] = (fxph - fxmh) / (2 * h) # the slope
it.iternext() # step to next dimension
return grad
我相信它正好起作用,因为np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
直接更新了net.params[param_name]
请确认此理解是否正确
目前没有回答
相关问题 更多 >
编程相关推荐