-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
177 lines (155 loc) · 6.29 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
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
import torch
import numpy as np
from typing import List, Mapping, Optional
import torch.nn.functional as F
def one_hot(y, num_class):
return torch.zeros((len(y), num_class)).scatter_(1, y.unsqueeze(1), 1)
def DBindex(cl_data_file):
class_list = cl_data_file.keys()
cl_num= len(class_list)
cl_means = []
stds = []
DBs = []
for cl in class_list:
cl_means.append( np.mean(cl_data_file[cl], axis = 0) )
stds.append( np.sqrt(np.mean( np.sum(np.square( cl_data_file[cl] - cl_means[-1]), axis = 1))))
mu_i = np.tile( np.expand_dims( np.array(cl_means), axis = 0), (len(class_list),1,1) )
mu_j = np.transpose(mu_i,(1,0,2))
mdists = np.sqrt(np.sum(np.square(mu_i - mu_j), axis = 2))
for i in range(cl_num):
DBs.append( np.max([ (stds[i]+ stds[j])/mdists[i,j] for j in range(cl_num) if j != i ]) )
return np.mean(DBs)
def sparsity(cl_data_file):
class_list = cl_data_file.keys()
cl_sparsity = []
for cl in class_list:
cl_sparsity.append(np.mean([np.sum(x!=0) for x in cl_data_file[cl] ]) )
return np.mean(cl_sparsity)
def rand_n_points(points, n_points): # points: (n_sample,n_points,3)
points = points.transpose(1,0,2)
np.random.shuffle(points)
return points[:n_points,:,:].transpose(1,0,2)
#from snorkel
Outputs = Mapping[str, List[torch.Tensor]]
def cross_entropy_with_probs(
input: torch.Tensor,
target: torch.Tensor,
weight: Optional[torch.Tensor] = None,
reduction: str = "mean",
) -> torch.Tensor:
"""Calculate cross-entropy loss when targets are probabilities (floats), not ints.
PyTorch's F.cross_entropy() method requires integer labels; it does accept
probabilistic labels. We can, however, simulate such functionality with a for loop,
calculating the loss contributed by each class and accumulating the results.
Libraries such as keras do not require this workaround, as methods like
"categorical_crossentropy" accept float labels natively.
Note that the method signature is intentionally very similar to F.cross_entropy()
so that it can be used as a drop-in replacement when target labels are changed from
from a 1D tensor of ints to a 2D tensor of probabilities.
Parameters
----------
input
A [num_points, num_classes] tensor of logits
target
A [num_points, num_classes] tensor of probabilistic target labels
weight
An optional [num_classes] array of weights to multiply the loss by per class
reduction
One of "none", "mean", "sum", indicating whether to return one loss per data
point, the mean loss, or the sum of losses
Returns
-------
torch.Tensor
The calculated loss
Raises
------
ValueError
If an invalid reduction keyword is submitted
"""
num_points, num_classes = input.shape
# Note that t.new_zeros, t.new_full put tensor on same device as t
cum_losses = input.new_zeros(num_points)
for y in range(num_classes):
target_temp = input.new_full((num_points,), y, dtype=torch.long)
y_loss = F.cross_entropy(input, target_temp, reduction="none")
if weight is not None:
y_loss = y_loss * weight[y]
cum_losses += target[:, y].float() * y_loss
if reduction == "none":
return cum_losses
elif reduction == "mean":
return cum_losses.mean()
elif reduction == "sum":
return cum_losses.sum()
else:
raise ValueError("Keyword 'reduction' must be one of ['none', 'mean', 'sum']")
class CrossEntropyLoss_with_prob(torch.nn.Module):
def __init__(self):
super.__init__()
def forward(self, input, target):
return cross_entropy_with_probs(input, target)
def nll_with_probs(
input: torch.Tensor,
target: torch.Tensor,
weight: Optional[torch.Tensor] = None,
reduction: str = "mean",
) -> torch.Tensor:
"""Calculate cross-entropy loss when targets are probabilities (floats), not ints.
PyTorch's F.cross_entropy() method requires integer labels; it does accept
probabilistic labels. We can, however, simulate such functionality with a for loop,
calculating the loss contributed by each class and accumulating the results.
Libraries such as keras do not require this workaround, as methods like
"categorical_crossentropy" accept float labels natively.
Note that the method signature is intentionally very similar to F.cross_entropy()
so that it can be used as a drop-in replacement when target labels are changed from
from a 1D tensor of ints to a 2D tensor of probabilities.
Parameters
----------
input
A [num_points, num_classes] tensor of logits
target
A [num_points, num_classes] tensor of probabilistic target labels
weight
An optional [num_classes] array of weights to multiply the loss by per class
reduction
One of "none", "mean", "sum", indicating whether to return one loss per data
point, the mean loss, or the sum of losses
Returns
-------
torch.Tensor
The calculated loss
Raises
------
ValueError
If an invalid reduction keyword is submitted
"""
num_points, num_classes = input.shape
# Note that t.new_zeros, t.new_full put tensor on same device as t
cum_losses = input.new_zeros(num_points)
for y in range(num_classes):
target_temp = input.new_full((num_points,), y, dtype=torch.long)
y_loss = F.nll_loss(input, target_temp, reduction="none")
if weight is not None:
y_loss = y_loss * weight[y]
cum_losses += target[:, y].float() * y_loss
if reduction == "none":
return cum_losses
elif reduction == "mean":
return cum_losses.mean()
elif reduction == "sum":
return cum_losses.sum()
else:
raise ValueError("Keyword 'reduction' must be one of ['none', 'mean', 'sum']")
def get_val_acc(experiment):
val_acc = []
test_acc = 0
main_dir = '/home/yunlu/ivi/fscnobeta/'
main_dir = '/home/yunlu/ivi/fscbeta/'
main_dir = '/home/yunlu/ivi/fsc/'
log_dir = main_dir+experiment+"/log.log"
fp = open(log_dir, 'r')
with open(log_dir, 'r') as fp:
for line in fp:
if line.find('val Acc = ')>-1:
val_acc.append(float(line[line.find('val Acc = ')+10: line.find('val Acc = ')+15]))
return val_acc