根据定义计算梯度

下面的代码会依次x的每个维度根据定义进行计算。
ix :(0,0,0...) (0,0,0...1)....
下面的代码写法可以兼容x为任意维数的情况。

# 参数df表示 cost函数对df求导
def eval_numerical_gradient_array(f, x, df, h=1e-5):
    """
    Evaluate a numeric gradient for a function that accepts a numpy
    array and returns a numpy array.
    """
    grad = np.zeros_like(x)
    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        ix = it.multi_index

        oldval = x[ix]
        x[ix] = oldval + h
        pos = f(x).copy()
        x[ix] = oldval - h
        neg = f(x).copy()
        x[ix] = oldval

        grad[ix] = np.sum((pos - neg) * df) / (2 * h)
        it.iternext()
    return grad

你可能感兴趣的:(根据定义计算梯度)