revbucket / geometric-certificates

Geometric Certifications of Neural Nets
41 stars 6 forks source link

Which code is to reproduce the figure "Network Input Partioning" in readme? #3

Open chenwydj opened 4 years ago

chenwydj commented 4 years ago

Thank you very much!

Kaleidophon commented 3 years ago

I would also be interested in that!

Kaleidophon commented 3 years ago

So I am not sure if this is still a current issues for @chenwydj, but I'll post what I learned for the last couple of days here for future reference. How to reproduce this figures seems indeed not documented explicitly, but I think I got the solution after a lot of time reading through the code and even more trial and error.

You can start off by using the example in 2D_example.ipynb where we train a plnn_obj. Then, I used the following code to plot the polytopes induced by this network:

from _polytope_ import Polytope
from utilities import plot_polytopes_2d

geo = GeoCert(plnn_obj, hyperbox_bounds=(-3.5, 3), verbose=True, neuron_bounds="ia")
test_point = torch.Tensor([0, 0])
region_count = geo.run(test_point, lp_norm='l_inf', problem_type='count_regions',  decision_radius=3)

polylist = []

for polytope in region_count.seen_polytopes:
    config = plnn_obj.config_str_to_config_list(polytope)
    polytope_config = plnn_obj.compute_polytope_config(config)
    poly = Polytope.from_polytope_dict(polytope_config, test_point)
    polylist.append(poly)

plot_polytopes_2d(polylist, xylim=[-3.5, 3])