File size: 2,289 Bytes
e0c7c25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os, sys, numpy, torch, argparse, skimage, json, shutil
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
import matplotlib

def main():
    parser = argparse.ArgumentParser(description='ACE optimization utility',
            prog='python -m netdissect.aceoptimize')
    parser.add_argument('--classname', type=str, default=None,
                        help='intervention classname')
    parser.add_argument('--layer', type=str, default='layer4',
                        help='layer name')
    parser.add_argument('--l2_lambda', type=float, nargs='+',
                        help='l2 regularizer hyperparameter')
    parser.add_argument('--outdir', type=str, default=None,
                        help='dissection directory')
    parser.add_argument('--variant', type=str, default=None,
                        help='experiment variant')
    args = parser.parse_args()

    if args.variant is None:
        args.variant = 'ace'

    run_command(args)

def run_command(args):
    fig = Figure(figsize=(4.5,3.5))
    FigureCanvas(fig)
    ax = fig.add_subplot(111)
    for l2_lambda in args.l2_lambda:
        variant = args.variant
        if l2_lambda != 0.01:
            variant += '_reg%g' % l2_lambda

        dirname = os.path.join(args.outdir, args.layer, variant, args.classname)
        snapshots = os.path.join(dirname, 'snapshots')
        try:
            dat = [torch.load(os.path.join(snapshots, 'epoch-%d.pth' % i))
                 for i in range(10)]
        except:
            print('Missing %s snapshots' % dirname)
            return
        print('reg %g' % l2_lambda)
        for i in range(10):
            print(i, dat[i]['avg_loss'],
                  len((dat[i]['ablation'] == 1).nonzero()))

        ax.plot([dat[i]['avg_loss'] for i in range(10)],
            label='reg %g' % l2_lambda)
    ax.set_title('%s %s' % (args.classname, args.variant))
    ax.grid(True)
    ax.legend()
    ax.set_ylabel('Loss')
    ax.set_xlabel('Epochs')
    fig.tight_layout()
    dirname = os.path.join(args.outdir, args.layer,
            args.variant, args.classname)
    fig.savefig(os.path.join(dirname, 'loss-plot.png'))

if __name__ == '__main__':
    main()