-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloss.py
406 lines (270 loc) · 17.5 KB
/
loss.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import numpy as np
import networkx as nx
import tensorflow as tf
from tensorflow import keras
from taxonomy import source_node_label, get_taxonomy_tree
from RNN_model import get_RNN_model
from dataloader import ts_length
class WHXE_Loss:
# Implementation of Weighted Hierarchical Cross Entropy loss function by Villar et. al. 2023 (https://arxiv.org/abs/2312.02266)
def __init__(self, tree, leaf_labels, alpha = 0.5) -> None:
# Free parameter that determines how much weight is given to different levels in the hierarchy
self.alpha = alpha
# The leaf labels of all the objects in the data set, used to compute the class weights
self.leaf_labels = leaf_labels
# The taxonomy tree is used for computing soft max at different levels and computing the WHXE loss
self.tree = tree
# Do a level order traversal of the tree to get an ordering of the nodes
self.level_order_nodes = nx.bfs_tree(self.tree, source=source_node_label).nodes()
# Compute and store parents for each node, ordered by level order traversal of the tree
self.compute_parents()
# Compute and store the masks used for soft max
self.create_masks()
# Compute and store the path lengths from the root to each node in the tree, ordered by level order traversal of the tree
self.compute_path_lengths()
# Compute the class weights, ordered by level order traversal of the tree
self.compute_class_weights()
def compute_class_weights(self):
# Count the total number of events in the data set
N_all = len(self.leaf_labels)
# Count the number of labels in the taxonomy
N_labels = len(self.level_order_nodes)
# Maintain a dictionary to count the number of occurrences of each node in the tree
counts_dict = {}
for node in self.level_order_nodes:
counts_dict[node] = 0
# Iterate through all the leaf labels in the data set
for labels in self.leaf_labels:
# Compute the path down the taxonomy for each element in the data set
path = nx.shortest_path(self.tree, source=source_node_label, target=labels)
# Iterate the counter for the node whenever the node appears in the path.
for step in path:
counts_dict[step] += 1
# Array to store the number of occurrences of each node in the taxonomy for the given data set, ordered by level order traversal of the tree
N_c = []
for node in self.level_order_nodes:
N_c.append(counts_dict[node])
# Compute the final weights, ordered by level order traversal of the tree
self.class_weights = N_all / (N_labels * np.array(N_c))
def compute_parents(self):
# Find the parent for each node in the tree, ordered using level order traversal
self.parents = [list(self.tree.predecessors(node)) for node in self.level_order_nodes]
for idx in range(len(self.parents)):
# Make sure the graph is a tree.
assert len(self.parents[idx]) == 0 or len(self.parents[idx]) == 1, 'Number of parents for each node should be 0 (for root) or 1.'
if len(self.parents[idx]) == 0:
self.parents[idx] = ''
else:
self.parents[idx] = self.parents[idx][0]
def create_masks(self):
# Finding unique parents for masking
unique_parents = list(set(self.parents))
unique_parents.sort()
# Create masks for applying soft max and calculating loss values.
self.masks = []
for parent in unique_parents:
self.masks.append(np.where(np.array(self.parents) == parent,1,0))
def compute_path_lengths(self):
# Compute the shortest paths from the root node to each of the other nodes in the tree.
self.path_lengths = []
for node in self.level_order_nodes:
self.path_lengths.append(len(nx.shortest_path(self.tree, source_node_label, node)) - 1)
self.path_lengths = np.array(self.path_lengths)
# Compute the secondary weight term, which emphasizes different levels of the tree. See paper for more details.
self.lambda_term = np.exp(-self.alpha * self.path_lengths)
@keras.saving.register_keras_serializable(package="my_package", name="custom_fn")
def compute_loss(self, target_probabilities, y_pred, epsilon=1e-10):
# Go through set of siblings
for mask in self.masks:
# Get the e^logits
exps = tf.math.exp(y_pred)
# Multiply (dot product) the e^logits with the mask to maintain just the e^logits values that belong to this mask. All other values will be zeros.
masked_exps = tf.math.multiply(exps, mask)
# Find the sum of the e^logits values that belong to the mask. Do this for each element in the batch separately. Add a small value to avoid numerical problems with floating point numbers.
masked_sums = tf.math.reduce_sum(masked_exps, axis=1, keepdims=True) + epsilon
# Compute the softmax by dividing the e^logits with the sume (e^logits)
softmax = masked_exps/masked_sums
# (1 - mask) * y_pred gets the logits for all the values not in this mask and zeros out the values in the mask. Add those back so that we can repeat the process for other masks.
y_pred = softmax + ((1 - mask) * y_pred)
# At this point we have the masked softmaxes i.e. the pseudo probabilities. We can take the log of these values
y_pred = tf.math.log(y_pred)
# Weight them by the level at which the corresponding node appears in the hierarchy
y_pred = y_pred * self.lambda_term
# Weight them by the class weight after using the target_probabilities as indicators. Then sum them up for each batch
v1 = tf.math.reduce_sum(self.class_weights * (y_pred * target_probabilities), axis=1)
# Finally, find the mean over all batches. Since we are taking logs of numbers <1 (the pseudo probabilities), we have to multiply by -1 to get a +ve loss value.
v2 = -1 * tf.math.reduce_mean(v1)
return v2
class HXE_Loss:
# Implementation of Weighted Hierarchical Cross Entropy loss function by Villar et. al. 2023 (https://arxiv.org/abs/2312.02266)
def __init__(self, tree, alpha = 0.5) -> None:
# Free parameter that determines how much weight is given to different levels in the hierarchy
self.alpha = alpha
# The taxonomy tree is used for computing soft max at different levels and computing the WHXE loss
self.tree = tree
# Do a level order traversal of the tree to get an ordering of the nodes
self.level_order_nodes = nx.bfs_tree(self.tree, source=source_node_label).nodes()
# Compute and store parents for each node, ordered by level order traversal of the tree
self.compute_parents()
# Compute and store the masks used for soft max
self.create_masks()
# Compute and store the path lengths from the root to each node in the tree, ordered by level order traversal of the tree
self.compute_path_lengths()
def compute_parents(self):
# Find the parent for each node in the tree, ordered using level order traversal
self.parents = [list(self.tree.predecessors(node)) for node in self.level_order_nodes]
for idx in range(len(self.parents)):
# Make sure the graph is a tree.
assert len(self.parents[idx]) == 0 or len(self.parents[idx]) == 1, 'Number of parents for each node should be 0 (for root) or 1.'
if len(self.parents[idx]) == 0:
self.parents[idx] = ''
else:
self.parents[idx] = self.parents[idx][0]
def create_masks(self):
# Finding unique parents for masking
unique_parents = list(set(self.parents))
unique_parents.sort()
# Create masks for applying soft max and calculating loss values.
self.masks = []
for parent in unique_parents:
self.masks.append(np.where(np.array(self.parents) == parent,1,0))
def compute_path_lengths(self):
# Compute the shortest paths from the root node to each of the other nodes in the tree.
self.path_lengths = []
for node in self.level_order_nodes:
self.path_lengths.append(len(nx.shortest_path(self.tree, source_node_label, node)) - 1)
self.path_lengths = np.array(self.path_lengths)
# Compute the secondary weight term, which emphasizes different levels of the tree. See paper for more details.
self.lambda_term = np.exp(-self.alpha * self.path_lengths)
@keras.saving.register_keras_serializable(package="my_package", name="custom_fn")
def compute_loss(self, target_probabilities, y_pred, epsilon=1e-10):
# Go through set of siblings
for mask in self.masks:
# Get the e^logits
exps = tf.math.exp(y_pred)
# Multiply (dot product) the e^logits with the mask to maintain just the e^logits values that belong to this mask. All other values will be zeros.
masked_exps = tf.math.multiply(exps, mask)
# Find the sum of the e^logits values that belong to the mask. Do this for each element in the batch separately. Add a small value to avoid numerical problems with floating point numbers.
masked_sums = tf.math.reduce_sum(masked_exps, axis=1, keepdims=True) + epsilon
# Compute the softmax by dividing the e^logits with the sume (e^logits)
softmax = masked_exps/masked_sums
# (1 - mask) * y_pred gets the logits for all the values not in this mask and zeros out the values in the mask. Add those back so that we can repeat the process for other masks.
y_pred = softmax + ((1 - mask) * y_pred)
# At this point we have the masked softmaxes i.e. the pseudo probabilities. We can take the log of these values
y_pred = tf.math.log(y_pred)
# Weight them by the level at which the corresponding node appears in the hierarchy
y_pred = y_pred * self.lambda_term
# Use the target_probabilities as indicators. Then sum them up for each batch
v1 = tf.math.reduce_sum(y_pred * target_probabilities, axis=1)
# Finally, find the mean over all batches. Since we are taking logs of numbers <1 (the pseudo probabilities), we have to multiply by -1 to get a +ve loss value.
v2 = -1 * tf.math.reduce_mean(v1)
return v2
class PAWHXE_Loss:
# Implementation of a Phase Aware Weighted Hierarchical Cross Entropy loss function by yours truly et al. (2024, or 2025 if I get lazy.....sigh)
def __init__(self, tree, leaf_labels, alpha = 0.5, beta=0.1) -> None:
# Free parameter that determines how much weight is given to different levels in the hierarchy
self.alpha = alpha
# Free parameter that determines how quickly the alpha parameter decays as a function of % of the light curve observed
self.beta = beta
# The leaf labels of all the objects in the data set, used to compute the class weights
self.leaf_labels = leaf_labels
# The taxonomy tree is used for computing soft max at different levels and computing the WHXE loss
self.tree = tree
# Do a level order traversal of the tree to get an ordering of the nodes
self.level_order_nodes = nx.bfs_tree(self.tree, source=source_node_label).nodes()
# Compute and store parents for each node, ordered by level order traversal of the tree
self.compute_parents()
# Compute and store the masks used for soft max
self.create_masks()
# Compute and store the path lengths from the root to each node in the tree, ordered by level order traversal of the tree
self.compute_path_lengths()
# Compute the class weights, ordered by level order traversal of the tree
self.compute_class_weights()
def compute_class_weights(self):
# Count the total number of events in the data set
N_all = len(self.leaf_labels)
# Count the number of labels in the taxonomy
N_labels = len(self.level_order_nodes)
# Maintain a dictionary to count the number of occurrences of each node in the tree
counts_dict = {}
for node in self.level_order_nodes:
counts_dict[node] = 0
# Iterate through all the leaf labels in the data set
for labels in self.leaf_labels:
# Compute the path down the taxonomy for each element in the data set
path = nx.shortest_path(self.tree, source=source_node_label, target=labels)
# Iterate the counter for the node whenever the node appears in the path.
for step in path:
counts_dict[step] += 1
# Array to store the number of occurrences of each node in the taxonomy for the given data set, ordered by level order traversal of the tree
N_c = []
for node in self.level_order_nodes:
N_c.append(counts_dict[node])
# Compute the final weights, ordered by level order traversal of the tree
self.class_weights = N_all / (N_labels * np.array(N_c))
def compute_parents(self):
# Find the parent for each node in the tree, ordered using level order traversal
self.parents = [list(self.tree.predecessors(node)) for node in self.level_order_nodes]
for idx in range(len(self.parents)):
# Make sure the graph is a tree.
assert len(self.parents[idx]) == 0 or len(self.parents[idx]) == 1, 'Number of parents for each node should be 0 (for root) or 1.'
if len(self.parents[idx]) == 0:
self.parents[idx] = ''
else:
self.parents[idx] = self.parents[idx][0]
def create_masks(self):
# Finding unique parents for masking
unique_parents = list(set(self.parents))
unique_parents.sort()
# Create masks for applying soft max and calculating loss values.
self.masks = []
for parent in unique_parents:
self.masks.append(np.where(np.array(self.parents) == parent,1,0))
def compute_path_lengths(self):
# Compute the shortest paths from the root node to each of the other nodes in the tree.
self.path_lengths = []
for node in self.level_order_nodes:
self.path_lengths.append(len(nx.shortest_path(self.tree, source_node_label, node)) - 1)
self.path_lengths = np.array(self.path_lengths).astype(np.float32)
@keras.saving.register_keras_serializable(package="my_package", name="custom_fn")
def compute_loss(self, target_probabilities, y_pred, fractions, epsilon=1e-10):
# Go through set of siblings
for mask in self.masks:
# Get the e^logits
exps = tf.math.exp(y_pred)
# Multiply (dot product) the e^logits with the mask to maintain just the e^logits values that belong to this mask. All other values will be zeros.
masked_exps = tf.math.multiply(exps, mask)
# Find the sum of the e^logits values that belong to the mask. Do this for each element in the batch separately. Add a small value to avoid numerical problems with floating point numbers.
masked_sums = tf.math.reduce_sum(masked_exps, axis=1, keepdims=True) + epsilon
# Compute the softmax by dividing the e^logits with the sume (e^logits)
softmax = masked_exps/masked_sums
# (1 - mask) * y_pred gets the logits for all the values not in this mask and zeros out the values in the mask. Add those back so that we can repeat the process for other masks.
y_pred = softmax + ((1 - mask) * y_pred)
# At this point we have the masked softmaxes i.e. the pseudo probabilities. We can take the log of these values
y_pred = tf.math.log(y_pred)
# Compute the secondary weight term, which emphasizes different levels of the tree at the appropriate phase. See paper for more details.
decay_term = tf.math.pow(self.beta, fractions)
decayed_alpha = tf.math.multiply(self.alpha, decay_term)
lambda_term = tf.math.exp(tf.math.multiply(tf.expand_dims(-decayed_alpha, axis=1), tf.expand_dims(self.path_lengths, axis=0)))
# Weight them by the level at which the corresponding node appears in the hierarchy
y_pred = y_pred * lambda_term
# Weight them by the class weight after using the target_probabilities as indicators. Then sum them up for each batch
v1 = tf.math.reduce_sum(self.class_weights * (y_pred * target_probabilities), axis=1)
# Finally, find the mean over all batches. Since we are taking logs of numbers <1 (the pseudo probabilities), we have to multiply by -1 to get a +ve loss value.
v2 = -1 * tf.math.reduce_mean(v1)
return v2
if __name__=='__main__':
tree = get_taxonomy_tree()
ts_dim = 5
static_dim = 15
latent_size = 10
output_dim = len(list(tree.nodes))
batch_size = 4
model = get_RNN_model(ts_dim, static_dim, output_dim, latent_size)
input_ts = np.random.randn(batch_size, ts_length, ts_dim)
input_static = np.random.randn(batch_size, static_dim)
outputs = model.predict([input_ts, input_static])
weighted_loss = WHXE_Loss(tree, list(tree.nodes))
unweighted_loss = HXE_Loss(tree)
print(weighted_loss.compute_loss(outputs, outputs))
print(unweighted_loss.compute_loss(outputs, outputs))