Skip to main content

Bound propagation in Pytorch

Project description

Bound propagation

Linear and interval bound propagation in Pytorch with easy-to-use API, GPU support, and heavy parallization. Initially made as an alternative to the original CROWN implementation which featured only Numpy, lots of for-loops, and a cumbersome API.

To install:

pip install bound-propagation

Supported bound propagation methods:

For the examples below assume the following network definition:

from torch import nn
from bound_propagation import crown, crown_ibp, ibp

# The decorators _must_ be on a subclass of nn.Sequential

@crown
@crown_ibp
@ibp
class Network(nn.Sequential):
    def __init__(self, *args):
        if args:
            # To support __get_index__ of nn.Sequential when slice indexing
            # CROWN (and implicitly CROWN-IBP) is doing this underlying
            super().__init__(*args)
        else:
            in_size = 30
            classes = 10

            super().__init__(
                nn.Linear(in_size, 16),
                nn.Tanh(),
                nn.Linear(16, 16),
                nn.Tanh(),
                nn.Linear(16, classes)
            )

net = Network()

Alternatively, you can add the functions to your network by calling the functions with an instance of your network:

from torch import nn
from bound_propagation import crown, crown_ibp, ibp

class Network(nn.Sequential):
    def __init__(self, *args):
        if args:
            # To support __get_index__ of nn.Sequential when slice indexing
            # CROWN (and implicitly CROWN-IBP) is doing this underlying
            super().__init__(*args)
        else:
            in_size = 30
            classes = 10

            super().__init__(
                nn.Linear(in_size, 16),
                nn.Tanh(),
                nn.Linear(16, 16),
                nn.Tanh(),
                nn.Linear(16, classes)
            )

# The instance _must_ be an nn.Sequential or a subclass thereof
net = crown(crown_ibp(ibp(Network())))

The method also works with nn.Sigmoid and nn.ReLU.

Interval bounds

To get interval bounds for either IBP, CROWN, or CROWN-IBP:

x = torch.rand(100, 30)
epsilon = 0.1
lower, upper = x - epsilon, x + epsilon

ibp_bounds = net.ibp(lower, upper)
crown_bounds = net.crown_interval(lower, upper)
crown_ibp_bounds = net.crown_ibp_interval(lower, upper)

Linear bounds

To get linear bounds for either CROWN or CROWN-IBP:

x = torch.rand(100, 30)
epsilon = 0.1
lower, upper = x - epsilon, x + epsilon

crown_bounds = net.crown_linear(lower, upper)
crown_ibp_bounds = net.crown_ibp_linear(lower, upper)

Authors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bound_propagation-0.2.0.tar.gz (21.1 kB view hashes)

Uploaded Source

Built Distribution

bound_propagation-0.2.0-py3-none-any.whl (22.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page