-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
64 lines (51 loc) · 1.76 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
import math
import numpy as np
import torch
def get_flat_params_from(model):
params = []
for param in model.parameters():
params.append(param.data.view(-1))
flat_params = torch.cat(params)
return flat_params
def set_flat_params_to(model, flat_params):
prev_ind = 0
for param in model.parameters():
flat_size = int(np.prod(list(param.size())))
param.data.copy_(
flat_params[prev_ind:prev_ind + flat_size].view(param.size()))
prev_ind += flat_size
def get_flat_grad_from(net, grad_grad=False):
grads = []
for param in net.parameters():
if grad_grad:
grads.append(param.grad.grad.view(-1))
else:
grads.append(param.grad.view(-1))
flat_grad = torch.cat(grads)
return flat_grad
def saveTensorCsv(t,filename):
import csv
with open(filename, mode='w') as file:
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in range(t.size()[0]):
writer.writerow(t[i].detach().numpy().tolist())
def loadTensorCsv(filename):
import csv
with open(filename) as file:
reader = csv.reader(file, delimiter=',')
t = []
for row in reader:
t.append([float(x) for x in row])
return torch.Tensor(t)
def saveParameterCsv(param,filename):
import csv
with open(filename, mode='w') as file:
writer = csv.writer(file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(param.detach().numpy().tolist())
def loadParameterCsv(filename):
import csv
with open(filename) as file:
reader = csv.reader(file, delimiter=',')
for row in reader:
param = [float(x) for x in row]
return torch.Tensor(param)