-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.py
93 lines (73 loc) · 2.12 KB
/
functions.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
import numpy as np
import torch
import torch.nn.functional as F
import time
from typing import Dict
def get_device():
if torch.cuda.is_available():
DEVICE = 'cuda'
torch.set_default_tensor_type(torch.cuda.FloatTensor)
elif torch.backends.mps.is_available():
DEVICE = torch.device("mps")
else:
DEVICE = 'cpu'
print('Using {}'.format(DEVICE))
return DEVICE
def get_time() -> int:
'''Returns current time in ms'''
return int(round(time.time() * 1000))
class Timer:
def __init__(self):
self.reset()
def lap(self):
self.t_lap = get_time()
def get(self):
return get_time() - self.t_lap
def reset(self):
self.t_total = get_time()
self.t_lap = get_time()
def __str__(self):
t = self.get()
ms = t % 1000
t = int(t / 1000)
s = t % 60
t = int(t / 60)
m = t % 60
if t == 0:
return "{}.{:03}".format(s,ms)
else:
t = int(t / 60)
h = t
if t == 0:
return "{}:{:02}.{:03}".format(m,s,ms)
else:
return "{}:{:02}:{:02}.{:03}".format(h,m,s,ms)
def append_dict(dict_a:Dict[str,np.ndarray], dict_b:Dict[str,np.ndarray]):
for k, v in dict_b.items():
dict_a[k] = np.concatenate((dict_a[k], v))
def L1Loss(x:torch.FloatTensor):
return torch.mean(torch.abs(x))
def L2Loss(x:torch.FloatTensor):
return torch.mean(torch.pow(x, 2))
def Linear(x:torch.FloatTensor):
return x
def parse_loss(args, terms):
if args == None:
return L1Loss, torch.tensor(0.0)
pre, post, weights = terms
arg1, arg2 = args.split('_')
if arg1 == 'l1':
loss_fn = L1Loss
else:
loss_fn = L2Loss
if arg2 == 'pre':
loss_arg = pre
elif arg2 == 'post':
loss_arg = post
else:
loss_arg = weights
return loss_fn, loss_arg
def init_params(size_x, size_y):
return ((torch.rand(size_x, size_y) * 2.) - 1. ) * np.sqrt(1. / size_x)
def normalize(x, p=2.0, dim=1):
return F.normalize(x, p, dim)