-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
80 lines (62 loc) · 2.13 KB
/
helpers.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
import torch
import math
import colorsys
import numpy as np
# WaterCan helpers
def derivate(f,x):
return torch.autograd.grad(f, x, grad_outputs=torch.ones_like(f), create_graph=True)[0]
def high_derivate(f,x,ord,cumul):
if cumul: derivatives=[f]
for _ in range(ord):
f=derivate(f,x)
if cumul: derivatives.append(f)
if cumul:
return derivatives
else:
return f
def derivate_vector(fs,x):
fs_d=[]
for f in fs:
fs_d.append(derivate(f,x))
return fs_d
def gpu2np(x):
return x.cpu().detach().numpy()
def np2gpu(x,device):
x=torch.tensor(x, dtype=torch.float32).to(device)#.double()
return x
def get_from(dict,key_prop):
return {key:perform[key_prop] for key,perform in dict.items()}
# Visualization
def generate_colors(n):
colors = []
for i in range(n):
hue = i / n # Evenly space colors by hue
saturation = 0.5 + 0.5 * (i % 2) # Alternate saturation for more distinct colors
value = 0.95 # Keep the value high for brightness
rgb = colorsys.hsv_to_rgb(hue, saturation, value)
colors.append(tuple(rgb))
return colors
## Method
def min_with_index(lst):
# Find the minimum value and its index
min_val, min_arg = min((val, idx) for idx, val in enumerate(lst))
return min_val, min_arg
def create_random_row_canonical_matrix(m, n):
indices = np.random.randint(0, n, size=m)
canonical_matrix = np.zeros((m, n))
rows = np.arange(m)
canonical_matrix[rows, indices] = 1.
return canonical_matrix
def uncenter_var(x,center,n):
return np.squeeze(np.sum((x[np.newaxis, :, :] - center[:, np.newaxis, :])**2, axis=1) / (n - 1))
def euclidean_uncenter_var(x,center,n):
distances=np.mean((x[np.newaxis, :, :] - center[:, np.newaxis, :])**2, axis=-1)
variance = np.sum(distances,axis=-1)/ (n - 1)
return np.squeeze(variance)
def euclidean_var(x,n):
center=np.mean(x,axis=0,keepdims=True)
return euclidean_uncenter_var(x,center,n)
def scoring(true,pred,a=1/13,b=1/10):
delta=true-pred
exp=np.exp(a*delta)*(delta > 0)+np.exp(-b*delta)*(delta <= 0)-1
return np.sum(exp)