可微分非線性優化的庫
紙張•視頻•Twitter•網頁•教程
Theseus是一個有效的應用程序庫,用於在Pytorch中構建自定義非線性優化層,以支持在機器人技術和視覺中構建各種問題作為端到端可區分體系結構。
可區分的非線性優化提供了一個通用方案來編碼歸納先驗,因為目標函數可以通過神經模型部分地參數化,部分可以與專家領域特定的可區分模型進行部分參數。通過通過優化器進行區分來計算梯度端到端的能力可以保留,該優化器使神經模型可以訓練最終的任務損失,同時還利用優化器捕獲的先驗。
請參閱使用Theseus發表的論文列表,以獲取各種應用程序域中的示例。
我們的實現提供了易於使用的接口來構建自定義優化層並將其插入任何神經體系結構。當前可用以下可微分功能:
我們支持一些改善計算時間和內存消耗的功能:
torch安裝。要安裝您的特定CPU/CUDA配置,請按照Pytorch網站中的說明進行操作。nvcc --version匹配。如果不是,請安裝並確保其位置在系統的$PATH變量上。suitesparse ,您可以通過以下方式安裝:sudo apt-get install libsuitesparse-dev (ubuntu)。conda install -c conda-forge suitesparse (Mac)。PYPI
pip install theseus-ai目前,我們使用CUDA 11.6和Python 3.10提供了輪子的CUDA擴展名。對於其他CUDA版本,請考慮從源安裝或使用我們的構建腳本。
請注意, pypi安裝不包括我們的實驗性Theseus實驗室。為此,請從源安裝。
從源安裝Theseus的最簡單方法是運行以下內容(請參見下面的進一步,還包括Baspacho)
git clone https://github.com/facebookresearch/theseus.git && cd theseus
pip install -e .如果您有興趣為Thues貢獻,請安裝
pip install -e " .[dev] "
pre-commit install並遵循更詳細的貢獻說明。
從源安裝baspacho擴展
默認情況下,從源安裝不包括我們的baspacho稀疏求解器擴展名。為此,請按照以下步驟:
從源彙編的baspacho此處按照說明進行編譯。我們建議使用flags -DBLA_STATIC=ON -DBUILD_SHARED_LIBS=OFF 。
跑步
git clone https://github.com/facebookresearch/theseus.git && cd theseus
BASPACHO_ROOT_DIR= < path/to/root/baspacho/dir > pip install -e . baspacho root dir必須在子目錄build中具有二進製文件。
dev ) python -m pytest tests默認情況下,單位測試包括我們CUDA擴展的測試。您可以在沒有CUDA支持的情況下添加選項-m "not cudaext"以跳過它們。此外,當未編譯extlib時,稀疏求解器baspacho的測試會自動跳過。
簡單示例。此示例適合曲線CostFunction來計算殘差的單個成本函數的Objective Objective和GaussNewton優化器被封裝到TheseusLayer中。隨著Adam和MSE損失, TheseusLayer區分來學習。
import torch
import theseus as th
x_true , y_true , v_true = read_data () # shapes (1, N), (1, N), (1, 1)
x = th . Variable ( torch . randn_like ( x_true ), name = "x" )
y = th . Variable ( y_true , name = "y" )
v = th . Vector ( 1 , name = "v" ) # a manifold subclass of Variable for optim_vars
def error_fn ( optim_vars , aux_vars ): # returns y - v * exp(x)
x , y = aux_vars
return y . tensor - optim_vars [ 0 ]. tensor * torch . exp ( x . tensor )
objective = th . Objective ()
cost_function = th . AutoDiffCostFunction (
[ v ], error_fn , y_true . shape [ 1 ], aux_vars = [ x , y ],
cost_weight = th . ScaleCostWeight ( 1.0 ))
objective . add ( cost_function )
layer = th . TheseusLayer ( th . GaussNewton ( objective , max_iterations = 10 ))
phi = torch . nn . Parameter ( x_true + 0.1 * torch . ones_like ( x_true ))
outer_optimizer = torch . optim . Adam ([ phi ], lr = 0.001 )
for epoch in range ( 10 ):
solution , info = layer . forward (
input_tensors = { "x" : phi . clone (), "v" : torch . ones ( 1 , 1 )},
optimizer_kwargs = { "backward_mode" : "implicit" })
outer_loss = torch . nn . functional . mse_loss ( solution [ "v" ], v_true )
outer_loss . backward ()
outer_optimizer . step ()請參閱教程,機器人和遠見示例,以了解API和用法。
如果您在工作中使用Theseus,請在下面的Bibtex引用紙張。
@article { pineda2022theseus ,
title = { {Theseus: A Library for Differentiable Nonlinear Optimization} } ,
author = { Luis Pineda and Taosha Fan and Maurizio Monge and Shobha Venkataraman and Paloma Sodhi and Ricky TQ Chen and Joseph Ortiz and Daniel DeTone and Austin Wang and Stuart Anderson and Jing Dong and Brandon Amos and Mustafa Mukadam } ,
journal = { Advances in Neural Information Processing Systems } ,
year = { 2022 }
}Theseus已獲得麻省理工學院的許可。有關詳細信息,請參見許可證。
以下貢獻者使Thusus成為可能:
用貢獻製成。