{ "info": { "author": "", "author_email": "", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 3" ], "description": "![cvxpylayers logo](cvxpylayers_logo.png)\n[![Build Status](https://travis-ci.org/cvxgrp/cvxpylayers.svg?branch=master)](https://travis-ci.org/cvxgrp/cvxpylayers)\n[![Build Status](https://ci.appveyor.com/api/projects/status/dhtbi9fb96mce56i/branch/master?svg=true)](https://ci.appveyor.com/project/sbarratt/cvxpylayers/branch/master)\n\n# cvxpylayers\n\ncvxpylayers is a Python library for constructing differentiable convex\noptimization layers in PyTorch, JAX, and TensorFlow using CVXPY.\nA convex optimization layer solves a parametrized convex optimization problem\nin the forward pass to produce a solution.\nIt computes the derivative of the solution with respect to\nthe parameters in the backward pass.\n\nThis library accompanies our [NeurIPS 2019 paper](https://web.stanford.edu/~boyd/papers/pdf/diff_cvxpy.pdf)\non differentiable convex optimization layers.\nFor an informal introduction to convex optimization layers, see our\n[blog post](https://locuslab.github.io/2019-10-28-cvxpylayers/).\n\nOur package uses [CVXPY](https://github.com/cvxgrp/cvxpy) for specifying\nparametrized convex optimization problems.\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Examples](#examples)\n- [Contributing](#contributing)\n- [Projects using cvxpylayers](#projects-using-cvxpylayers)\n- [License](#contributing)\n- [Citing](#citing)\n\n## Installation\n\nUse the package manager [pip](https://pip.pypa.io/en/stable/) to install\ncvxpylayers.\n\n```bash\npip install cvxpylayers\n```\n\nOur package includes convex optimization layers for\nPyTorch, JAX, and TensorFlow 2.0;\nthe layers are functionally equivalent. You will need to install\n[PyTorch](https://pytorch.org),\n[JAX](https://github.com/google/jax), or\n[TensorFlow](https://www.tensorflow.org)\nseparately, which can be done by following the instructions on their websites.\n\ncvxpylayers has the following dependencies:\n* Python 3\n* [NumPy](https://pypi.org/project/numpy/)\n* [CVXPY](https://github.com/cvxgrp/cvxpy) >= 1.1.a4\n* [PyTorch](https://pytorch.org) >= 1.0, [JAX](https://github.com/google/jax) >= 0.2.12, or [TensorFlow](https://tensorflow.org) >= 2.0\n* [diffcp](https://github.com/cvxgrp/diffcp) >= 1.0.13\n\n## Usage\nBelow are usage examples of our PyTorch, JAX, and TensorFlow layers.\nNote that the parametrized convex optimization problems must be constructed\nin CVXPY, using\n[DPP](https://www.cvxpy.org/tutorial/advanced/index.html#disciplined-parametrized-programming).\n\n### PyTorch\n\n```python\nimport cvxpy as cp\nimport torch\nfrom cvxpylayers.torch import CvxpyLayer\n\nn, m = 2, 3\nx = cp.Variable(n)\nA = cp.Parameter((m, n))\nb = cp.Parameter(m)\nconstraints = [x >= 0]\nobjective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1))\nproblem = cp.Problem(objective, constraints)\nassert problem.is_dpp()\n\ncvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x])\nA_tch = torch.randn(m, n, requires_grad=True)\nb_tch = torch.randn(m, requires_grad=True)\n\n# solve the problem\nsolution, = cvxpylayer(A_tch, b_tch)\n\n# compute the gradient of the sum of the solution with respect to A, b\nsolution.sum().backward()\n```\n\nNote: `CvxpyLayer` cannot be traced with `torch.jit`.\n\n### JAX\n```python\nimport cvxpy as cp\nimport jax\nfrom cvxpylayers.jax import CvxpyLayer\n\nn, m = 2, 3\nx = cp.Variable(n)\nA = cp.Parameter((m, n))\nb = cp.Parameter(m)\nconstraints = [x >= 0]\nobjective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1))\nproblem = cp.Problem(objective, constraints)\nassert problem.is_dpp()\n\ncvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x])\nkey = jax.random.PRNGKey(0)\nkey, k1, k2 = jax.random.split(key, 3)\nA_jax = jax.random.normal(k1, shape=(m, n))\nb_jax = jax.random.normal(k2, shape=(m,))\n\nsolution, = cvxpylayer(A_jax, b_jax)\n\n# compute the gradient of the summed solution with respect to A, b\ndcvxpylayer = jax.grad(lambda A, b: sum(cvxpylayer(A, b)[0]), argnums=[0, 1])\ngradA, gradb = dcvxpylayer(A_jax, b_jax)\n```\n\nNote: `CvxpyLayer` cannot be traced with the JAX `jit` or `vmap` operations.\n\n### TensorFlow 2\n```python\nimport cvxpy as cp\nimport tensorflow as tf\nfrom cvxpylayers.tensorflow import CvxpyLayer\n\nn, m = 2, 3\nx = cp.Variable(n)\nA = cp.Parameter((m, n))\nb = cp.Parameter(m)\nconstraints = [x >= 0]\nobjective = cp.Minimize(0.5 * cp.pnorm(A @ x - b, p=1))\nproblem = cp.Problem(objective, constraints)\nassert problem.is_dpp()\n\ncvxpylayer = CvxpyLayer(problem, parameters=[A, b], variables=[x])\nA_tf = tf.Variable(tf.random.normal((m, n)))\nb_tf = tf.Variable(tf.random.normal((m,)))\n\nwith tf.GradientTape() as tape:\n # solve the problem, setting the values of A, b to A_tf, b_tf\n solution, = cvxpylayer(A_tf, b_tf)\n summed_solution = tf.math.reduce_sum(solution)\n# compute the gradient of the summed solution with respect to A, b\ngradA, gradb = tape.gradient(summed_solution, [A_tf, b_tf])\n```\n\nNote: `CvxpyLayer` cannot be traced with `tf.function`.\n\n### Log-log convex programs\nStarting with version 0.1.3, cvxpylayers can also differentiate through log-log convex programs (LLCPs), which generalize geometric programs. Use the keyword argument `gp=True` when constructing a `CvxpyLayer` for an LLCP. Below is a simple usage example\n\n```python\nimport cvxpy as cp\nimport torch\nfrom cvxpylayers.torch import CvxpyLayer\n\nx = cp.Variable(pos=True)\ny = cp.Variable(pos=True)\nz = cp.Variable(pos=True)\n\na = cp.Parameter(pos=True, value=2.)\nb = cp.Parameter(pos=True, value=1.)\nc = cp.Parameter(value=0.5)\n\nobjective_fn = 1/(x*y*z)\nobjective = cp.Minimize(objective_fn)\nconstraints = [a*(x*y + x*z + y*z) <= b, x >= y**c]\nproblem = cp.Problem(objective, constraints)\nassert problem.is_dgp(dpp=True)\n\nlayer = CvxpyLayer(problem, parameters=[a, b, c],\n variables=[x, y, z], gp=True)\na_tch = torch.tensor(a.value, requires_grad=True)\nb_tch = torch.tensor(b.value, requires_grad=True)\nc_tch = torch.tensor(c.value, requires_grad=True)\n\nx_star, y_star, z_star = layer(a_tch, b_tch, c_tch)\nsum_of_solution = x_star + y_star + z_star\nsum_of_solution.backward()\n```\n\n## Examples\nOur [examples](examples) subdirectory contains simple applications of convex optimization\nlayers in IPython notebooks.\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to\ndiscuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\nPlease lint the code with `flake8`.\n```bash\npip install flake8 # if not already installed\nflake8\n```\n\n### Running tests\n\ncvxpylayers uses the `pytest` framework for running tests.\nTo install `pytest`, run:\n```bash\npip install pytest\n```\n\nExecute the tests from the main directory of this repository with:\n```bash\npytest cvxpylayers/{torch,jax,tensorflow}\n```\n\n## Projects using cvxpylayers\nBelow is a list of projects using cvxpylayers. If you have used cvxpylayers in a project, you're welcome to make a PR to add it to this list.\n* [Learning Convex Optimization Control Policies](https://web.stanford.edu/~boyd/papers/learning_cocps.html)\n* [Learning Convex Optimization Models](https://web.stanford.edu/~boyd/papers/learning_copt_models.html)\n\n## License\ncvxpylayers carries an Apache 2.0 license.\n\n## Citing\nIf you use cvxpylayers for research, please cite our accompanying [NeurIPS paper](https://web.stanford.edu/~boyd/papers/pdf/diff_cvxpy.pdf):\n\n```\n@inproceedings{cvxpylayers2019,\n author={Agrawal, A. and Amos, B. and Barratt, S. and Boyd, S. and Diamond, S. and Kolter, Z.},\n title={Differentiable Convex Optimization Layers},\n booktitle={Advances in Neural Information Processing Systems},\n year={2019},\n}\n```\n\nIf you use cvxpylayers to differentiate through a log-log convex program, please cite the accompanying [paper](https://web.stanford.edu/~boyd/papers/diff_dgp.html):\n\n```\n@article{agrawal2020differentiating,\n title={Differentiating through log-log convex programs},\n author={Agrawal, Akshay and Boyd, Stephen},\n journal={arXiv},\n archivePrefix={arXiv},\n eprint={2004.12553},\n primaryClass={math.OC},\n year={2020},\n}\n```", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/cvxgrp/cvxpylayers", "keywords": "", "license": "Apache License, Version 2.0", "maintainer": "", "maintainer_email": "", "name": "cvxpylayers", "package_url": "https://pypi.org/project/cvxpylayers/", "platform": "", "project_url": "https://pypi.org/project/cvxpylayers/", "project_urls": { "Homepage": "https://github.com/cvxgrp/cvxpylayers" }, "release_url": "https://pypi.org/project/cvxpylayers/0.1.5/", "requires_dist": null, "requires_python": "", "summary": "Differentiable convex optimization layers", "version": "0.1.5", "yanked": false, "yanked_reason": null }, "last_serial": 10077167, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "369fa109358f262b222b704442661c01", "sha256": "528989c9d7765169e14fa2c1aa140ba71f08894a9c6930359fe621f440068370" }, "downloads": -1, "filename": "cvxpylayers-0.1.0.tar.gz", "has_sig": false, "md5_digest": "369fa109358f262b222b704442661c01", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11979, "upload_time": "2019-10-27T23:22:09", "upload_time_iso_8601": "2019-10-27T23:22:09.004441Z", "url": "https://files.pythonhosted.org/packages/9f/ff/a7c28879ff97782317e510b326521d79bba4173495846646c158ea5c5c94/cvxpylayers-0.1.0.tar.gz", "yanked": false, "yanked_reason": null } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "e2f40baabc495374ebaad1238ed18caf", "sha256": "2d7750ff91f462127efaed3f1e80d2f32c2782388916c3508b62621ad6dc2843" }, "downloads": -1, "filename": "cvxpylayers-0.1.1.tar.gz", "has_sig": false, "md5_digest": "e2f40baabc495374ebaad1238ed18caf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12428, "upload_time": "2019-10-28T21:32:02", "upload_time_iso_8601": "2019-10-28T21:32:02.973212Z", "url": "https://files.pythonhosted.org/packages/f9/1e/b190f6ff29505ba265fe4d7902f5be1125963c704caaef354c92395f4391/cvxpylayers-0.1.1.tar.gz", "yanked": false, "yanked_reason": null } ], "0.1.2": [ { "comment_text": "", "digests": { "md5": "0ba967a5f5355e51271378f6e696dc80", "sha256": "dd1c59ce2899a1004e8dd7052c7dd44785fbf2b4daede0b19af3735f221ebc64" }, "downloads": -1, "filename": "cvxpylayers-0.1.2.tar.gz", "has_sig": false, "md5_digest": "0ba967a5f5355e51271378f6e696dc80", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12596, "upload_time": "2019-11-12T19:40:48", "upload_time_iso_8601": "2019-11-12T19:40:48.090576Z", "url": "https://files.pythonhosted.org/packages/e1/fb/eff511a623bdf09db3fe6dae7c9d45640aa6da3811419434e280fa28d397/cvxpylayers-0.1.2.tar.gz", "yanked": false, "yanked_reason": null } ], "0.1.3": [ { "comment_text": "", "digests": { "md5": "6dba9446f05839da5b9a338e53df4114", "sha256": "35bb0a74ff2652762e11f6d8265efe55d3ba67442c5bac3df16986d9b58210b0" }, "downloads": -1, "filename": "cvxpylayers-0.1.3.tar.gz", "has_sig": false, "md5_digest": "6dba9446f05839da5b9a338e53df4114", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 15720, "upload_time": "2020-04-27T19:01:07", "upload_time_iso_8601": "2020-04-27T19:01:07.054421Z", "url": "https://files.pythonhosted.org/packages/d1/45/80d6d2007ffdb46cd1001bd7c3c60c95711ceeac9fed6567c06fd52ab097/cvxpylayers-0.1.3.tar.gz", "yanked": false, "yanked_reason": null } ], "0.1.4": [ { "comment_text": "", "digests": { "md5": "0d756bb1efefd56c8d81e808a890775d", "sha256": "273a2cebb514af94159d4f0a4beb65dee9e648d633b42f7cabe524e78ee1a3fe" }, "downloads": -1, "filename": "cvxpylayers-0.1.4.tar.gz", "has_sig": false, "md5_digest": "0d756bb1efefd56c8d81e808a890775d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 18639, "upload_time": "2020-06-03T20:02:03", "upload_time_iso_8601": "2020-06-03T20:02:03.101660Z", "url": "https://files.pythonhosted.org/packages/6a/12/a2e94f9a0450bd3d20f2062c7f55ca8026b42478ef8fa3e5e510fe951084/cvxpylayers-0.1.4.tar.gz", "yanked": false, "yanked_reason": null } ], "0.1.5": [ { "comment_text": "", "digests": { "md5": "fc7173c80c4931e305d404fefbd75808", "sha256": "e10882fab76c7d682a1686f9e8f65b8ce20324db9e157f203c82dd5bc4e112ae" }, "downloads": -1, "filename": "cvxpylayers-0.1.5.tar.gz", "has_sig": false, "md5_digest": "fc7173c80c4931e305d404fefbd75808", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24844, "upload_time": "2021-04-15T20:14:49", "upload_time_iso_8601": "2021-04-15T20:14:49.560917Z", "url": "https://files.pythonhosted.org/packages/13/4f/e58e410cadceca32d57215b87460f1a337c7d82fd4c1f1e670c59da7b7fe/cvxpylayers-0.1.5.tar.gz", "yanked": false, "yanked_reason": null } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fc7173c80c4931e305d404fefbd75808", "sha256": "e10882fab76c7d682a1686f9e8f65b8ce20324db9e157f203c82dd5bc4e112ae" }, "downloads": -1, "filename": "cvxpylayers-0.1.5.tar.gz", "has_sig": false, "md5_digest": "fc7173c80c4931e305d404fefbd75808", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24844, "upload_time": "2021-04-15T20:14:49", "upload_time_iso_8601": "2021-04-15T20:14:49.560917Z", "url": "https://files.pythonhosted.org/packages/13/4f/e58e410cadceca32d57215b87460f1a337c7d82fd4c1f1e670c59da7b7fe/cvxpylayers-0.1.5.tar.gz", "yanked": false, "yanked_reason": null } ], "vulnerabilities": [] }