Verified-Intelligence / auto_LiRPA

auto_LiRPA: An Automatic Linear Relaxation based Perturbation Analysis Library for Neural Networks and General Computational Graphs
https://arxiv.org/pdf/2002.12920
Other
269 stars 67 forks source link

Error messages for feedforward networks with method CROWN #27

Closed AntonXue closed 2 years ago

AntonXue commented 2 years ago

I am trying to compute_bounds on some feedforward networks and getting an error. Here is a minimal failing example:

import torch
import torch.nn as nn
from auto_LiRPA import BoundedModule, BoundedTensor, PerturbationLpNorm

# Set up inputs
x = torch.ones(2)
ptb_input = BoundedTensor(x, PerturbationLpNorm(eps=0.1))

# The model
model = nn.Sequential(
  nn.Linear(2, 10),
  nn.ReLU(),
  nn.Linear(10, 10),
  nn.ReLU(),
  nn.Linear(10, 2)
)
model = BoundedModule(model, x)

# 
lb, ub = model.compute_bounds(x=(ptb_input,), method="IBP") # This works

lb, ub = model.compute_bounds(x=(ptb_input,), method="CROWN") # This does not

Which gets the output:

Traceback (most recent call last):
  File "/home/antonxue/stuff/test/autolirpa/hello.py", line 22, in <module>
    lb, ub = model.compute_bounds(x=(ptb_input,), method="CROWN") # This does not
  File "/home/antonxue/foo/miniconda3/envs/jl/lib/python3.9/site-packages/auto_LiRPA/bound_general.py", line 1334, in compute_bounds
    num_channel = node.output_shape[-3]
IndexError: tuple index out of range

Why is this the case, and what should I do differently?

shizhouxing commented 2 years ago

Hi @AntonXue , it's recommended to have a batch dimension for input tensor. If you change the input into:

x = torch.ones(1, 2) # 1 stands for batch size

it works for me.

Our code may assume the existence of the batch dimension, so there can be issues if the input doesn't have a batch dimension.

AntonXue commented 2 years ago

This works! Thank you very much for the help :)