Giter VIP home page Giter VIP logo

constrainet's Introduction

ConstraiNet

A PyTorch implementation of ConstraiNet – differentiable output constrained neural network layer.

This repository includes a python package, containing:

  • ConstraiNetLayer – a neural network layer with (hard) constrained output;
  • Simple Feed-Forward DenseConstraiNet – easy-to-use neural network with constrained output;
  • test_runner – a simple testing platform for evaluation of different layer implementations on optimization tasks;
  • Utility methods for problems generation, etc.;
  • Other less successful neural network layers & models for constraining output (considered during research).

Usage

The most successful models in the package are ConstraiNetLayer and DenseConstraiNet.

A ConstraiNetLayer is a layer that generates output vectors only inside the domain. The domain is determined by a constraint set, which can be constructed with:

  • LinearConstraints(A, b) – linear inequality constraints of form A x <= b;
  • LinearEqualityConstraints(A, b) – linear equality constraints of form A x == b;
  • QuadraticConstraints(P, q, b) – quadratic inequality constraints of form x^T P_i x + q^T_i x <= b_i.

To create a ConstraiNetLayer just pass a list of constraints (also you can pass only one constraints instance, or a ConstraintSet):

from constrainet import (
    LinearConstraints,
    QuadraticConstraints,
    ConstraiNetLayer,
    DenseConstraiNet,
)

c_layer = ConstraiNetLayer(
    constraints=[
        # a triangle
        LinearConstraints(
            A=np.array([[-1., 0.], [0., -1.], [1., 1.]]),
            b=np.array([0., 0., 1.]),
        ),
        # a circle
        QuadraticConstraints(
            P=2.0 * np.eye(2, dtype=np.float64)[np.newaxis],
            q=np.zeros((1, 2), dtype=np.float64),
            b=(0.75 ** 2) * np.ones((1,), dtype=np.float64)
        )
    ],
    mode='center_projection',  # or 'ray_shift'
)

Here is an example of mapping created by this layer: linear_and_quadratic_example

The layer c_layer will take vectors of dimension (batch_size, c_layer.n_inputs) as input and will return constrained vectors of dimension determined by the given constraints.

Note, that c_layer.n_inputs depends on layer mode, which may be one of:

  • 'center_projection' – then the number of inputs is the same as output vector dimension;
  • 'ray_shift" – then the number of inputs is n_features + 1.

To be able to easily handle different modes, we provide simple dense network with constrained output – DenseConstraiNet. It consists of a feed-forward network and a ConstraiNetLayer at the end, and it automatically decides what number of inputs should be passed to the ConstraiNetLayer:

nn = DenseConstraiNet(
    input_dim=input_dim,             # number of input features
    hidden_dim=config.nn_hidden_dim,  # width of hidden layers
    n_layers=config.nn_n_layers,      # number of hidden layers
    constraints=constraints,         # list of constraints
    mode='center_projection',        # ConstraiNetLayer mode (another option: 'ray_shift')
)

For convenience it is possible to pass multiple separate LinearConstraints, QuadraticConstraints in arbitrary order.

Installation

Development

Currently the package is at the development stage, so it cannot be installed from PyPI.

We use Hatch for project management, but we believe that the package can be easily converted for use with any other tool, like Poetry.

  1. Install Hatch.

  2. Clone repository and go to the project directory.

    git clone https://github.com/andruekonst/ConstraiNet.git
    cd ConstraiNet
  3. Create a Hatch environment.

    hatch env create
  4. Activate Hatch shell.

    hatch shell

Now you can run any script that uses ConstraiNet.

constrainet's People

Contributors

andruekonst avatar

Stargazers

 avatar Jon Arrizabalaga avatar  avatar  avatar

Watchers

 avatar Kostas Georgiou avatar

constrainet's Issues

Are there any examples for constraints that depends on input?

Hi,

I am interested in your work and I was wondering if you could point me to an example where ConstraiNet is used for a constraint of the neural network that is based on the input to the network.

I want to use your method to ensure a constraint Ax<=b is met, however, the A matrix depends on the input whereas b is fixed.

Thanks,
Arnaud

Some questions about combining ConstraiNetLayer with regular torch-based neural networks

The ConstraiNetLayer you proposed is very good. I tried to add it to the neural network, but I found that this layer seems to be calculated only through CPU. Doesn't ConstraiNetLayer support CUDA and GPU?
Another question is that numpy is used to define constraints in the ConstraiNetLayer layer. Will this cause the data format conversion between numpy and tensor to consume a lot of time? Because I found the following warning in my calculation example:
UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor.
Forgive me for not having deep mathematical knowledge :)

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.