본문 바로가기
Data Science/머신러닝&딥러닝 기초 이론

[딥러닝 실습] numerical_gradient 경사하강법

by titaniumm 2020. 4. 24.
def numerical_gradient(f,x):
    h = 1e-4
    grad = np.zeros_like(x)
    
    it = np.nditer(x, flags = ['multi_index'],op_flag= ['readwrite'])
    while not it.finished:
        idx = it.multi_index
        tem_val = x[idx]
        x[idx] = float(tmp_val)+h
        fxh1 = f(x)
        
        x[idx] = tem_val -h
        fxh2 = f(x)
        grad[idx] (fxh1 = fxh2) / (2*h)
        
        x[idx] = tmp_val
        it.iternext()
    return grad

댓글