Giter Club home page Giter Club logo

constorch's Introduction

constorch

Constrained optimization in PyTorch. Currently uses transformation-based constraints.

Example usage

# demo_constorch.py

import torch
import numpy as np
from constorch import yktorch as ykt
from matplotlib import pyplot as plt


class DemoModule(ykt.BoundedModule):
    def __init__(self, param0):
        super().__init__()
        self.lbub = ykt.BoundedParameter(param0, lb=-1., ub=1.)
        self.lb = ykt.BoundedParameter(param0, lb=-1., ub=np.inf)
        self.ub = ykt.BoundedParameter(param0, lb=-np.inf, ub=1.)
        self.no_bound = ykt.BoundedParameter(param0, lb=-np.inf, ub=np.inf)

    def forward(self, target):
        """
        Note: currently need to use indexing, e.g., param[:] or param[1:],
        to invoke __getitem__() and __setitem__()
        """
        return (
                (self.lbub[:] - target) ** 2
                + (self.lb[:] - target) ** 2
                + (self.ub[:] - target) ** 2
                + (self.no_bound[:] - target) ** 2
        ).sum()


# Usage is almost the same as torch.nn.Module,
# except that currently a parameter's value needs to be
# referred to as, e.g., model.param[:], 
# to invoke __getitem__() and __setitem__().
model = DemoModule(torch.tensor([0.5, -0.5, 0.]))
print(model.lbub[:] + 2.) # works like regular parameters

loss = model(torch.tensor([-2., 2., 0.]))
model.zero_grad()
loss.backward()

# Loss and gradient vector, useful for optimization
# with scipy.optimize.minimize()
print(loss)
print(model.grad_vec())

# Bar plot of parameters with bounds, useful for monitoring training
model.plot_params()
plt.show()

constorch's People

Contributors

gravifer avatar yulkang avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.