-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.py
146 lines (120 loc) · 3.89 KB
/
misc.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
import torch
import torch.nn as nn
from torchdiffeq import odeint_adjoint as odeint
import torchdiffeq
import numpy as np
from einops import rearrange, repeat
import time
import torch.optim as optim
import glob
import imageio
from math import pi
from random import random
from torch.utils.data import Dataset, DataLoader
from torch.distributions import Normal
from torchvision import datasets, transforms
import sys
from matplotlib import pyplot as plt
import pickle
import csv
# Format [time, batch, diff, vector]
# tol = 1e-3
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def shrink_parameters(model, ratio):
model_dict = model.state_dict()
for i in model_dict:
model_dict[i] *= ratio
model.load_state_dict(model_dict)
return model
def gradnorm(model, p=2):
param_normp = [param.grad.data.norm(p) ** p for param in model.parameters() if param.grad is not None]
total_normp = sum(param_normp)
total_norm = total_normp ** (1 / p)
return total_norm
class ArgumentParser:
def add_argument(self, str, type, default):
setattr(self, str[2:], default)
def parse_args(self):
return self
def str_rec(names, data, unit=None, sep=', ', presets='{}'):
if unit is None:
unit = [''] * len(names)
out_str = "{{}}: {} {{{{}}}}" + sep
out_str *= len(names)
out_str = out_str.format(*data)
out_str = out_str.format(*names)
out_str = out_str.format(*unit)
out_str = presets.format(out_str)
return out_str
def to_float(arr, truncate=False):
if isinstance(arr, list):
return [to_float(i, truncate=truncate) for i in arr]
if arr is None:
return None
if isinstance(arr, torch.Tensor):
arr = arr.detach().cpu().numpy()
if isinstance(arr, np.ndarray):
arr = arr.flatten()[0]
if truncate:
arr = int(arr * 10 ** truncate) / 10 ** truncate
return arr
class EmptyClass:
pass
class Recorder:
def __init__(self):
self.store = []
self.current = dict()
def __setitem__(self, key, value):
for method in ['detach', 'cpu', 'numpy']:
if hasattr(value, method):
value = getattr(value, method)()
if key in self.current:
self.current[key].append(value)
else:
self.current[key] = [value]
def capture(self, verbose=False):
for i in self.current:
self.current[i] = np.mean(self.current[i])
self.store.append(self.current.copy())
self.current = dict()
if verbose:
for i in self.store[-1]:
if i[0] != '_':
print('{}: {}'.format(i, self.store[-1][i]))
return self.store[-1]
def tolist(self):
labels = set()
labels = sorted(labels.union(*self.store))
outlist = []
for obs in self.store:
outlist.append([obs.get(i, np.nan) for i in labels])
return labels, outlist
def writecsv(self, writer):
labels, outlist = self.tolist()
if isinstance(writer, str):
outfile = open(writer, 'w')
csvwriter = csv.writer(outfile)
csvwriter.writerow(labels)
csvwriter.writerows(outlist)
outfile.close()
else:
csvwriter = writer
csvwriter.writerow(labels)
csvwriter.writerows(outlist)
class NLayerNN(nn.Module):
def __init__(self, *args, actv=nn.ReLU()):
super().__init__()
self.linears = nn.ModuleList()
for i in range(len(args)):
self.linears.append(nn.Linear(args[i], args[i+1]))
self.actv = actv
def forward(self, x):
for i in range(self.layer_cnt):
x = self.linears[i](x)
if i < self.layer_cnt - 1:
x = self.actv(x)
return x
@property
def layer_cnt(self):
return len(self.linears)