How to use the diffcp.utils.get_random_like function in diffcp

To help you get started, we’ve selected a few diffcp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github cvxgrp / diffcp / tests.py View on Github external
def test_solve_and_derivative(self):
        np.random.seed(0)
        m = 20
        n = 10

        A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
        for mode in ["lsqr", "dense"]:
            x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
                A, b, c, cone_dims, eps=1e-10, mode=mode, solver="SCS")

            dA = utils.get_random_like(
                A, lambda n: np.random.normal(0, 1e-6, size=n))
            db = np.random.normal(0, 1e-6, size=b.size)
            dc = np.random.normal(0, 1e-6, size=c.size)

            dx, dy, ds = derivative(dA, db, dc)

            x_pert, y_pert, s_pert, _, _ = cone_prog.solve_and_derivative(
                A + dA, b + db, c + dc, cone_dims, eps=1e-10, solver="SCS")

            np.testing.assert_allclose(x_pert - x, dx, atol=1e-8)
            np.testing.assert_allclose(y_pert - y, dy, atol=1e-8)
            np.testing.assert_allclose(s_pert - s, ds, atol=1e-8)

            x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
                A, b, c, cone_dims, eps=1e-10, mode=mode, solver="SCS")
github cvxgrp / diffcp / tests.py View on Github external
def test_get_random_like(self):
        np.random.seed(0)
        A = sparse.eye(5)
        B = utils.get_random_like(A,
                                  lambda n: np.random.normal(0, 1e-6, size=n))
        A_r, A_c = [list(x) for x in A.nonzero()]
        B_r, B_c = [list(x) for x in B.nonzero()]
        self.assertListEqual(A_r, B_r)
        self.assertListEqual(A_c, B_c)
github cvxgrp / diffcp / prof.py View on Github external
import time

import diffcp.cone_program as cone_prog
import diffcp.cones as cone_lib
import diffcp.utils as utils


m = 100
n = 50

A, b, c, cone_dims = utils.least_squares_eq_scs_data(m, n)
for mode in ["lsqr", "dense"]:
    x, y, s, derivative, adjoint_derivative = cone_prog.solve_and_derivative(
        A, b, c, cone_dims, eps=1e-10, mode=mode)

    dA = utils.get_random_like(
        A, lambda n: np.random.normal(0, 1e-2, size=n))
    db = np.random.normal(0, 1e-2, size=b.size)
    dc = np.random.normal(0, 1e-2, size=c.size)

    derivative_time = 0.0
    for _ in range(10):
        tic = time.time()
        dx, dy, ds = derivative(dA, db, dc)
        toc = time.time()
        derivative_time += (toc - tic) / 10

    adjoint_derivative_time = 0.0
    for _ in range(10):
        tic = time.time()
        dA, db, dc = adjoint_derivative(
            c, np.zeros(y.size), np.zeros(s.size))