-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
244 lines (200 loc) · 7.78 KB
/
utils.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import os,sys
import numpy as np
from copy import deepcopy
import torch
from tqdm import tqdm
import pandas as pd
########################################################################################################################
def print_model_report(model):
print('-'*100)
print(model)
print('Dimensions =',end=' ')
count=0
for p in model.parameters():
print(p.size(),end=' ')
count+=np.prod(p.size())
print()
print('Num parameters = %s'%(human_format(count)))
print('-'*100)
return count
def human_format(num):
magnitude=0
while abs(num)>=1000:
magnitude+=1
num/=1000.0
return '%.1f%s'%(num,['','K','M','G','T','P'][magnitude])
def print_optimizer_config(optim):
if optim is None:
print(optim)
else:
print(optim,'=',end=' ')
opt=optim.param_groups[0]
for n in opt.keys():
if not n.startswith('param'):
print(n+':',opt[n],end=', ')
print()
return
########################################################################################################################
def get_model(model):
return deepcopy(model.state_dict())
def set_model_(model,state_dict):
model.load_state_dict(deepcopy(state_dict))
return
def freeze_model(model):
for param in model.parameters():
param.requires_grad = False
return
########################################################################################################################
def compute_conv_output_size(Lin,kernel_size,stride=1,padding=0,dilation=1):
return int(np.floor((Lin+2*padding-dilation*(kernel_size-1)-1)/float(stride)+1))
########################################################################################################################
def compute_mean_std_dataset(dataset):
# dataset already put ToTensor
mean=0
std=0
loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False)
for image, _ in loader:
mean+=image.mean(3).mean(2)
mean /= len(dataset)
mean_expanded=mean.view(mean.size(0),mean.size(1),1,1).expand_as(image)
for image, _ in loader:
std+=(image-mean_expanded).pow(2).sum(3).sum(2)
std=(std/(len(dataset)*image.size(2)*image.size(3)-1)).sqrt()
return mean, std
########################################################################################################################
def fisher_matrix_diag(t,x,y,model,criterion,sbatch=5, pass_t = False):
# Init
fisher={}
if(x.size(0) < 500):
sbatch = 1
for n,p in model.named_parameters():
fisher[n]=0*p.data
# Compute
model.train()
samples_taken = 0
for i in tqdm(range(0,x.size(0),sbatch),desc='Fisher diagonal',ncols=100,ascii=True):
b=torch.LongTensor(np.arange(i,np.min([i+1,x.size(0)]))).cuda()
images=torch.autograd.Variable(x[b],volatile=False)
target=torch.autograd.Variable(y[b],volatile=False)
# Forward and backward
model.zero_grad()
if not pass_t:
outputs=model.forward(images)
else:
outputs=model.forward(t, images)
loss=criterion(t,outputs[t],target)
loss.backward()
samples_taken += 1
# Get gradients
for n,p in model.named_parameters():
if p.grad is not None:
fisher[n]+=1*p.grad.data.pow(2)
# Mean
for n,_ in model.named_parameters():
fisher[n]=fisher[n]/samples_taken
fisher[n]=torch.autograd.Variable(fisher[n],requires_grad=False)
return fisher
def l2_reg(appr):
loss_reg=0
for name,param in appr.model.named_parameters():
loss_reg+=torch.sum(param**2)/2
return loss_reg/80
########################################################################################################################
def cross_entropy(outputs,targets,exp=1,size_average=True,eps=1e-5):
out=torch.nn.functional.softmax(outputs)
tar=torch.nn.functional.softmax(targets)
if exp!=1:
out=out.pow(exp)
out=out/out.sum(1).view(-1,1).expand_as(out)
tar=tar.pow(exp)
tar=tar/tar.sum(1).view(-1,1).expand_as(tar)
out=out+eps/out.size(1)
out=out/out.sum(1).view(-1,1).expand_as(out)
ce=-(tar*out.log()).sum(1)
if size_average:
ce=ce.mean()
return ce
########################################################################################################################
def set_req_grad(layer,req_grad):
if hasattr(layer,'weight'):
layer.weight.requires_grad=req_grad
if hasattr(layer,'bias'):
layer.bias.requires_grad=req_grad
return
########################################################################################################################
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
########################################################################################################################
def compute_kl(mean, exp_var, prior_mean, prior_exp_var, sum = True, lamb = 1, initial_prior_var = 0.0357**2):
trace_term = torch.exp(exp_var - prior_exp_var)
if lamb != 1:
mean_term = (mean - prior_mean)**2 * (lamb * torch.clamp(torch.exp(-prior_exp_var) - (1/initial_prior_var), min = 0.0) + (1/initial_prior_var))
else:
mean_term = (mean - prior_mean)**2 * torch.exp(-prior_exp_var)
det_term = prior_exp_var - exp_var
if sum:
return 0.5 * torch.sum(trace_term + mean_term + det_term - 1)
else:
return 0.5 * (trace_term + mean_term + det_term - 1)
########################################################################################################################
def print_log_acc_bwt(acc, lss):
print('*'*100)
print('Accuracies =')
for i in range(acc.shape[0]):
print('\t',end=',')
for j in range(acc.shape[1]):
print('{:5.4f}% '.format(acc[i,j]),end=',')
print()
avg_acc = np.mean(acc[acc.shape[0]-1,:])
print ('ACC: {:5.4f}%'.format(avg_acc))
print()
print()
bwt = (acc[-1] - np.diag(acc)).mean()
print ('BWT : {:5.2f}%'.format(bwt))
print('*'*100)
print('Done!')
return avg_acc, bwt
########################################################################################################################
class logger(object):
def __init__(self, file_name='pmnist2', resume=True, path='./result_data/csvdata/', data_format='csv'):
self.data_name = os.path.join(path, file_name)
self.data_path = '{}.csv'.format(self.data_name)
self.log = None
if os.path.isfile(self.data_path):
if resume:
self.load(self.data_path)
else:
os.remove(self.data_path)
self.log = pd.DataFrame()
else:
self.log = pd.DataFrame()
if not os.path.isdir("." + self.data_path[1:self.data_path.rindex("/")+ 1]):
os.makedirs("." + self.data_path[1:self.data_path.rindex("/")+ 1])
self.data_format = data_format
def add(self, **kwargs):
"""Add a new row to the dataframe
example:
resultsLog.add(epoch=epoch_num, train_loss=loss,
test_loss=test_loss)
"""
df = pd.DataFrame([kwargs.values()], columns=kwargs.keys())
self.log = self.log.append(df, ignore_index=True)
def save(self):
return self.log.to_csv(self.data_path, index=False, index_label=False)
def load(self, path=None):
path = path or self.data_path
if os.path.isfile(path):
self.log = pd.read_csv(path)
else:
raise ValueError('{} isn''t a file'.format(path))