可微凸优化层
cvxpylayers的Python项目详细描述
CVX层
cvxpylayers是一个用于构造可微凸的Python库 使用CVXPY优化Pythorch和TensorFlow中的层。 凸优化层解决参数化凸优化问题 在向前传球时产生一个解决方案。 它计算关于 反向过程中的参数。在
这个图书馆是我们的NeurIPS 2019 paper 关于可微凸优化层。 有关凸优化层的非正式介绍,请参见 blog post。在
我们的包使用CVXPY来指定 参数化凸优化问题。在
安装
使用包管理器pip安装 cvxpylayers公司。在
pip install cvxpylayers
我们的软件包包括Pythorch和TensorFlow 2.0的凸优化层; 这些层在功能上是等效的。您需要安装 PyTorch或{a15} 他们的说明可以单独在他们的网站上完成。在
cvxpylayers具有以下依赖关系:
- Python3
- NumPy
- CVXPY>;=1.1.a4
- TensorFlow>;=2.0或PyTorch>;=1.0
- diffcp>;=1.0.13
使用
下面是Pythorch和TensorFlow层的使用示例。请注意 在CVXPY中必须构造参数化凸优化问题, 使用DPP。在
Pythorch
^{pr2}$注意:CvxpyLayer
不能用torch.jit
来跟踪。在
张量流2
importcvxpyascpimporttensorflowastffromcvxpylayers.tensorflowimportCvxpyLayern,m=2,3x=cp.Variable(n)A=cp.Parameter((m,n))b=cp.Parameter(m)constraints=[x>=0]objective=cp.Minimize(0.5*cp.pnorm(A@x-b,p=1))problem=cp.Problem(objective,constraints)assertproblem.is_dpp()cvxpylayer=CvxpyLayer(problem,parameters=[A,b],variables=[x])A_tf=tf.Variable(tf.random.normal((m,n)))b_tf=tf.Variable(tf.random.normal((m,)))withtf.GradientTape()astape:# solve the problem, setting the values of A, b to A_tf, b_tfsolution,=cvxpylayer(A_tf,b_tf)summed_solution=tf.math.reduce_sum(solution)# compute the gradient of the summed solution with respect to A, bgradA,gradb=tape.gradient(summed_solution,[A_tf,b_tf])
注意:CvxpyLayer
不能用tf.function
来跟踪。在
Log-Log凸规划
从0.1.3版开始,cvxpylayers还可以通过log-log凸程序(LLCPs)进行区分,后者是几何程序的泛化。在为LLCP构造CvxpyLayer
时,请使用关键字参数gp=True
。下面是一个简单的用法示例
importcvxpyascpimporttorchfromcvxpylayers.torchimportCvxpyLayerx=cp.Variable(pos=True)y=cp.Variable(pos=True)z=cp.Variable(pos=True)a=cp.Parameter(pos=True,value=2.)b=cp.Parameter(pos=True,value=1.)c=cp.Parameter(value=0.5)objective_fn=1/(x*y*z)objective=cp.Minimize(objective_fn)constraints=[a*(x*y+x*z+y*z)<=b,x>=y**c]problem=cp.Problem(objective,constraints)assertproblem.is_dgp(dpp=True)layer=CvxpyLayer(problem,parameters=[a,b,c],variables=[x,y,z],gp=True)a_tch=torch.tensor(a.value,requires_grad=True)b_tch=torch.tensor(b.value,requires_grad=True)c_tch=torch.tensor(c.value,requires_grad=True)x_star,y_star,z_star=layer(a_tch,b_tch,c_tch)sum_of_solution=x_star+y_star+z_starsum_of_solution.backward()
示例
我们的examples子目录包含凸优化的简单应用 IPython笔记本的层。在
贡献
欢迎拉取请求。如需重大更改,请先将问题打开到 讨论你想改变什么。在
请确保根据需要更新测试。在
请用flake8
来连接代码。在
pip install flake8 # if not already installed
flake8
运行测试
cvxpylayers使用pytest
框架来运行测试。
要安装pytest
,请运行:
pip install pytest
要运行torch
的测试,请在此存储库的主目录中运行:
pytest cvxpylayers/torch
要运行tensorflow
的测试,请在此存储库的主目录中运行:
pytest cvxpylayers/tensorflow
使用cvxpylayers的项目
下面是使用cvxpylayers的项目列表。如果你在一个项目中使用过cvxpylayers,欢迎你做个公关,把它添加到这个列表中。在
许可证
cvxpylayers拥有apache2.0许可证。在
引用
如果您使用CVxPyrayers进行研究,请引用我们随附的NeurIPS paper:
@inproceedings{cvxpylayers2019,
author={Agrawal, A. and Amos, B. and Barratt, S. and Boyd, S. and Diamond, S. and Kolter, Z.},
title={Differentiable Convex Optimization Layers},
booktitle={Advances in Neural Information Processing Systems},
year={2019},
}
如果您使用cvxpylayers通过log-log凸程序进行区分,请引用随附的paper:
@article{agrawal2020differentiating,
title={Differentiating through log-log convex programs},
author={Agrawal, Akshay and Boyd, Stephen},
journal={arXiv},
archivePrefix={arXiv},
eprint={2004.12553},
primaryClass={math.OC},
year={2020},
}
- 项目
标签: