-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhessian_eig_driver.py
74 lines (60 loc) · 2.65 KB
/
hessian_eig_driver.py
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
64
65
66
67
68
69
70
71
72
73
74
from __future__ import print_function
import numpy as np
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
# from progressbar import *
from utils import *
from models.c1 import *
import hessianflow as hf
import hessianflow.optimizer as hf_optm
# Training settings
parser = argparse.ArgumentParser(description = 'PyTorch Example')
parser.add_argument('--name', type = str, default = 'cifar10', metavar = 'N',
help='dataset')
parser.add_argument('--batch-size', type = int, default = 128, metavar = 'N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type = int, default = 200, metavar = 'N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--seed', type = int, default = 1, metavar = 'S',
help = 'random seed (default: 1)')
parser.add_argument('--arch', type = str, default = 'c1',
help = 'choose the archtecure')
parser.add_argument('--eigen-type', type = str, default = 'approximate',
help = 'full dataset of subset')
parser.add_argument('--resume', type = str, default = './model_param.pkl',
help = 'choose the archtecure')
args = parser.parse_args()
# set random seed to reproduce the work
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
# get dataset
train_loader, test_loader = getData(name = args.name, train_bs = args.batch_size, test_bs = args.test_batch_size)
# get model and optimizer
model_list = {
'c1':c1_model(),
'c2':c2_model(),
}
model = model_list[args.arch].cuda()
model = torch.nn.DataParallel(model)
#model.load_state_dict(torch.load(args.resume))
print(' Total params: %.2fM' % (sum(p.numel() for p in model.parameters()) / 1000000.0))
criterion = nn.CrossEntropyLoss()
if args.eigen_type == 'full':
# compute the eigen information based on the whole testing data set
eigenvalue, eigenvector = hf.get_eigen_full_dataset(model, test_loader, criterion, cuda = True, maxIter = 10, tol = 1e-2)
elif args.eigen_type == 'approximate':
# here we choose a random batch from test_loader to compute approximating eigen information
get_data = True
for data, target in test_loader:
# finish the for loop otherwise there is a warning
if get_data:
inputs = data
targets = target
get_data = False
eigenvalue, eigenvector = hf.get_eigen(model, inputs, targets, criterion, cuda = True, maxIter = 10, tol = 1e-2)
print('Eigenvalue is: %.2f' % eigenvalue)