项目作者: mctorch

项目描述 :
A manifold optimization library for deep learning
高级语言: Python
项目地址: git://github.com/mctorch/mctorch.git
创建时间: 2018-09-21T14:16:20Z
项目社区:https://github.com/mctorch/mctorch

开源协议:MIT License

下载


McTorch Lib, a manifold optimization library for deep learning

McTorch is a Python library that adds manifold optimization functionality to PyTorch.

McTorch:

  • Leverages tensor computation and GPU acceleration from PyTorch.
  • Enables optimization on manifold constrained tensors to address nonlinear optimization problems.
  • Facilitates constrained weight tensors in deep learning layers.

Sections:

More about McTorch

McTorch builds on top of PyTorch and supports all PyTorch functions in addition to Manifold optimization. This is done to ensure researchers and developers using PyTorch can easily experiment with McTorch functions. McTorch’s manifold implementations and optimization methods are derived from the Matlab toolbox Manopt and the Python toolbox Pymanopt.

Using McTorch for Optimization

  1. Initialize Parameter - McTorch manifold parameters are same as PyTorch parameters (mctorch.nn.Parameter) and requires just addition of one property to parameter initialization to constrain the parameter values.
  2. Define Cost - Cost function can be any PyTorch function using the above parameter mixed with non constrained parameters.
  3. Optimize - Any optimizer from mctorch.optim can be used to optimize the cost function using same functionality as any PyTorch code.

PCA Example

  1. import torch
  2. import mctorch.nn as mnn
  3. import mctorch.optim as moptim
  4. # Random data with high variance in first two dimension
  5. X = torch.diag(torch.FloatTensor([3,2,1])).matmul(torch.randn(3,200))
  6. # 1. Initialize Parameter
  7. manifold_param = mnn.Parameter(manifold=mnn.Stiefel(3,2))
  8. # 2. Define Cost - squared reconstruction error
  9. def cost(X, w):
  10. wTX = torch.matmul(w.transpose(1,0), X)
  11. wwTX = torch.matmul(w, wTX)
  12. return torch.sum((X - wwTX)**2)
  13. # 3. Optimize
  14. optimizer = moptim.rAdagrad(params = [manifold_param], lr=1e-2)
  15. for epoch in range(30):
  16. cost_step = cost(X, manifold_param)
  17. print(cost_step)
  18. cost_step.backward()
  19. optimizer.step()
  20. optimizer.zero_grad()

Using McTorch for Deep Learning

Multi Layer Perceptron Example

  1. import torch
  2. import mctorch.nn as mnn
  3. import torch.nn.functional as F
  4. # a torch module using constrained linear layers
  5. class ManifoldMLP(nn.Module):
  6. def __init__(self):
  7. super(ManifoldMLP, self).__init__()
  8. self.layer1 = mnn.rLinear(in_features=28*28, out_features=100, weight_manifold=mnn.Stiefel)
  9. self.layer2 = mnn.rLinear(in_features=100, out_features=100, weight_manifold=mnn.PositiveDefinite)
  10. self.output = mnn.rLinear(in_features=100, out_features=10, weight_manifold=mnn.Stiefel)
  11. def forward(self, x):
  12. x = F.relu(self.layer1(x))
  13. x = F.relu(self.layer2(x))
  14. x = F.log_softmax(self.output(x), dim=0)
  15. return x
  16. # create module object and compute cost by applying module on inputs
  17. mlp_module = ManifoldMLP()
  18. cost = mlp_module(inputs)

More examples added - here

Functionality Supported

This would be an ever increasing list of features. McTorch currently supports:

Manifolds

  • Stiefel
  • Positive Definite
  • Hyperbolic
  • Doubly Stochastic

All manifolds support k multiplier as well.

Optimizers

  • rSGD
  • rAdagrad
  • rASA
  • rConjugateGradient

Layers

  • Linear
  • Conv1d, Conv2d, Conv3d

Installation

After installing PyTorch can be installed with python setup.py install

Linux

  1. source activate myenv
  2. conda install numpy setuptools
  3. # Add LAPACK support for the GPU if needed
  4. conda install -c pytorch magma-cuda90 # or [magma-cuda80 | magma-cuda92 | magma-cuda100 ] depending on your cuda version
  5. conda install pytorch torchvision cudatoolkit=9.0 -c pytorch # or cudatoolkit=10.0 | cudatoolkit=10.1 | .. depending on your cuda version
  6. pip install mctorch-lib

Release and Contribution

McTorch is currently under development and any contributions, suggestions and feature requests are welcome. We’d closely follow PyTorch stable versions to keep the base updated and will have our own versions for other additions.

McTorch is released under the open source 3-clause BSD License.

Team

Reference

Please cite [1] if you found this code useful.

McTorch, a manifold optimization library for deep learning

[1] M. Meghawanshi, P. Jawanpuria, A. Kunchukuttan, H. Kasai, and B. Mishra, McTorch, a manifold optimization library for deep learning

  1. @techreport{meghwanshi2018mctorch,
  2. title={McTorch, a manifold optimization library for deep learning},
  3. author={Meghwanshi, Mayank and Jawanpuria, Pratik and Kunchukuttan, Anoop and Kasai, Hiroyuki and Mishra, Bamdev},
  4. institution={arXiv preprint arXiv:1810.01811},
  5. year={2018}
  6. }