-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
356 lines (291 loc) · 16 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import time
import numpy as np
import torch
import importlib
from sklearn.model_selection import train_test_split
from sklearn.utils import shuffle
from tqdm import tqdm
import sys
sys.path.append('../Datasets')
# import ALA_mon as ott
# import ALA_nonmon as ott
import ALA_nonmon_mon as ott
torch.set_default_dtype(torch.double)
is_cuda = torch.cuda.is_available()
if is_cuda:
device = torch.device("cuda")
else:
device = torch.device("cpu")
import torch.nn as nn
class dataset:
data = []
Xtr = []
ytr = []
def __init__(self,name,random_state):
self.name = name
self.random_state = random_state
self.data = importlib.import_module(name)
self.Xtr, Xts, self.ytr, yts = train_test_split(self.data.X_train,
self.data.y_train,train_size=0.80,
random_state=random_state)
# self.Xtr, self.ytr = shuffle(self.data.X_train, self.data.y_train, random_state=random_state)
trunc = ["Nash", "Dembo"]
curv = ["Curv", "Nocurv"]
list_data_names = ["adult", "ailerons", "appliances", "arcene", "blogfeed",
"boston_house", "breast_cancer", "cifar10", "gisette",
"iris", "mnist", "mv", "qsar"]
random_seeds = [1, 2, 3, 4]
neurons = [30, 40, 50]
for t in trunc:
for c in curv:
fid = open(t + "_" + c + '.txt','a')
fid.truncate(0)
print(' Name & perm & nneu & K & KTOT & Nf & Ng & Nint & f_0 & f & g_norm & time\\\\\\\hline',file=fid)
fid.close()
for name in list_data_names:
for r in random_seeds:
for nneu in neurons:
data = dataset(name,r)
# print("Test ", name, " Seed ", r, "Neurons ", nneu, " ", c, " ", t)
X_train, y_train = map(torch.tensor, (data.Xtr, data.ytr))
X_train = X_train.double()
y_train = y_train.double()
# y_train = y_train.view(-1, 1).long()
X_train = X_train.to(device)
y_train = y_train.to(device)
ntrain,input_dim = X_train.shape
ntrain,out_dim = y_train.shape
# print(X_train.shape)
# print(y_train.shape)
# print(out_dim)
torch.cuda.manual_seed(r)
torch.manual_seed(r)
class Net(nn.Module):
def __init__(self, dims):
super(Net, self).__init__()
self.nhid = len(dims)
self.dims = dims
self.fc = nn.ModuleList().double()
for i in range(self.nhid - 1):
linlay = nn.Linear(dims[i], dims[i+1]).double().to(device)
# linlay = nn.Linear(dims[i], dims[i+1], bias = False).double().to(device)
self.fc.append(linlay)
def forward(self, x):
b = torch.tensor([], device = device, dtype=torch.double)
for i in range(self.nhid - 2):
x = self.fc[i](x)
#x = torch.relu(x)
# x = torch.sigmoid(x)
x = torch.tanh(x)
# x = swish(x)
x = self.fc[self.nhid - 2](x)
# x = torch.softmax(x, dim = 1)
b = torch.cat((b,x))
return b
def swish(x):
return torch.mul(x, sigmoid(x))
def sigmoid(x):
return torch.where(x >= 0, _positive_sigm(x), _negative_sigm(x))
def _negative_sigm(x):
expon = torch.exp(-x)
return 1 / (1 + expon)
def _positive_sigm(x):
expon = torch.exp(x)
return expon / (1 + expon)
def init_weights(m):
if type(m) == nn.Linear:
torch.nn.init.uniform_(m.weight.data,a=-1.0,b=1.0).double()
torch.nn.init.uniform_(m.bias.data,a=-1.0,b=1.0).double()
def cross_entropy(y_hat, y):
return torch.mean(- torch.log(y_hat[range(len(y_hat)), y.view(-1,)])).double()
MSELoss = torch.nn.MSELoss()
def my_loss(X, y):
y_hat = net(X).double()
loss = MSELoss(y_hat,y)
# loss = cross_entropy(y_hat, y)
return loss
def my_loss_reg(X, y, ro):
y_hat = net(X).double()
loss = MSELoss(y_hat,y)
# loss = cross_entropy(y_hat, y)
l2_reg = torch.tensor(0.0, device = device, dtype=torch.double)
for param in net.parameters():
l2_reg += torch.norm(param)**2
loss += ro * l2_reg
return loss
#################################
# define the variable array for
# NWTNM optimizer
#################################
def dim():
n = 0
for k,v in net.state_dict().items():
n += v.numel()
return n
def startp(n1):
x = torch.zeros(n1,dtype=torch.double,requires_grad=True)
torch.nn.init.normal_(x).double()
return x.detach().to(device)
def set_x(x):
state_dict = net.state_dict()
i = 0
for k,v in state_dict.items():
lpart = v.numel()
x[i:i+lpart] = state_dict[k].reshape(lpart).double()
i += lpart
def funct(x):
state_dict = net.state_dict()
i = 0
for k,v in state_dict.items():
lpart = v.numel()
state_dict[k] = x[i:i+lpart].reshape(v.shape).double()
i += lpart
net.load_state_dict(state_dict)
l_train = my_loss(X_train, y_train)
# l_train = my_loss_reg(X_train, y_train, l2_lambda)
return l_train
def grad(x):
for param in net.parameters():
if param.requires_grad:
if not type(param.grad) is type(None):
param.grad.zero_()
param.requires_grad_()
f = funct(x)
f.backward()
if False:
g = x.clone().detach()
i = 0
for v in net.parameters():
if v.requires_grad:
lpart = v.numel()
d = v.grad.reshape(lpart)
g[i:i+lpart] = d
i += lpart
views = []
for p in net.parameters():
if p.requires_grad:
view = p.grad.view(-1)
views.append(view)
g1 = torch.cat(views, 0).to(device)
return g1
def hessdir2(x,d):
if False:
state_dict = net.state_dict()
i = 0
for k,v in state_dict.items():
lpart = v.numel()
state_dict[k] = x[i:i+lpart].reshape(v.shape).double()
i += lpart
net.load_state_dict(state_dict)
for param in net.parameters():
if param.requires_grad:
if not type(param.grad) is type(None):
param.grad.zero_()
param.requires_grad_()
grads = torch.autograd.grad(outputs=funct(x), inputs=net.parameters(), create_graph=True)
dot = nn.utils.parameters_to_vector(grads).mul(d).sum()
grads = [g.contiguous() for g in torch.autograd.grad(dot, net.parameters(), retain_graph = True)]
return nn.utils.parameters_to_vector(grads)
'''
in hessdir3 a seconda del valore di goth:
FALSE -> si calcola gradstore e lo si memorizza
TRUE -> si usa gradstore salvato senza ricalcolarlo
'''
def hessdir3(x,d,goth):
for param in net.parameters():
if param.requires_grad:
if not type(param.grad) is type(None):
param.grad.zero_()
param.requires_grad_()
if not goth:
hessdir3.gradstore = torch.autograd.grad(outputs=funct(x), inputs=net.parameters(), create_graph=True)
dot = nn.utils.parameters_to_vector(hessdir3.gradstore).mul(d).sum()
grads = [g.contiguous() for g in torch.autograd.grad(dot, net.parameters(), retain_graph = True)]
return nn.utils.parameters_to_vector(grads)
# which_algo = 'lbfgs'
# which_algo = 'sgd'
which_algo = 'troncato'
maxiter = 10000
maxtim = 1800
l2_lambda = 1e-05
nrnd = 1
tolmax = 1.e-5
tolchmax = 1.e-9
iprint = 0 # -1
satura = True
hd_exact = True
# print()
# print("----------------------------------------------")
# print(" define a neural net to be minimized ")
# print("----------------------------------------------")
# print()
# n_class = 10
# dims = [input_dim, hidden_1, n_class]
dims = [input_dim, nneu, out_dim]
net = Net(dims).double().to(device)
for irnd in range(nrnd):
net.apply(init_weights)
# print(net)
# print(net.parameters())
n = dim()
x = startp(n)
set_x(x)
l_train = funct(x)
nabla_l_train = grad(x)
gnorm = nabla_l_train.norm().item()
# print("numero di parametri totali: n=", n, "nneu: ", nneu,
# "loss: ", l_train.item(), " gloss: ", gnorm)
tol = tolmax
tolch = tolchmax
with tqdm(total=maxiter) as pbar:
ng = 0
ni = 0
def fun_closure(x):
global ni
deltai = ott.n_iter - ni
pbar.update(deltai)
ni = ott.n_iter
l_train = funct(x)
return l_train
def closure():
global ng
global ni
optimizer.zero_grad()
loss1 = my_loss(X_train, y_train)
# loss1 = my_loss_reg(X_train, y_train, l2_lambda)
ng += 1
deltai = optimizer.state_dict()['state'][0]['n_iter'] - ni
pbar.update(deltai)
ni = optimizer.state_dict()['state'][0]['n_iter']
loss1.backward()
return loss1
def closure_sgd(ni):
optimizer.zero_grad()
loss1 = my_loss(X_train, y_train)
# loss1 = my_loss_reg(X_train, y_train, l2_lambda)
pbar.update(1)
loss1.backward()
return loss1
if which_algo == 'lbfgs':
timelbfgs = time.time()
optimizer = torch.optim.LBFGS(net.parameters(), lr=1, max_iter=maxiter, max_eval=None, tolerance_grad=tol,
tolerance_change=tolch, history_size=10, line_search_fn="strong_wolfe")
optimizer.step(closure)
niter = optimizer.state_dict()['state'][0]['n_iter']
timelbfgs_tot = time.time() - timelbfgs
timeparz = timelbfgs_tot
elif which_algo == 'troncato':
ott.n_iter = 0
f_0 = funct(x)
#fstar,xstar,niter,nf,ng,nneg,timeparz = ott.NWTNM(fun_closure,grad,hessdir3,x,tol,maxiter,maxtim,iprint,satura,hd_exact)
fstar,xstar,niter,nf,ng,nneg,timeparz = ott.NWTNM(funct,grad,hessdir3,x,tol,maxiter,maxtim,iprint,satura,hd_exact,name,r,nneu,c,t,f_0)
elif which_algo == 'sgd':
optimizer = torch.optim.SGD(net.parameters(), lr=0.01)
timesgd = time.time()
niter = maxiter
for it in range(0, niter):
closure_sgd(it)
optimizer.step()
timeparz = time.time() - timesgd
pbar.close()
# print("KTOT:",niter,"time:",timeparz,"fstar:",fstar.item())