当前位置: 首页>>代码示例>>Python>>正文


Python numdifftools.Gradient方法代码示例

本文整理汇总了Python中numdifftools.Gradient方法的典型用法代码示例。如果您正苦于以下问题:Python numdifftools.Gradient方法的具体用法?Python numdifftools.Gradient怎么用?Python numdifftools.Gradient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在numdifftools的用法示例。


在下文中一共展示了numdifftools.Gradient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_dl_dG

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dG():
    nz, neq, nineq = 10, 0, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq)

    def f(G):
        G = G.reshape(nineq, nz)
        _, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
        return 0.5 * np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dG_fd = df(G.ravel()).reshape(nineq, nz)
    if verbose:
        # print('dG_fd[1,:]: ', dG_fd[1,:])
        # print('dG[1,:]: ', dG[1,:])
        print('dG_fd: ', dG_fd)
        print('dG: ', dG)
    npt.assert_allclose(dG_fd, dG, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:qpth,代码行数:20,代码来源:test.py

示例2: test_dl_dA

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dA():
    nz, neq, nineq = 10, 3, 1
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(A):
        A = A.reshape(neq, nz)
        _, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
        return 0.5 * np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dA_fd = df(A.ravel()).reshape(neq, nz)
    if verbose:
        # print('dA_fd[0,:]: ', dA_fd[0,:])
        # print('dA[0,:]: ', dA[0,:])
        print('dA_fd: ', dA_fd)
        print('dA: ', dA)
    npt.assert_allclose(dA_fd, dA, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:qpth,代码行数:20,代码来源:test.py

示例3: check_gradient

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def check_gradient(f, x):
    print(x, "\n", f(x))

    print("# grad2")
    grad2 = Gradient(f)(x)
    print("# building grad1")
    g = grad(f)
    print("# computing grad1")
    grad1 = g(x)

    print("gradient1\n", grad1, "\ngradient2\n", grad2)
    np.allclose(grad1, grad2)

    # check Hessian vector product
    y = np.random.normal(size=x.shape)
    gdot = lambda u: np.dot(g(u), y)
    hess1, hess2 = grad(gdot)(x), Gradient(gdot)(x)
    print("hess1\n", hess1, "\nhess2\n", hess2)
    np.allclose(hess1, hess2) 
开发者ID:popgenmethods,项目名称:momi2,代码行数:21,代码来源:test_autograd.py

示例4: approx_fprime_cs

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def approx_fprime_cs(x, f, epsilon=None, args=(), kwargs={}):
    '''
    Calculate gradient or Jacobian with complex step derivative approximation

    Parameters
    ----------
    x : array
        parameters at which the derivative is evaluated
    f : function
        `f(*((x,)+args), **kwargs)` returning either one value or 1d array
    epsilon : float, optional
        Stepsize, if None, optimal stepsize is used. Optimal step-size is
        EPS*x. See note.
    args : tuple
        Tuple of additional arguments for function `f`.
    kwargs : dict
        Dictionary of additional keyword arguments for function `f`.

    Returns
    -------
    partials : ndarray
       array of partial derivatives, Gradient or Jacobian

    Notes
    -----
    The complex-step derivative has truncation error O(epsilon**2), so
    truncation error can be eliminated by choosing epsilon to be very small.
    The complex-step derivative avoids the problem of round-off error with
    small epsilon because there is no subtraction.
    '''
    # From Guilherme P. de Freitas, numpy mailing list
    # May 04 2010 thread "Improvement of performance"
    # http://mail.scipy.org/pipermail/numpy-discussion/2010-May/050250.html
    n = len(x)
    epsilon = _get_epsilon(x, 1, epsilon, n)
    increments = np.identity(n) * 1j * epsilon
    # TODO: see if this can be vectorized, but usually dim is small
    partials = [f(x+ih, *args, **kwargs).imag / epsilon[i]
                for i, ih in enumerate(increments)]
    return np.array(partials).T 
开发者ID:birforce,项目名称:vnpy_crypto,代码行数:42,代码来源:numdiff.py

示例5: test_dl_dp

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dp():
    nz, neq, nineq = 10, 2, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(p):
        _, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
        return 0.5 * np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dp_fd = df(p)
    if verbose:
        print('dp_fd: ', dp_fd)
        print('dp: ', dp)
    npt.assert_allclose(dp_fd, dp, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:qpth,代码行数:17,代码来源:test.py

示例6: test_dl_dh

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dh():
    nz, neq, nineq = 10, 0, 3
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=1., Gscale=1.)

    def f(h):
        _, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
        return 0.5 * np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dh_fd = df(h)
    if verbose:
        print('dh_fd: ', dh_fd)
        print('dh: ', dh)
    npt.assert_allclose(dh_fd, dh, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:qpth,代码行数:17,代码来源:test.py

示例7: test_dl_db

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_db():
    nz, neq, nineq = 10, 3, 1
    [p, Q, G, h, A, b, truez], [dQ, dp, dG, dh, dA, db] = get_grads(
        nz=nz, neq=neq, nineq=nineq, Qscale=100., Gscale=100., Ascale=100.)

    def f(b):
        _, zhat, nu, lam, slacks = qp_cvxpy.forward_single_np(Q, p, G, h, A, b)
        return 0.5 * np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    db_fd = df(b)
    if verbose:
        print('db_fd: ', db_fd)
        print('db: ', db)
    npt.assert_allclose(db_fd, db, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:qpth,代码行数:17,代码来源:test.py

示例8: example_function_gradient_fixtures

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def example_function_gradient_fixtures():
    def f(x):
        """f:R^3 -> R"""
        x1, x2, x3 = x[0], x[1], x[2]
        y1 = np.sin(x1) + np.cos(x2) + x3 - x3
        return y1

    def fprime(x):
        """Gradient(f)(x):R^3 -> R^3"""
        x1, x2, x3 = x[0], x[1], x[2]
        grad = np.array([np.cos(x1), -np.sin(x2), x3 - x3])
        return grad

    return {"func": f, "func_prime": fprime} 
开发者ID:OpenSourceEconomics,项目名称:estimagic,代码行数:16,代码来源:test_numdiff_np.py

示例9: test_first_derivative_gradient_richardson

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_first_derivative_gradient_richardson(example_function_gradient_fixtures):
    f = example_function_gradient_fixtures["func"]
    fprime = example_function_gradient_fixtures["func_prime"]

    true_grad = fprime(np.ones(3))
    numdifftools_grad = Gradient(f, order=2, n=3, method="central")(np.ones(3))
    grad = first_derivative(f, np.ones(3), n_steps=3, method="central")

    aaae(numdifftools_grad, grad)
    aaae(true_grad, grad) 
开发者ID:OpenSourceEconomics,项目名称:estimagic,代码行数:12,代码来源:test_numdiff_np.py

示例10: test_dl_dz0

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dz0():
    def f(z0):
        zhat, nu, lam = af.forward_single_np(p, L, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dz0_fd = df(z0)
    if verbose:
        print('dz0_fd: ', dz0_fd)
        print('dz0: ', dz0)
    npt.assert_allclose(dz0_fd, dz0, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:13,代码来源:optnet-np.py

示例11: test_dl_ds0

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_ds0():
    def f(s0):
        zhat, nu, lam = af.forward_single_np(p, L, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    ds0_fd = df(s0)
    if verbose:
        print('ds0_fd: ', ds0_fd)
        print('ds0: ', ds0)
    npt.assert_allclose(ds0_fd, ds0, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:13,代码来源:optnet-np.py

示例12: test_dl_dp

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dp():
    def f(p):
        zhat, nu, lam = af.forward_single_np(p, L, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dp_fd = df(p)
    if verbose:
        print('dp_fd: ', dp_fd)
        print('dp: ', dp)
    npt.assert_allclose(dp_fd, dp, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:13,代码来源:optnet-np.py

示例13: test_dl_dp_batch

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dp_batch():
    def f(p):
        zhat, nu, lam = af.forward_single_np(p, L, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dp_fd = df(p)
    if verbose:
        print('dp_fd: ', dp_fd)
        print('dp: ', dp)
    npt.assert_allclose(dp_fd, dp, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:13,代码来源:optnet-np.py

示例14: test_dl_dA

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dA():
    def f(A):
        A = A.reshape(neq,nz)
        zhat, nu, lam = af.forward_single_np(p, L, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dA_fd = df(A.ravel()).reshape(neq, nz)
    if verbose:
        print('dA_fd[1,:]: ', dA_fd[1,:])
        print('dA[1,:]: ', dA[1,:])
    npt.assert_allclose(dA_fd, dA, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:14,代码来源:optnet-np.py

示例15: test_dl_dL

# 需要导入模块: import numdifftools [as 别名]
# 或者: from numdifftools import Gradient [as 别名]
def test_dl_dL():
    def f(l0):
        L_ = np.copy(L)
        L_[:,0] = l0
        zhat, nu, lam = af.forward_single_np(p, L_, G, A, z0, s0)
        return 0.5*np.sum(np.square(zhat - truez))

    df = nd.Gradient(f)
    dL_fd = df(L[:,0])
    dl0 = np.array(dL[:,0]).ravel()
    if verbose:
        print('dL_fd: ', dL_fd)
        print('dL: ', dl0)
    npt.assert_allclose(dL_fd, dl0, rtol=RTOL, atol=ATOL) 
开发者ID:locuslab,项目名称:optnet,代码行数:16,代码来源:optnet-np.py


注:本文中的numdifftools.Gradient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。